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 gv
to 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.
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.
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.
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] . '`]'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With