Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

up vote 11 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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