Vim: Select and indent last paste
Posted 30/03, 2020
I stumbled upon this super useful snippet a while ago, for when you’ve pasted something and need to indent it, or whatever. This will select what you just pasted and put you in insert mode:
nnoremap <expr> gp '`[' . strpart(getregtype(), 0, 1) . '`]'
This goes in yer .vimrc obviously.
I completely forgot where I got it from (probably some StackOverflow genius), and not totally sure how it works, to be honest. I was just messing about with my .vimrc the other day and rediscovered it.
p gp >
explanation:
p = paste
gp = grab paste
\> = Indent
I love it.
Update almost 6 years later:
2025-12-14
Neovim and Lua:
vim.keymap.set("n", "gp", function()
local t = vim.fn.getregtype()
local mode = t:sub(1, 1)
return "`[" .. mode .. "`]"
end, { expr = true, silent = true })