Xavier T.
Xavier T.

Reputation: 42258

Is there a vim command to select pasted text?

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.

Upvotes: 50

Views: 6136

Answers (3)

Luc Hermitte
Luc Hermitte

Reputation: 32966

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.

Upvotes: 79

htaccess
htaccess

Reputation: 3130

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.

Upvotes: 6

Peter Rincker
Peter Rincker

Reputation: 45177

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] . '`]'

Upvotes: 14

Related Questions