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?

3 Answers 3

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

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

1
  • 3
    +1 for [p and ]p
    – lisowski.r
    Feb 3, 2015 at 18:33
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`]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.