How do I do a block search-and-replace with Vim?
I have text in Vim
- hit
Ctrl+V
to put VIm in block mode - highlight the text I want
- type
:
this gives the this prompt:'<,'>
- I add to the prompt my regex
s/ /*/g
. This leaves me with:'<,'>s/ /*/g
and the text highlighted - I hit enter
Unfortunately, it operates on the whole line for the block, not just the block. Is there anyway to do a block search and replace?
Solution 1:
When using ex commands in visual block mode, :
, they always operate on the whole line. There are two ways around this:
-
The
\%V
atom will match only inside the visual area. Try:'<,'>s/\%V /*/g
See
:help %V
- There are special visual versions of some commands, live v_s or v_r. See
:help visual-operators