Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 12 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
share|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
1  
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .