Adjust the other end of a visual selection

14

4

I think this would allow for the visual selection feature of vim to surpass the efficiency even of the mouse by providing an ability to "switch" the current terminus of the selection:

Illustrated (The | shall represent the cursor (Vim has it operate at the left-side of the character it is on) and [text] represents selected text, just pretend they take up zero width. The background of the space between [ ] looks grey, and the character immediately following the | is green):

Start with

Some |text here
Some second line of text
Some more text in the third line 

Press v2j; note the m character is included in the selection (it is highlighted by the cursor now):

Some [text here
Some second line of text
Some |m]ore text in the third line 

Press 10l:

Some [text here
Some second line of text
Some more text |i]n the third line 

It is at this point that I might decide I wanted to start selecting from a different location. e.g., including the "Some" on the first line.

I've got no choice in a typical editor/IDE. I probably would have to move my hand to the mouse, it's usually too painful to select text using traditional methods, the use of word hopping via Ctrl and page-up/down does help somewhat, but no matter what, I've got to commit to one of my terminating points of the selection before I start the selection.

So in Vim I'd love for the ability to take my current state and bring it to this by hitting a mystery binding:

Some [|text here
Some second line of text
Some more text i]n the third line 

Then I can just hit ^ or <Home> (both do the same in this case where there is no leading whitespace) to turn it into

[|Some text here
Some second line of text
Some more text i]n the third line 

Say I change my mind again, I want to adjust the other end!

[Some text here
Some second line of text
Some more text |i]n the third line 

Hit e:

[Some text here
Some second line of text
Some more text i|n] the third line 

Beautiful! I can do whatever I want next with this selection now.

So what's this mystery binding?

Also, even more potentially efficient would be a set of alternate bindings for some of the most-used movement commands to another row of keys, and these would (when in visual mode) always move the other end of the selection. So in that visual mode selection operation, if the movement keys for the other end of the selection were set to "yuio" (probably not a good choice since we kind of need y but just bear with me) we could have typed

v2j10l5ye

rather than

v2j10lX^Xe

Hey you know what, that's not even that much of an improvement. Just gimme that X feature. :)

The perfect answers that I'm looking for:

  • Hey, look up :help some_awesome_vim_feature_that_just_does_this
  • Hey, look at https://github.com/awesome_user/vim_plugin_that_does_this
  • Hey, look at :help vimscript_functions_that_allow_mutation_of_visual_selections

I'm pretty new to this fascination with Vim so I am very green when it comes to Vimscript, and honestly I'd like to stay away from trying to learn yet another language, so hopefully I won't be forced to build this as a plugin.

Steven Lu

Posted 2013-05-25T02:42:29.813

Reputation: 2 818

such a long question for such a very simple answer. o :) – dylnmc – 2016-11-29T20:24:02.247

Okay. Now I don't know what is going on here (question got mysteriously ninja-moved from SO to SU), but I originally posted this on SO and I really would prefer it to stay there. With all due respect to non-programmers, only programmers could ever care this much about text editing. Of course, all users of vim could benefit from learning how this might work, I just think it would get more eyeballs and a better chance at getting answered over there where the plugin devs are. – Steven Lu – 2013-05-25T03:44:38.333

Do you sometimes read the :help? :help visual-mode has all the info you might need about visual mode, including o. – romainl – 2013-05-25T04:23:02.290

I do sometimes read it, but it probably could have taken about the same time writing out my thoughts. It is more definite a task than guessing at what the help string is. – Steven Lu – 2013-05-25T04:35:26.100

Learning is better than guessing. Heptite's answer teached you two things, can you name them? – romainl – 2013-05-25T04:50:41.017

Yeah, i mean there are lots of things to be learned but I've already forgotten what O capital O does, but I'll remember o. The second thing you are referring to is probably use the :help more. And I have been using it more than when I started. – Steven Lu – 2013-05-25T06:12:05.720

No, the second thing to take from Heptite's answer was v_{char} which allows you to search the help for visual mode commands. A knowledge nugget that can be extremely useful, when you want info on an insert mode command, for example. – romainl – 2013-05-25T07:41:47.120

Oh yeah that is a good point. I had subconsciously pieced it together that i_-prefixed help entries could be used to look for built-in insert-mode key mappings – Steven Lu – 2013-05-25T22:45:18.953

Answers

48

I'm not sure I'm actually understanding what you want, but perhaps it is this:

:help v_o

Basically, while text is visually highlighted, you can press o and it will jump to the other "end" of the visual region without modifying it.

Heptite

Posted 2013-05-25T02:42:29.813

Reputation: 16 267

@StevenLu This should probably be the accepted answer, given that it not only solved your problem, has a ton of upvotes, also happened exactly what I needed on the first Google result, and is actually the only answer submitted by anyone... – Kingdon – 2018-01-15T21:47:06.233

Yep, I think i never accepted it because it was posted so quickly that I was not allowed to do it yet at the time, and I’ve been happily using this Vim feature for nearly five years since, never needing to remind myself what the key is, as it’s super easy to remember. – Steven Lu – 2018-01-15T22:17:28.313

2Holy crap, yes!!! That's even better than I had hoped, it's already bound to a single key, so X above is actually o – Steven Lu – 2013-05-25T04:06:24.090

1Allow me also to point out that the right click mouse (if you have your terminal configured to pass it through to Vim) will intelligently let it adjust the nearest end of the current visual selection to where the cursor is. This makes it a much less pain-inducing method for adjusting selection area because it doesn't throw away the location of the opposite end of the selection. – Steven Lu – 2013-06-02T04:18:44.607

@StevenLu: It's not thrown away anyway. You can tap o again and go back. – Heptite – 2013-06-02T04:23:02.230