Search code examples
vimcygwinvi

vi replaces with empty when searching


In vi (from cygwin), when I do searching:

:%s/something

It just replaces the something with empty string like

:%s/something//  .

I've googled for a while but nothing really mentions this. Is there anything I should add to the .vimrc or .exrc to make this work?

Thanks!


Solution

  • In vi and vim, when you search for a pattern, you can search it again by simply typing /. It is understood that the previous pattern has to be used when no pattern is specified for searching. (Though, you can press n for finding next occurence)

    Same way, when you give a source (pattern) and leave the replacement in substitute command, it assumes that the replacement is empty and hence the given pattern is replaced with no characters (in other words, the pattern is removed)

    In your case, you should understand that % stand for whole file(buffer) and s for substitute. To search, you can simply use /, followed by a pattern. To substitute , you will use :s. You need not confuse searching and substituting. Hence, no need for such settings in ~/.exrc. Also, remember that / is enough to search the whole buffer and % isnt necessary with /. / searches the entire buffer implicitly.

    You may also want to look at :g/Pattern/. Learn more about it by searching :help global or :help :g in command line.