Search code examples
vimreplace

Search and replace on specific lines in Vim


I can use

:5,12s/foo/bar/g

to search for foo and replace it by bar between lines 5 and 12. How can I do that only in line 5 and 12 (and not in the lines in between)?


Solution

  • Vim has special regular expression atoms that match in certain lines, columns, etc.; you can use them (possibly in addition to the range) to limit the matches:

    :5,12s/\(\%5l\|\%12l\)foo/bar/g
    

    See :help /\%l