4

Let's say that I've got the following text:

This allows you to select some text using Vim's visual mode

With the cursor on the 'a' in 'allows'. If I type vaw I switch to visual mode and then select "a word" ("allows "). If I repeat the aw then Vim also selects the "you ". Let's say I do this once more (highlighting "to ") and then realize that I've gone too far.

Is there a way to shrink/reduce the size of the visual area (other than using the h,j,k,l keys)?

I'd love to be able to either 'undo' my last command (i.e., have Vim move the selection back to before I typed that last 'aw') or else use the motion/text object commands to reduce the size of the visual area.

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
MikeTheTall
  • 3,214
  • 4
  • 31
  • 40
  • Based on your post and [this related one](https://stackoverflow.com/questions/70937148/vi-increases-the-visual-selection-but-can-i-reduce-the-visual-selection-in-a), I decided to make the [vim-visual-history](https://github.com/Matt-A-Bennett/vim-visual-history) plugin that keeps a traversable record of previous visual selections with `[v`, `]v`, `[V` and `]V`. So in your case, you could just do [v to go back one selection. The plugin works, but is still under construction. – mattb Feb 04 '22 at 11:43

2 Answers2

7

yes, you can keep pressing ge till it selecting allows again.

Kent
  • 189,393
  • 32
  • 233
  • 301
  • D'oh - I should have looked closer at the motion commands & noticed that you can move backwards :) – MikeTheTall Oct 24 '14 at 16:03
  • 1
    Is there a way to move backwards by text object? – MikeTheTall Oct 24 '14 at 16:05
  • @MikeTheTall what do you mean by that? can you make example ? – Kent Oct 24 '14 at 16:11
  • 1
    How's this: I've got a visual selection covering two sentences. I type "as" to extend it rightward by another sentence, and then realize that I don't want that third sentence. Is there a way to shrink the selection and exclude that third sentence? (Kinda of a leftwards "as"?) – MikeTheTall Oct 24 '14 at 22:35
  • @MikeTheTall there is no built-in way to do that. you have to do `?xx` or `Tx` in that case. – Kent Oct 24 '14 at 23:09
  • @MikeTheTall see my [visual-history plugin](https://github.com/Matt-A-Bennett/vim-visual-history) for a way to do just that. Also see the answer below for a demo. – mattb Feb 04 '22 at 17:04
0

Based on your post (and on this one), I decided to make the vim-visual-history plugin that keeps a traversable record of previous visual selections with [v, ]v, [V and ]V. So in your case, you would just do [v to go back one selection. You can keep on pressing [v to go back further in time, and use ]v to go forwards again. The plugin works for your case, and also selections using text objects etc.

[count][v : Reselect previous visual selection
[count]]v : Reselect next visual selection
[V : Reselect first visual selection
]V : Reselect last visual selection

enter image description here

You can also give a count to jump through the history faster:

enter image description here

mattb
  • 2,787
  • 2
  • 6
  • 20