VIM: Substituting in a visual selection

March 17, 2016
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” (^ goes to first nonblank character, w moves forward a word, v enters visual mode, e moves forward to end of next word). Then press Escape and enter the following command to change all “us” to “az” in the last-selected area within the current line:
:s/\%Vus/az/g
The result is:
music amaze faze refuse
Source: Search and replace in a visual selection – Vim Tips Wiki – Wikia

posted in Linux by nico