How do you reuse a visual mode selection?

35,169

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

Vim remembers the last selection.

If you enter : in visual mode it will auto-fill :'<,'> for you so you can continue entering command, e.g.

:'<,'>s/old/new/    # (Replace pattern in selected area)

If you want to execute another command on the same visual selection, you can simply bring up the old one from the history and edit it.

:'<,>'s/abc/xyz/    # (This will run the replace command on the same selection area)

Another way to tell the replace command and other commands which support match pattern to use the previous selection is by adding \%V to the pattern, e.g.

:s/\%Vabc/xyz/      # (Same as above)

For more info, please, see :help \%V.

To re-select the previous selection use gv.

Solution 5

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 '<,'>

Share:
35,169

Related videos on Youtube

labyrinth
Author by

labyrinth

My passions are literature and programming. I am working on tools for analysis of poetry using computational linguistics libraries. I'm also working on a webapp for crowdsourced categorization of literature following foundational literary critical approaches (specifically Frye's Anatomy of Criticism). I'm also working on a d3/node.js game for teaching ipv6 (to the technical RFC level). I love my current job because it allows me to study Rails, Node, and other technologies that are useful in my goal to do more digital humanities work. I work with some great people on an innovative, lean team. I love how we have the flexibility to try out just about any new programming/devops technologies as we see fit to meet the customers' needs. I think the only way my job could be better is if it allowed me to work directly in the digital humanities/computational literature field.

Updated on September 17, 2022

Comments

  • labyrinth
    labyrinth over 1 year

    Often when editing code, I'll select a block in visual mode and do a search and replace over the block. After I make the changes, however, it leaves visual mode. How do you do a new find and replace over the same selection?

    • labyrinth
      labyrinth over 13 years
      Well I found one way, but it's a little cumbersome: :'<,'>s/old/new/g Any way to do this w/o having to type in the '<,'> ?
  • labyrinth
    labyrinth over 13 years
    Sorry, that's not what I was asking. What I'm asking is how do you reuse the selection to do another find and replace on the same range as selected before?
  • Scott Nguyen
    Scott Nguyen over 13 years
    After replacing, you will get out of visual mode. type in gv and your visual mode will revert to the previous selection state.
  • sillyMunky
    sillyMunky over 10 years
    zmto enter that you need to know the line numbers. I think its better to define two marks and then use :'a,'b to operate on the range between them (or replace ' with backticks if want granularity within a line)
  • AlexMA
    AlexMA almost 10 years
    Yeah, the line numbers thing was dumb -- I just used it as an example for how to create a selection macro. Using a range of marks is an interesting idea though...
  • labyrinth
    labyrinth over 8 years
    Wow, that sounds really cool. I'll have to give that plugin a try! By the way, can these VisualMarks persist with mkview like you can with marks?
  • iago-lito
    iago-lito over 8 years
    @labyrinth I don't know mkview but.. I guess yes since marks are saved in a separate file and.. I suggest you try anyway :) If they don't persist like you wish, we'll be pleased to receive your feature request on GitHub and try'da process it.
  • skywinder
    skywinder about 8 years
    Awesome! @Heptite how to find this command by myself w\o Stackoverflow?
  • Heptite
    Heptite about 8 years
    @skywinder :help gv
  • Ben Thul
    Ben Thul over 6 years
    @Heptite: I'm not sure if you're being tongue in cheek here, but that command shows you what :gv does once you know about it. But what if I didn't? Teach a (wo)man to fish and all that.
  • Heptite
    Heptite over 6 years
    @BenThul: I think I misunderstood the question. I know about about many of Vim's features simply by reading through the bulk of the documentation over time, starting with ":help".
  • Admin
    Admin about 6 years
    :help select-visual should lead you to the correct manual inside vim @skywinder.