I have text in Vim

  1. hit Ctrl+V to put VIm in block mode
  2. highlight the text I want
  3. type : this gives the this prompt :'<,'>
  4. I add to the prompt my regex s/ /*/g. This leaves me with :'<,'>s/ /*/g and the text highlighted
  5. 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?

link|improve this question

61% accept rate
feedback

1 Answer

up vote 5 down vote accepted

When using ex commands in visual block mode, :, they always operate on the whole line. There are two ways around this:

  1. The \%V atom will match only inside the visual area. Try

    :'<,'>s/\%V /*/g
    

    See :help %V

  2. There are special visual versions of some commands, live v_s or v_r. See :help visual-operators
link|improve this answer
I can't figure out how to get this to work – Evan Carroll Feb 22 '10 at 19:30
the \%V atom will restrict a pattern so that it matches only inside the visual selection. Try :'<,'>s/\%V /*/g – DaveParillo Feb 23 '10 at 2:45
This page has a good writeup on what you are trying to do. vim.wikia.com/wiki/Search_and_replace_in_a_visual_selection – DaveParillo Feb 23 '10 at 2:46
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.