The Computer Oracle

How do you reuse a visual mode selection?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 3

--

Chapters
00:00 Question
00:29 Accepted answer (Score 319)
00:42 Answer 2 (Score 20)
01:41 Answer 3 (Score 8)
02:27 Answer 4 (Score 3)
03:25 Thank you

--

Full question
https://superuser.com/questions/220666/h...

Answer 2 links:
[this plugin]: https://github.com/iago-lito/vim-visualM...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#vim #texteditors #textediting

#avk47



ACCEPTED ANSWER

Score 331


You may re-select the last selected visual area with gv.




ANSWER 2

Score 21


gv is definitely the fastest method (use last selection), but if you want a stable saved selection region (or several), you can always create macros.

Lets say I want to store a selection of my current method, which goes from lines 25-35. I can create a macro that selects the whole method by typing

q    //start recording
a    //use register a
25G  //Go to line 25
V    //visual-line mode
35G  //Go to line 35
q    // stop recording

I can then get that selection back by typing @a (run macro in register a). Repeat with any register, lines, or sections of lines, that you wish. Obviously if you make changes to the file the selection may change as well, so you may want to consider using marks instead of "hardcoding" line numbers.




ANSWER 3

Score 9


gv works great for recovering the last selection. But one sometimes needs a bit more.

If you ever needed more persistent record, have a look a this plugin we are currently working on on the GitHub.

VisualMarks allows you to save and restore visually selected areas just like you save and mark specific locations in your files with m. After installing, and with the default options, use:

ma

in visual mode to save your current selection to mark a, then

<a

in normal mode to get back to this selection.




ANSWER 4

Score 0


Suppose I wanted to replace Goodbye with Hello and the code below was selected:

public static void main(String[] args){
    System.out.println("Goodbye World");
}

I would type in :s/Goodbye/Hello/ and vim will replace all instances of Goodbye with Hello

It's simply a combination of vim's regular expressions and visual mode selections. When you select, it should autofill '<,'>