Skip to content

Instantly share code, notes, and snippets.

@glass-ships
Last active December 18, 2020 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glass-ships/286809c326c4e713a5d5ee87e35678d6 to your computer and use it in GitHub Desktop.
Save glass-ships/286809c326c4e713a5d5ee87e35678d6 to your computer and use it in GitHub Desktop.
---
layout: post
title: "Just testing the waters!"
---
<h2>VIM Search and replace syntax</h2>
<p>The syntax is as follows:<br />
<code>:%s/Search/Replace/CommandFlag<br />
:%s/Search-Word/Replace-Word/g<br />
:%s/Search-Word/Replace-Word/g<br />
:%s/Search-Word/Replace-Word/gc<br />
<span style="color: #999999;">## Search and replace in<span style="color: #009900;"> the current line only</span> ##</span><br />
:s/Find-Word/Replace-Word/g<br />
<span style="color: #999999;">## Change each 'Unix' to 'Linux'<span style="color: #009900;"> for all lines from line 36 to line 42</span> ##</span><br />
:36,42s/Unix/Linux/g<br />
</code></p>
<h2>VIM Find And Replace</h2>
<p>Both vi and vim provides the <kbd>:s</kbd> command for search and replace. Let us see some easy to understand examples.</p>
<h3>Examples</h3>
<p>I am going to use the following sample text file:</p>
<pre>-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
## full acess to lo and eth0 ##
-A INPUT -i lo -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
-A INPUT -i eth0 -j ACCEPT
-A OUTPUT -o eth0 -j ACCEPT
# log spoof
-A INPUT -i eth1 -s 10.0.0.0/8 -j LOG --log-prefix "IP DROP SPOOF A: "
-A INPUT -i eth1 -s 10.0.0.0/8 -j DROP
-A INPUT -i eth1 -s 172.16.0.0/12 -j LOG --log-prefix "IP DROP SPOOF B: "
-A INPUT -i eth1 -s 172.16.0.0/12 -j DROP
</pre>
<p>To find each occurrence of &#8216;eth0&#8217; in <span style="color: #009900;"><strong>the current line only</strong></span>, and replace it with &#8216;br0&#8217;, enter (first press <kbd>Esc</kbd> key and type):<br />
<code>:s/eth0/br0/g</code></p>
<p>Next try to find and replace all occurrences of &#8216;eth1&#8217; with &#8216;br1&#8217; under vim, enter:<br />
<code>:%s/eth1/br1/g</code></p>
<p>Search and replace all occurrences of &#8216;eth1&#8217; with &#8216;br1&#8217;, but ask for confirmation first on vim, enter:<br />
<code>:%s/eth1/br1/gc</code></p>
<p>To find and replace all occurrences of case insensitive &#8216;eth1&#8217; with &#8216;br1&#8217;, enter:<br />
<code>:%s/eth1/br1/gi</code><br />
The above example will find eth1, ETH1, eTh1, ETh1 and so on and replace with br1. To find and replace all occurrences of &#8216;eth1&#8217; with &#8216;br1&#8217; for lines from 3 to 7, enter:<br />
<code>:3,7s/eth1/br1/g</code></p>
<h2>Substitute syntax</h2>
<p>The full syntax is as follows:<br />
<code>:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]</code><br />
So for each line in [range] replace a match of {pattern} with {string}. A {pattern} is one or more branches. A {string} can be a literal string. When [range] and [count] are omitted, replace in the current line only. When [count] is given, replace in [count] lines, starting with the last line in [range]. When [range] is omitted start in the current line. The [count] must be a positive number.</p>
<h3>On each line, delete all occurrences of the whole word &#8220;Sysadmin&#8221;</h3>
<p>Yes, we can delete words too:<br />
<code>:%s/\<Sysadmin\>//g</code></p>
<h3>How can I delete the first occurrence of the whole word &#8220;method_&#8221; and the following 3 characters?</h3>
<p><code>:%s/\<method\>.\{3}//</code></p>
<h2>Conclusion</h2>
<p>You learned how to find and replace text with vi or vim text editor using the <kbd>%s</kbd> substitute command. Type the following command inside vim to get help about substitute topic or <a href="http://vimdoc.sourceforge.net/htmldoc/change.html#:substitute" target="_blank" rel="noopener noreferrer">online here</a>:<br />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment