Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim: how to select pasted block

Tags:

vim

Is there a vim command to directly select a block of text which has just been pasted?

ps. I know about gv to reselect a block after exiting visual mode. It doesn't apply to this case.

like image 900
Nick Redmark Avatar asked Jan 23 '11 17:01

Nick Redmark


2 Answers

If you want to select it just after paste (before you change anything else), use

nnoremap <expr> gV    "`[".getregtype(v:register)[0]."`]"

. [ and ] marks point to start and end of the last change, v:register is set to the last register used (which is register used for the paste command unless you, for example, yank something), [0] selects only first byte of register type (it is required because for blockwise register it returns <C-v>{width}) and register type is one byte which is just the same as the keystroke you should use in normal mode to invoke visual mode.

I saw this solution somewhere on SO, you may want to search for it in order to get some alternatives.

like image 72
ZyX Avatar answered Nov 15 '22 21:11

ZyX


In my case I have this map:

:nnoremap gp `[v`]

After more research I think the better solution is: " https://vim.fandom.com/wiki/Selecting_your_pasted_text

nnoremap <expr> gp '`[' . strpart(getregtype(), 0, 1) . '`]'
like image 29
SergioAraujo Avatar answered Nov 15 '22 21:11

SergioAraujo