Skip to main content

Linux tip - Find and replace in vi

To find and replace one or more occurences of a given text pattern with a
new text string, use the s[ubstitute] command.

There are a variety of options, but these are what you most probably want:

:%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//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.

For a full description and some more interesting examples of the substitute command refer to

:help substitute

See also:

:help cmdline-ranges
:help pattern
:help gdefault


To find out more check out:
http://www.vim.org/tips/tip.php?tip_id=31

Comments

Popular posts from this blog

Start with flask

1. Install python 2. Python comes with pip. Else install pip 3. pip install Flask 4. Create a file flaskFirst.py from flask import Flask app = Flask(__name__) @app.route('/') def index():     return 'Index Page' 3. Go to the path where the flaskFirst.py is present 4. export FLASK_APP=flaskFirst.py 5. flask run