17

I usually need to move/copy code snippets between my source files. After I paste a block of text, I usually need to apply some operation to that block such as reindenting them with =.

Is there a way to easily visually select the block of text I just pasted without doing it manually?

200_success
  • 9,549
  • 5
  • 51
  • 64
thameera
  • 17,434
  • 16
  • 40
  • 42

3 Answers3

25

The '[ and '] marks delimit the first and last line of the previously changed or yanked text. The `[ and `] counterparts delimit the respective lines & columns.

Using that, you could visually select the last changed block of lines with '[V'] and then apply the = command.

However, since a paste leaves your cursor at the first line of the content that was pasted, you could also just specify a motion over which the = command should be applied, in this case to the '] mark. This would be ='].

jamessan
  • 11,045
  • 2
  • 38
  • 53
  • This answer describes a method that works for all types of visual selections (including blockwise visual): https://stackoverflow.com/questions/4312664/is-there-a-vim-command-to-select-pasted-text – Adam Byrtek May 07 '17 at 19:20
17

This mapping allows you to reselect the text you just pasted:

nnoremap gV `[v`]

But you should have used [p and ]p instead.

See :help [p.

romainl
  • 40,486
  • 5
  • 85
  • 117
3

Vim includes the following helpful mappings:

`[

and

`]

Which select the first and last character of previously changed text, respectively.

The following series of keys (in normal mode) will select the last pasted text:

`[v`]

For faster access, you could set up a mapping, for example:

nnoremap gp `[v`]
asfallows
  • 131
  • 5