Reselect pasted text


To paste text in Vim we use the p mapping. This text text can come from another buffer, copied or “yanked” with y, or it can come from another application on your system using the clipboard register.

But sometimes after pasting text, we might want to do some additional clean up on it. Perhaps, we want to perform some substitution, indent it, or align it. We can do any of these things if the text is visually selected again.

Instead of manually reselecting the text you just pasted, try adding this mapping to your .vimrc:

" reselect pasted text
nnoremap gp `[v`]

Now you can use gp to instantly reselect the text you just pasted. This pairs nicely with the native Vim mapping gv, which reselects the last visual selection.

Check out the brief demo below. It it, I paste some text with p then reselect it using my gp mapping and then press = to indent the selected text.

How useful was this tip?

Average rating 4.8 / 5. Vote count: 61

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

105 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts