This article is part of the on-going Vi / Vim Tips and Tricks series. Vim is commonly mentioned as text editor, not text creator. Why ? Because we spend lot of time editing an existing text than creating new text. In the text editing, text/pattern substitutions becomes a vital part.
In this article, let us review how to perform both basic and advanced text and pattern substitution features in Vi and Vim Editor. These features are explained using 12 very practical and powerful text substitution examples.
Syntax of the text substitution inside vim editor:
:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]
Following are three possible flags.
- [c] Confirm each substitution.
- [g] Replace all occurrences in the line.
- [i] Ignore case for the pattern.
Example 1. Substitute all occurrences of a text with another text in the whole file
This is the basic fundamental usage of the text substitution inside Vi editor. When you want a specific text to be replaced with another text in the entire file then you can use the following sequence.
:%s/old-text/new-text/g
- %s – specifies all lines. Specifying the range as ‘%’ means do substitution in the entire file.
- g – specifies all occurrences in the line. With the ‘g’ flag , you can make the whole line to be substituted. If this ‘g’ flag is not used then only first occurrence in the line only will be substituted.
Example 2. Substitution of a text with another text within a single line
When you want a specific text to be replaced with another text within a single line in a case insensitive manner. Specifying no range means, do substitution in the current line only. With the ‘i’ flag, you can make the substitute search text to be case insensitive.
:s/I/We/gi
Example 3. Substitution of a text with another text within a range of lines
With the range, you can make only a range of line to be affected in the substitution. Specifying 1, 10 as range means, do substitution only in the lines 1 – 10.
:1,10s/helo/hello/g
Example 4. Substitution of a text with another text by visual selection of lines
You can also select a specific lines by visually selecting those lines. Press CTRL + V in command mode, use navigation keys to select the part of the file you want to be substituted. Press ‘:’ which will automatically formed as :’<,’> Then you can use the normal substitute as
:'<,'>s/helo/hello/g
Example 5. Substitution of a text with another text only the 1st X number of lines
Using count in substitution, If you specify the count N in the substitution then it means do substitution in N lines from the current position of the cursor. do substitution in 4 lines from the current line.
:s/helo/hello/g 4
Example 6. Substitute only the whole word and not partial match
Let us assume that you want to change only the whole word ‘his’ to ‘her’ in the original text mentioned below. If you do the standard substitution, apart from changing his to her, it will also change This to Ther as shown below.
Standard Subsitution
Original Text: This is his idea :s/his/her/g Translated Text: Ther is her idea
Whole Word Subsitution
Original Text: This is his idea :s/\<his\>/her/ Translated Text: This is her idea
Note:: You should enclose the word with < and > , which will force the substitution to search only for the full word and not any partial match.
Example 7. Substitute either word1 or word2 with a new word using regular expression
In the following example, it will translate any occurrences of either good or nice will be replaced with awesome.
Original Text: Linux is good. Life is nice. :%s/\(good\|nice\)/awesome/g Translated Text: Linux is awesome. Life is awesome.
You can also do substitution by specifying regular expression. Following example does the substitution of hey or hi to hai. Please note that this does not do any substitution for the words ‘they’, ‘this’.
:%s/\<\(hey\|hi\)\>/hai/g
- \< – word boundary.
- \| – “logical or” (in this case hey or hi)
Example 8. Interactive Find and Replace in Vim Editor
You can perform interactive find and replace using the ‘c’ flag in the substitute, which will ask for confirmation to do substitution or to skip it as explained below. In this example, Vim editor will do a global find the word ‘awesome’ and replace it with ‘wonderful’. But it will do the replacement only based on your input as explained below.
:%s/awesome/wonderful/gc replace with wonderful (y/n/a/q/l/^E/^Y)?
- y – Will replace the current highlighted word. After replacing it will automatically highlight the next word that matched the search pattern
- n – Will not replace the current highlighted word. But it will automatically highlight the next word that matched the search pattern
- a – Will substitute all the highlighted words that matched the search criteria automatically.
- l – This will replace only the current highlighted word and terminate the find and replace effort.
Example 9. Substituting all lines with its line number.
When the string starts with ‘\=’, it should be evaluated as an expression. Using the ‘line’ function we can get the current line number. By combining both the functionality the substitution does the line numbering of all lines.
:%s/^/\=line(".") . ". "/g
Note: This is different from the “:set number” where it will not write the line numbers into the file. But when you use this substitution you are making these line number available inside the file permanently.
Example 10. Substituting special character with its equivalent value.
Substituting the ~ with $HOME variable value.
Original Text: Current file path is ~/test/ :%s!\~!\= expand($HOME)!g Translated Text: Current file path is /home/ramesh/test/
You can use expand function to use all available predefined and user defined variables.
Example 11. Alter sequence number in a numbered list while inserting a new item
Assume that you have a numbered list like the following inside a text file. In this example, let us assume that you want to add a new line after Article 2. For this, you should change the number of all other articles accordingly.
vi / vim tips & tricks series Article 1: Vi and Vim Editor: 3 Steps To Enable Thesaurus Option Article 2: Vim Autocommand: 3 Steps to Add Custom Header To Your File Article 3: 5 Awesome Examples For Automatic Word Completion Using Ctrl-X Article 4: Vi and Vim Macro Tutorial: How To Record and Play Article 5: Tutorial: Make Vim as Your C/C++ IDE Using c.vim Plugin Article 6: How To Add Bookmarks Inside Vim Editor Article 7: Make Vim as Your Bash-IDE Using bash-support Plugin Article 8: 3 Powerful Musketeers Of Vim Editor ? Macro, Mark and Map Article 9: 8 Essential Vim Editor Navigation Fundamentals Article 10: Vim Editor: How to Correct Spelling Mistakes Automatically Article 11: Transfer the Power of Vim Editor to Thunderbird for Email Article 12: Convert Vim Editor to Beautiful Source Code Browser
3rd Article “Make Vim as Your Perl IDE Using perl-support.vim Plugin” got missed. So when you want
to add it, then you want to change “Article 3″ to “Article 4″, “Article 4″ to “Article 5″, upto “Article 12″ to “Article 13″.
This can be achieved by the following vim substitution command.
:4,$s/\d\+/\=submatch(0) + 1/
- Range: 4,$ – 4th line to last line.
- Pattern to Search – \d\+ – digits sequence
- Pattern to Replace – \=submatch(0) + 1 – gets the matched pattern and adds 1 to it.
- Flag – as there is no flag, by default it substitutes only the first occurrence.
After executing the substitute statement the file will become like this, where you can
add the 3rd Article.
vi / vim tips & tricks series Article 1: Vi and Vim Editor: 3 Steps To Enable Thesaurus Option Article 2: Vim Autocommand: 3 Steps to Add Custom Header To Your File Article 4: 5 Awesome Examples For Automatic Word Completion Using Ctrl-X Article 5: Vi and Vim Macro Tutorial: How To Record and Play Article 6: Tutorial: Make Vim as Your C/C++ IDE Using c.vim Plugin Article 7: How To Add Bookmarks Inside Vim Editor Article 8: Make Vim as Your Bash-IDE Using bash-support Plugin Article 9: 3 Powerful Musketeers Of Vim Editor ? Macro, Mark and Map Article 10: 8 Essential Vim Editor Navigation Fundamentals Article 11: Vim Editor: How to Correct Spelling Mistakes Automatically Article 12: Transfer the Power of Vim Editor to Thunderbird for Email Article 13: Convert Vim Editor to Beautiful Source Code Browser
Note: Check the substitution changed the 3 to 4, 4 to 5 and so on. Now we can add a new line mentioning it as Article 3, and no need to do any manual changes.
Example 12. Substituting the sentence beginnings with upper case. ( i.e title case the entire document ).
While formatting a document, making the title case is also an important thing. It can be done easily with substitution.
:%s/\.\s*\w/\=toupper(submatch(0))/g
- \.\s*\w – Search Pattern – literal . ( dot ) followed by Zero or more space, and a word character.
- toupper – converts the given text to upper case.
- submatch(0) – returns the matched pattern.
Text before substitution:
Lot of vi/vim tips and tricks are available at thegeekstuff.com. reading
these articles will make you very productive. following activities can be
done very easily using vim editor.
a. source code walk through,
b. record and play command executions,
c. making the vim editor as ide for several languages,
d. and several other @ vi/vim tips & tricks.
Text after substitution (Changes are in bold)
Lot of vi/vim tips and tricks are available at thegeekstuff.com. Reading
these articles will make you very productive. Following activities can be
done very easily using vim editor.
a. Source code walk through,
b. Record and play command executions,
c. Making the vim editor as ide for several languages,
d. And several other @ vi/vim tips & tricks.
Recommended Reading
Vim 101 Hacks, by Ramesh Natarajan. I’m a command-line junkie. So, naturally I’m a huge fan of Vi and Vim editors. Several years back, when I wrote lot of C code on Linux, I used to read all available Vim editor tips and tricks. Based on my Vim editor experience, I’ve written Vim 101 Hacks eBook that contains 101 practical examples on various advanced Vim features that will make you fast and productive in the Vim editor. Even if you’ve been using Vi and Vim Editors for several years and have not read this book, please do yourself a favor and read this book. You’ll be amazed with the capabilities of Vim editor.
Awesome Vim Editor Articles
Following are few awesome Vi / Vim editor tutorials that you might find helpful.
- Vi and Vim Macro Tutorial: How To Record and Play
- Turbocharge Firefox Browser With Vim Editor Functionality Using Vimperator Add-on
- Tutorial: Make Vim as Your C/C++ IDE Using c.vim Plugin
- Convert Vim Editor to Beautiful Source Code Browser for Any Programming Language
Note: Please subscribe to The Geek Stuff and don’t miss any future Vi and Vim editor tips and tricks.
Get free Unix tutorials, tips and tricks straight to your email in-box.
If you enjoyed this article, you might also like..



My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about
{ 4 trackbacks }
{ 24 comments… read them below or add one }
Thanks for these great examples.
I am a :g kind of vi guy
I’m ad-hoc vim user, somehow tend to do develompent under more resource consuming/less flexxible but bit more intuitive editors but this article is another great eye opener. Keep them comming…
Don’t forget \v for very-magic mode so you don’t have to escape everything.
ie., s/\(hello\|hai\)/hi/ becomes s/\v(hello|hai)/hi/g
@housetier,
We are glad you found this article helpful. Thanks for the comment.
@Chris,
How do you use :g for substitute? One of the example I can think of would be the following:
g& Synonym for ":%s//~/&" (repeat last substitute on all lines with the same flags). Mnemonic: global substitute. {not in Vi}@m,
Yeah. One of our goal is to educate everybody on the power of Vim Editor. I’m glad you found this article as an eyeopener to start using the Vim editor intensively.
@Shawn,
Awesome tip. Thanks for the great tip about the \v , which escapes everything. So, example 7 in this article can be written as:
using :g to do a substitute is as follows
:g/xyz/s//abc/
This substitutes the first occurance of xyz with abc on every line
:g/xyz/s//abc/g
This substitutes all occurances of xyz with abc.
i like it. thanks
@Chris,
Thanks for explaining how you use the :g for substitution in vim.
@Behrad,
Thanks for the comments. I’m really glad that you liked this article and found it useful.
Hi Ramesh,
NIce article! I have a question though.
In the examples provided above, I would like to take the following example for my question:
:s/I/We/gi
So in the above command any occurrence of I or i will be replaced with We. But this can also affect words like Ignore, classic, and so on where we have occurrence of either I or i.
Is there any method that we can use to find a replace the strings occuring at a certain given position in the word? Like if we want to change the string occuring only in the first palce of a word and so on..
@Gagan,
Yes, there are methods such as:
1. Replace only words, not substrings. ( this is already explained Example 6. )
2. Replace only if “I” occurs in the first position of the word
3. Replace only if “I” occurs in a specific position of the word ( 6th position )
Hi,
I m trying to find (similar) and replace (distinct) few strings in 140 files.
request you to help me on this
definitely, we can help you. Kindly provide some more details about your requirement, and example if possible.
What if I want to replace not the first occurrence, but the second one? E.g. lines are like this:
command file1.inp >& file.inp
command file1.inp >& file.inp
Is there a way to substitute the second “inp” in each line to “out”?
Sorry, while at it, I have a second question, a follow up to a great tip #11.
Is there a way to renumber columns consecutively, no matter what the numbers are there, i.e.
Article 5: Vi and Vim Editor: 3 Steps To Enable Thesaurus Option
Article 1: Vim Autocommand: 3 Steps to Add Custom Header To Your File
Article 11: 5 Awesome Examples For Automatic Word Completion Using Ctrl-X
Article 114: Vi and Vim Macro Tutorial: How To Record and Play
Article 2: Tutorial: Make Vim as Your C/C++ IDE Using c.vim Plugi
How to make it into 1,2,3,4, etc. This would require some built in match counter I guess…
thanks for good info.
@Visvaldas
There is more than one way to do it.
1. What if I want to replace not the first occurrence, but the second one ?
Way 1: According to the example given, following is the very simplest way.
%s/inp$/out/
inp$ specifies the inp present at the end of the line ($) should be substituted.
Way 2: If you wanted a particular occurrence to be substituted then use the “Interactive find and replace” explained in Example 8.
2. Renumber columns consecutively
Simplest way is to write a macro for it as:
Step 1: Make the 1st line as 1.
Article 1: Vi and Vim Editor: 3 Steps To Enable Thesaurus Option
Article 1: Vim Autocommand: 3 Steps to Add Custom Header To Your File
Article 11: 5 Awesome Examples For Automatic Word Completion Using Ctrl-X
Article 114: Vi and Vim Macro Tutorial: How To Record and Play
Article 2: Tutorial: Make Vim as Your C/C++ IDE Using c.vim Plugi
Step 2: Place the cursor in 1, and Start recording the macro as qa
Step 3: Copy the current number ( 1 ) to the register as “ayw
Step 4: Go to the next line as j
Step 5: Change word using dw
Step 6: Paste the word from register as “aP
Step 7: Increment the current number as press CTRL + A
Step 8: Stop recording the macro as q.
Use the above macro @q to change the number for all the articles. Refer our earlier article for more clarification in macros Vi and Vim Macro Tutorial: How To Record and Play
Do we have any possibility to replace any character in a particular position with the replace chracter. For ex:
1: #!/usr/local/bin/perl
2: $resultstring = “”;
3: print(“Enter your input – type an empty line to quit\n”);
4: $input = ;
5: chop ($input);
6: while ($input ne “”) {
7: $resultstring .= $input;
8: $input = ;
9: chop ($input);
10: }
11: print (“Here is the final string:\n”);
12: print (“$resultstring\n”);
In the above program copied from a e-book I wanna replace the numbers with null in the first 3 positions.
Regards,
Arun Venky
@Arun
We can match any character by using . ( dot ) in the pattern.
%s/\v^.{3}//g
. – match any character
{3} – 3 characters.
You may want to replace the first four characters when the number of lines of code crosses 100, at that time you can use range of lines mentioned in example 3.
Thanks for this very useful question.
Sathiya Moorthy,
Thanks for your kind reply,
I have two more questions.
1.How to search and replace any character in a multiple lines but not in a range.For example in the previous program I submitted if I wanted to replace the word input in only 5th and the 7th line but not in 6 line. Because, if I understood right the pattern
5,7s/input/output/g
would replace the word input in all the 3 lines (5,6&7). How to ignore the 6th line here.
2. If I use the option “c” at the end how to control/escape any particular line without replacing the particular instance.
Again thanks for your reply,
Regards,
Arun
@Arun
1. range
Sorry as specified in the syntax of the substitute in the introduction part of this article, it is range and not multiple lines. So you cannot do some thing as you asked in a straight away manner. You can give only ranges in the first part of the substitute command.
So you can do some of the following work around to achieve, such as giving the lines in the range one by one as :5s/// and the :7s/// or using the c option.
2. c option
How to escape without replacing ?!
It is been explained in the Example #8.
Sathiya,
Thanks for the reply. I definitely gained some knowledge through this discussion.
How can I replace a full line or syntax after a certain match example:-
next_hop: 1.1.1.1
next_hop: 2.2.2.2
next_hop: 3.3.3.1
replace all with
next_hop: 1.2.3.4
Tks,
Patrick.
After playing around I can do it via the following, by replacing next_hop + >15 characters to next-hop 1.2.3.4
:g/^next_hop……………/s//next-hop 1.2.3.4/g
if you type the following it will work
:g/.\..\..\../s//1.2.3.4/
this will change all three lines to next_hop: 1.2.3.4
of course since he wants them all the same this would also work
:g/^.*$/s//next_hop: 1.2.3.4/
this would also work
:g/:.*$/s//: 1.2.3.4/
I hope that helped.
-Chris