vi Find And Replace Text Command

How do I find and replace (substitute) test using vi or vim text editor under UNIX / Linux / BSD or Apple OS X operating systems?

Both vi and vim text editor comes with substitute command for finding and replacing text.

ADVERTISEMENTS

Syntax

The syntax is as follows:
:%s/WORD-To-Find-HERE/Replace-Word-Here/g
OR
:%s/FindMe/ReplaceME/g

Examples

The substitute command can be used as per your requirements.

Task: VI / Vim Basic Find and Replace

To find each occurrence of ‘UNIX’, and replace it with ‘Linux’, enter (press ESC, type : and following command):
:%s/UNIX/Linux/g

Task: Find and Replace with Confirmation

Find a word called ‘UNIX’ and replace with ‘Linux’, but ask for confirmation first, enter:
:%s/UNIX/Linux/gc

Task: Find and Replace Whole Word Only

Find whole words exactly matching ‘UNIX’ to ‘Linux’; and ask for confirmation too:
:%s/\<UNIX\>/Linux/gc

Task: Case Insensitive Find and Replace

Find ‘UNIX’ (match UNIX, unix, UnIx, Unix and so on) and replace with ‘Linux’:
:%s/unix/Linux/gi
Same command with confirmation:
:%s/unix/Linux/gic

Task: Case sensitive Find and Replace

Find each ‘UNIX’ and replace with ‘bar’:
:%s/UNIX/bar/gI
Same command with confirmation:
:%s/UNIX/bar/gIc

How Do I Replace In the Current Line Only?

Find ‘UNIX’ and replace with ‘Linux’ in the current line only (note % is removed from substitute command):
:s/UNIX/Linux/g
NOTE: You need to prefix % the substitute command to make changes on all lines:
:%s/UNIX/Linux/g

How Do I Replace All Lines Between line 100 and line 250?

:{START-n},{END-n}s/word1/word2/g
Find ‘UNIX’ and replace with ‘Linux’ all lines between line 100 and line 250, enter:
:100,200s/UNIX/Linux/g
OR
:100,200s/UNIX/Linux/gc

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
37 comments… add one
  • nidirondel Nov 17, 2010 @ 11:49

    Thanks for the tips, I didn’t knew the “confirmation”.

    The section “Find and Replace Whole Word Only” seems incorrect
    Find whole words exactly matching ‘UNIX’ to ‘Linux’; and ask for confirmation too:
    :%s/\/Linux/gc ?????

  • 🐧 nixCraft Nov 17, 2010 @ 14:20

    Thanks for the heads up!

  • toto Feb 25, 2011 @ 6:23

    hi,
    good tutor. thank you

    regards,

    toto

  • Manvenson Jul 17, 2012 @ 7:37

    Thanks for the tutor.
    It is very helpful.

  • kArThk Oct 23, 2012 @ 7:51

    That was really awesome.

    Thanks a lot.

  • riash Nov 24, 2012 @ 18:19

    Thanks

  • yo yo Mar 6, 2013 @ 4:34

    Nice one dude..thanks

  • Sanchit Apr 22, 2013 @ 15:26

    Nice article. Thanks for sharing your knowlodeg

  • ritesh May 2, 2013 @ 4:44

    very helpful

  • dhawal May 7, 2013 @ 6:39

    thanks for the help dude

  • saman May 18, 2013 @ 5:52

    how i find / and replace it.

    thank.

  • ashok sharma May 27, 2013 @ 3:23

    great guidens

  • Madhav Jun 14, 2013 @ 11:17

    please let me know how to replace W* with abc

  • Pibomb Jul 5, 2013 @ 2:41

    Thanks a lot, but does it mean :100,250s/UNIX/Linux/g instead of :100,200s/UNIX/Linux/g for lines 100-250?

  • vivek tudi Jul 24, 2013 @ 9:40

    its good documentation

  • vivek tudi Jul 24, 2013 @ 9:41

    it is easy to understand

  • rajesh Aug 5, 2013 @ 8:49

    Thanks for sharing.. Its good and quick

  • ibraheien Aug 20, 2013 @ 19:54

    Thansk ,it is help me very much …
    but how to search & replace if the keyword contain / char ?
    for example (life example)
    I try to edit xml file and i want to change every with ?
    thanks ….

  • Amit Nov 13, 2013 @ 15:31

    Hi All,

    This is really nice stuff.
    But I want to replace all occurrence of ‘./’ in my file in vi editor with /application/tf3.
    first_string= ./
    new_string= /application/tf3
    can anyone please help

    • Ravindra Naik May 1, 2015 @ 4:10

      Try this
      :%s#/#/application/tf3#g

  • sumit singh Dec 2, 2013 @ 5:15

    Thanks for the tips, these proved very useful for me.

  • vivek Jan 13, 2014 @ 9:23

    Hi Vivek,

    How to replace C:\\Projects\\website\\upload to /home/httpd/www

    • Ravindra Naik May 1, 2015 @ 4:09

      Try this
      :%s#C:\\Projects\\website\\upload#/home/httpd/www#g

  • Niranjan K Jan 21, 2014 @ 15:09

    Thank you……always helping!!!! Nice site….!!!

  • nayeem Feb 12, 2014 @ 13:33

    i need to replace ’04/05/3013′ date with ’04/10/2013′ througout the file.
    please help and let me know the exact command.

    • tomytree22 Apr 8, 2014 @ 19:20

      You must escape character / with \
      then replace it
      04/05/3013 by 04\/05\/3013

  • Ashok Jul 28, 2014 @ 13:04

    Any one help me how to replace the string within multiple lines which choose randomly

    Suppose my file text.txt contain 100 lines , every line also have same string name “Hello”, But my requirement is I have to replace the String name “Hello” to “Hi”
    in line no ,1,10, 28, 45.
    if I am using 1,10, 28, 45s/Hello/Hi —Then all the lines from 1 to 45
    Pls Let me know how to replace the string within that specific lines only

    Thanks
    Ashok

  • Max Aug 27, 2014 @ 6:04

    If you change /home/httpd to /var/httpd in a textfile, using \ before / is handy:

    :s/var\/http/\/home/httpd/g

    (In case people are wondering why not just use var and home, I can tell you that var is represented elsewhere in the file I edited)

    Sincerely
    Max

  • vaibhav atalkar Dec 30, 2014 @ 10:39

    thank you very much ….

  • chris Jan 5, 2015 @ 9:41

    sorry, but “…/gc” is only vim-style, isn’t it?

  • Vikas mahajan Apr 3, 2015 @ 13:30

    Thanks to all this help

  • saurabh Tandon Apr 13, 2015 @ 6:16

    Nice …. It helped me..

  • madhukar Jul 9, 2015 @ 6:19

    how to replace the string if there is ‘\’ in word to find and replace word

  • rajan Jul 17, 2015 @ 5:40

    Thank u very much.it really simple and helpful

  • vaibhav Jul 28, 2015 @ 6:36

    Is there any support of special character like “*”.
    Suppose I want to replace anything that comes after my search, till “;” (semicolon).
    Example : protocol:http;
    protocol:dns;
    protocol: dhcp;
    There are many parameters the “Protocol” keyword can take.
    Appreciate your help. ( I don’t want to repeat the same replace commands for the different parameters)
    Thanks,
    Vaibhav

  • Ken Patrick Du Feb 27, 2016 @ 5:41

    Hi guys

    Can some one help me ?
    Problem: All the word that starts with s must be lowercase

  • Maharshi Mar 17, 2017 @ 7:11

    Suppose I have a text file which has list of files with absolute path.
    Now I want to replace /home/username/directory/file with ~/directory/file, How can I do that?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.