Vim logoThis came about as i wanted to replace just a few lines of text in vi.

Work out what range of lines to select

:set number

Find the lines you wish to modify

35 #Hosts per intl
36 192.168.0.22 uk.dev.site.com
37 192.168.0.22 de.dev.site.com
38 192.168.0.22 es.dev.site.com
39 192.168.1.92 it.dev.site.com
40 192.168.0.21 fr.dev.site.com

Do the replacement

:36,40s/patternToReplace/replacementText/gc

Tips

  • If your looking to do a lot of replacements, take out the ‘c’ at the end of the substitution (if your confident on what your changing), this is the confirmation option
  • If your unfamiliar with all the commands in vi, simply type ‘u’ (undo) if you mess up
  • Like the command line, vi has a history so type ‘:’ and use the up and down arrows to retrieve your previous text replacements
  • To turn line numbering off, type :set nonumber