开发者

Is there a vim command to select pasted text?

开发者 https://www.devze.com 2023-01-27 08:41 出处:网络
I find myself often repeating the following pattern of operations. I usually go into visual mode, select some lines or block. Then I yank them using y, and paste them using p or P. The next step is t

I find myself often repeating the following pattern of operations.

I usually go into visual mode, select some lines or block. Then I yank them using y, and paste them using p or P. The next step is to select the pasted text, to replace a variable or function name or change indentation.

I know that I can use gvto reselect the "origin" but what I would like is a simila开发者_如何学JAVAr command to select the "destination".

:help gv mentions :

After using "p" or "P" in Visual mode the text that was put will be selected.

but it is only useful when you are replacing a selection by the content of register, not when you are inserting a whole new block.


You are looking for

`[v`]

'[ and '] are marks automatically set by vim to the start and the end of the "previously changed or yanked text". v switches to visual mode in between.


I prefer the following simple mapping to Benoit's function

nnoremap <expr> g<c-v> '`[' . strpart(getregtype(), 0, 1) . '`]'

Learn more about expression maps:

:h :map-expression

As @ZyX pointed out the strpart is not needed and can be rewritten as:

nnoremap <expr> g<c-v> '`[' . getregtype()[0] . '`]'


One of your use cases is to change indentation after pasting.

I use the following maps to achieve this:

nnoremap <leader>[ `[V`]<
nnoremap <leader>] `[V`]>

They do the following:

  • de-indent the recently pasted block
  • indent the recently pasted block

I find these very useful and well used maps.

0

精彩评论

暂无评论...
验证码 换一张
取 消