Q. I’ve just installed CentOS Linux server and started to use vi text editor to make changes to config files. How do I perform search and replace operation using vi / vim text editor?
A. vi (vim) is not difficult to learn, the vi editor is well known and used for both writing code and editing config files.
VI search and replace command format
Simple format is as follows:
%s/old-string/new-string/
VI search and replace command examples
Let us say you would like to find a word called “foo” and replace with “bar”.
First hit [Esc] key
Type : (colon) followed by %s/foo/bar/ and hit [Enter] key.
:%s/foo/bar/
Above command will replace first occurrence of word foo with bar on all lines. The % is shorthand for all lines.
To replace all occurrences of word foo with bar on all lines, use the g option (which indicates all occurrences on a line).
:%s/foo/bar/g
Note that the g can be replaced with a number 1,2,…N to change only the n’th occurrence on each line.
Use find and replace on line ranges (match by line numbers)
You can also make changes on range of lines i.e. replace first occurrence of foo with bar on lines 5 through 20 only, enter:
:5,20s/foo/bar/
Following command will replace first occurrence of foo with bar starting at the current line for the next 100 lines:
:.,+100s/foo/bar/
Match by words
Finally, you can match by words i.e. replace first occurrence of foo with bar starting at at the next line containing a word “test”:
:/test/s/foo/bar/g
As usual you can specify ranges:
:/test/,/guest/s/foo/bar/g
Please note that all search/replace commands should be start with the [ESC]: keystroke combination only.
🐧 19 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Hi,
I’m fairly new to vi and I’ve recently been making bulk changes on config files in vi using the following example;
:1,$s/sometexttoreplace/newtext
The $s means to the end of the file. I also found you need to escape characters like . with \.
However, I didn’t realise you could use the /g option to limit change to one line – that’s really helped me a lot!
You could also use regular expression. Suppose you have the httpd.conf
in this way:
simply do:
vim /etc/httpd/conf/httpd.conf
inside vim editor en command mode write:
How to apply this on multiple files?
How can I find and replace 2 different words from one paragraph using one vi command..?
thanx for the good article, as I have learned vim I have returned here a couple of times for different search methods.
I WANT TO KNOW THE SEARCH OPTION LIKE IN WINDOWS THAT SOME WORD WE DON’T CHANGE AND SOME WE CHANGE,IS THERE ANY OTHER METHOD RATHER THIS
:5,20S/FOO/BAR
THIS IS THE METHOSD TO CHANGE A LIMITED WORDS BUT CAN I DO ;LIKE
:5,20,25,35S/FOO/BAR
I WANT TO KNOW THE SEARCH OPTION LIKE IN WINDOWS THAT SOME WORD WE DON’T CHANGE AND SOME WE CHANGE,IS THERE ANY OTHER METHOD RATHER THIS
The article is really helpful for the people who want to learn REPLACE command from vi editor.
Thanks for this, I’m beginning to sink my teeth into Linux and this is another one of those commands that has saved me half an hour’s editting in my first few attempts. I will definately spread the “GOSPEL”
I want to add word ‘cp ‘ in each line of my file test.001 which has around 400 lines. how can i do this thru vi editir? In the same way, how can a add ward at the end of each line ?
Your help is highly appreciated. I can do this thru cat and awk commands. But I want to know haw can I do this in vi editor.
Thanks
I know this is old, but I hate to see unanswered questions.
For the beginning:
:%s/^/cp /
For the end:
:%s/$/ BlahBlahBlah/
For the beginning and end:
:%s/^\(.*\)$/BeginningWord \1 EndWord/
Thanks so much. Vim is great!
i want o find a pattern and replace it
for ex:- line 1 1234,9876540
line 2 56789000,987654012
In above example i want to replace 1234, and 56789000, with
So the output should be
line 1 9876540
line 2 987654012
Basically i want to find the pattern that stars with and ends with , and replace it with
Please help
i want o find a pattern and replace it
for ex:- line 1 pnonenumber1234,9876540pnonenumberq
line 2 pnonenumber56789000,987654012
In above example i want to replace pnonenumber1234, and pnonenumber56789000, with pnonenumber.
So the output should be
line 1 pnonenumber9876540pnonenumberq
line 2 pnonenumber987654012pnonenumberq
Basically i want to find the pattern that stars with pnonenumber and ends with , and replace it with
Please help
hello sir,
I have shared server, all maximum php file is infected with the following line of code
I want to replace following all lines with single word or blnk., Please tell me if there is an any linux script to remove following lines
I would like to replace the last occurence of a pattern in all lines (or even just 1 line might help). In lines below, I want to change only the last occurence of foo to bar.
change fooaaaaafooaaaabbbfooxxx to fooaaaaafooaaaabbbbarxxx
change aaafoobbbbfoocccfoozz to aaafoobbbbfoocccbarzz
change fooaaaafoobbbbfoo to fooaaaafoobbbbbar
change aaafoobbbfoocccfoo to aaafoobbbfoocccbar
Thanks.
:1,$s/sometexttoreplace/newtext
example:
:1, $s/a2b/a3bc/
works fine to replace the first occurance in each line of entire file, i want to replce second occurance and/or nth occurance – please help me with command. thanks in advance for time &help.
Thanks,
-Jagan
Good afternoon,
In a VI ex command how many strings can there be?
for example, if i use this set of strings, how many strings can i have in one command?
:%s/\(.*\)\/\(.*\)\/.*\/\(.*\)/\1 \2 \3 \4
if i just want replace only first 10 occurance of a word in a text file then what will be the vim command.