Thursday, 2 March 2017

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" (^ 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

No comments:

Post a Comment