Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a vim command to select pasted text?

Tags:

vim

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 similar 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.

like image 796
Xavier T. Avatar asked Nov 30 '10 10:11

Xavier T.


People also ask

How do I yank in Vim?

To yank one line, position the cursor anywhere on the line and type yy . Now move the cursor to the line above where you want the yanked line to be put (copied), and type p . A copy of the yanked line will appear in a new line below the cursor.

How do I select something in Vim?

In vim , text is selected by entering Visual mode. This can be done in multiple ways. v (lowercasev) begins regular Visual mode, and works similar to selecting text with a mouse.


2 Answers

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.

like image 99
Luc Hermitte Avatar answered Sep 21 '22 15:09

Luc Hermitte


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] . '`]' 
like image 29
Peter Rincker Avatar answered Sep 21 '22 15:09

Peter Rincker