7

I know that gv reselects the last visual selection. This is great and I use it frequently.

But sometimes I want to do something related but different: from the current cursor position, select a block in the same shape as the prior visual selection, but positioned where the cursor is now.

In particular, I often have this desire when pasting a chunk of text and then wanting to do some sort of substitution or transformation to it. So this command would let me reselect what I just pasted. (For that reason, also useful would be a command to "visually select last paste," but there are nonetheless other instances where what I literally want is "reselect last visual shape irrespective of whether that reselects the last paste." EDIT: Please see Is there a way to reselect the last pasted text, rather than the last actual selection, starting at the current cursor position? for this question)

(Note: if there is not a built-in way to do this, I'm comfortable editing ~/.vim to add a vimscript example [and possibly modify it] and bind this to an easy key combination, but writing one from scratch to do this is beyond my present ability.)

  • 1
    You could do a macro .. – VanLaser Dec 7 '15 at 19:20
  • 1
    This is a good idea (I assume you mean something like qpv[select...]q[move...]@p") but it doesn't really get at what I want, since in normal course of use I want to operate in visual mode, then only "realize" that I want the selection again after the paste operation. I'm looking for a few keystrokes to help in that circumstance, though I agree this is useful if I am operating slowly enough to anticipate needing to reselect the region. – Philip Dec 7 '15 at 19:32
  • Actually there's another case where this isn't possible: pasting from outside vim (e.g. via the * register). In this case the question should be as in the parenthetical: is there a motion to visually select the last paste? This is useful e.g. for sending example code from Stackexchange from vim to a REPL. – Philip Dec 7 '15 at 19:36
  • Actually, I'm not sure what you want - to remember the "coordinates" of last paste, before further changes, in order to reselect them easily? – VanLaser Dec 7 '15 at 19:52
6

This is a simple remap that allows you to re-select last pasted (or changed) text:

noremap gV `[v`]

From comments in original post, perhaps you want to save the coordinates of the last change/paste before other/further changes, and restore that visual selection?

  • 1
    OK yes, tested this and it works; this is great. Really this is my fault for asking two related questions in one. I think what I'll do is change this question to be the one you've correctly answered here, and then re-ask the related question as a separate question, unless a mod has a better idea. – Philip Dec 8 '15 at 14:39
  • Actually that question is asked and answered here: vi.stackexchange.com/questions/31/… Sorry--my fault for making a mess. – Philip Dec 8 '15 at 20:20
4

To reselect the old shape, you can do this:

1v
  • 1
    It doesn't reselect the same shape, but the same number of characters. – muru Dec 8 '15 at 7:52
  • Tested this and it also seems to work with the caveat @muru notes. In most instances when the re-select is not for selecting pasted text but recreating a selection then correctly positioning the cursor will imply that selecting the same number of characters will recreate the same shape. BUT, I can imagine exceptions. So still curious about an alternative approach that duplicates the shape. Maybe this is impossible in circumstances where lines don't have the same length. – Philip Dec 8 '15 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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