Vim Tips Wiki
(Slight tweaks + example + little more (not really "visual", but related) + remove html entities)
(Substituting in a visual selection)
Line 38: Line 38:
 
:'<,'>s/red/green/g
 
:'<,'>s/red/green/g
 
</pre>
 
</pre>
 
Note that substitution applies to whole lines. See [[VimTip63|Applying substitutes to a visual block]] for how to operate on just the selected region.
 
   
 
To repeat an Ex command over a previously selected block, use the ''':''' history. That is, press ''':''' then <Up>, then edit a previous command.
 
To repeat an Ex command over a previously selected block, use the ''':''' history. That is, press ''':''' then <Up>, then edit a previous command.
   
  +
==Substituting in a visual selection==
===References===
 
  +
The substitute command (<tt>:s</tt>) applies to whole lines, however the <tt>\%V</tt> atom will restrict a pattern so that it matches only inside the visual selection. This works with characterwise and blockwise selection (and is not needed with linewise selection).
  +
  +
For example, put the cursor on this line:
  +
<pre>
  +
music amuse fuse refuse
  +
</pre>
  +
  +
In normal mode, type <tt>^wvee</tt> to visually select "amuse fuse". Then press Escape and enter the following command to change all "us" to "az" in the last-selected area:
  +
<pre>
  +
:s/\%Vus/az/g
  +
</pre>
  +
  +
The result is:
  +
<pre>
  +
music amaze faze refuse
  +
</pre>
  +
  +
The [[VimTip63|vis.vim plugin]] is another approach to apply substitutes to a selected region.
  +
 
==References==
 
*{{help|:'}}
 
*{{help|:'}}
 
*{{help|history}}
 
*{{help|history}}

Revision as of 23:36, 10 August 2008

Tip 438 Printable Monobook Previous Next

created March 8, 2003 · complexity intermediate · author Chris Smith · version 5.7


Visual selection basics

You can visually select text, then operate on the selection.

Press v to select a range of text, or V to select whole lines, or Ctrl-V (Ctrl-Q if you use Ctrl-V to paste) to select a block.

After starting the selection, move the cursor with any normal-mode movement commands (such as j to move down, or w to move by a word, or / to search).

Marks for the beginning and end of the visual selection are automatically defined:

'<  start line
`<  start character
'>  end line
`>  end character

You can press gv to reselect the last visual selection, but the '< and '> marks defining the beginning and end of the block persist even when the selection highlight has been removed, so gv is not necessary to repeat a command.

Ex commands

When text is visually selected, press : to enter a command. The command line will automatically enter the range:

:'<,'>

You can enter a command such as s/red/green/g to replace each red with green in all lines of the last visual selection. The command will appear as:

:'<,'>s/red/green/g

To repeat an Ex command over a previously selected block, use the : history. That is, press : then <Up>, then edit a previous command.

Substituting in a visual selection

The substitute command (:s) applies to whole lines, however the \%V atom will restrict a pattern so that it matches only inside the visual selection. This works with characterwise and blockwise selection (and is not needed with linewise selection).

For example, put the cursor on this line:

music amuse fuse refuse

In normal mode, type ^wvee to visually select "amuse fuse". Then press Escape and enter the following command to change all "us" to "az" in the last-selected area:

:s/\%Vus/az/g

The result is:

music amaze faze refuse

The vis.vim plugin is another approach to apply substitutes to a selected region.

References

Searching with / and ?

In visual mode, / and ? will update the visual selection just like any other cursor-movement command (that is, when in visual mode, searching will extend the selection).

In order to actually search within the visual selection, you will need to use the \%V atom, or use the markers defined by the visual selection with the \%>'< and \%<'> atoms. This is best done by leaving the visual selection with <Esc> before entering your search. You may want to consider a mapping to automatically leave visual selection and enter the appropriate atoms. For example:

:vnoremap <M-/> <Esc>/\%V

Using this mapping, you can press Alt-/ in order to automatically fill in a "range" for your search just like using an Ex command with :. To use this, move to the first line of interest and press V to start line-wise visual selection. Move down (press j for a line or } for a paragraph, etc). When you have selected the area you want to search, press Alt-/. The visual selection will be removed, and a search command will start. You will see:

/\%V

Add what you want to find, then press Enter. For example, you may enter green and see:

/\%Vgreen

When you press Enter, each occurrence of "green" will be highlighted, but only in the area that you had previously selected.

Here are two further examples that do not use a visual selection. The first command searches only in lines 10 to 20 inclusive. The second searches only between marks a and b.

/\%>9l\%<21lgreen
/\%>'a\%<'bgreen

See Search keywords in C function which uses this technique to search within a function in a C program.

References

Comments