It seems with VI you are always learning a new trick to it every other day, so I thought I’d start adding the tools I use regularly here.

The search and replace in VI is very powerful and if you know a flavour of regular expressions already, you’ll enjoy it.


:%s/foo/bar/g find each occurance of 'foo' and replace it with 'bar' without asking for confirmation
:%s/foo/bar/gc find each occurance of 'foo' and replace it with 'bar' asking for confirmation first
:%s/<foo>/bar/gc find (match exact word only) and replace each occurance of 'foo' with 'bar'
:%s/foo/bar/gci find (case insensitive) and replace each occurance of 'foo' with 'bar'
:%s/foo/bar/gcI find (case sensitive) and replace each occurance of 'foo' with 'bar'
NB: Without the 'g' flag, replacement occurs only for the first occurrence in each line.

Source: http://www.vim.org/tips/tip.php?tip_id=31

Categories: LAMP

Jonathan Adjei

Jon's expertise in web development is legendary and he oversees all technical aspects of our projects from development to hosting (all through the command line!) Jon is excited by the latest techniques and keeps the company on track by finding ways to adopt new practices into our workflow.