Vim: How to Find and Replace

vim find and replace

This tutorial explains how to find and replace words in Vim/Vi.

Vim provides the substitute command, similar to sed, for replacing text. Let’s go through and look at your options for finding and replacing text in this popular Linux text editor.

Vim Syntax

#from :help substitute

:[range][substitute]/[pattern/{string}/[flags][count]
For each line in [range] replace a match of {pattern} with {string}.
For the {pattern} see pattern ":help pattern". For instance if you wanted to replace all instances of Nov & Dec with Jan.
:%s/Nov\|Dec/Jan/G
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.
#Flags from :help s_flags
[g] - Global replace
[c] - Confirm each substitution
[i] - Ignore case
[n] - Report number of matches, do not substitute. Ignores [c] flag.

The syntax will take us through the next steps.

Simple Replace

The syntax we’ll be using for the first demo.

:%s/find/found/g
#If you want to replace X times the form is "\{n,m}" instead of g

Using the g flag will globally replace, not just the first instance on a line.

After running the command, all instances of “find” are replaced with “found” in our file. Note: In the event that you made a typo in your search and replace, simply press u to undo. Ctrl+r will redo.

While doing some rule editing last week one issue we ran into was replacing terms instead of whole words. A quick u and a change of search parameter:

"%s/\<20088\>/20089/gc

Fixed it right up for us and we carried on. Changing a port number through a config file, but not all instances of the search parameter.

Real Examples

How can I change the TLS option and SSL cipher on a .yml config file?

START_NETWORK_OPTIONS
-load_lib SSL
-SSL_version TLSv1.1
-SSL_ciphers AES256_SHA
-SSL_verification SSL_VERIFY_FAIL_IF_NO_PEER_CERT
-SSL_CRL_control NO_CRL_CHECK

END_NETWORK_OPTIONS

START_NETWORK_OPTIONS

-load_lib SSL
-SSL_version TLSv1.1
-SSL_ciphers AES128_SHA
-SSL_verification SSL_VERIFY_FAIL_IF_NO_PEER_CERT
-SSL_CRL_control NO_CRL_CHECK

END_NETWORK_OPTIONS

We’ll parse this file with the following commands.

:%s/\<TLSv1.1\>/TLSv1.2/gc
:%s/\<AES128-SHA\>/AES256-SHA/gc

This will walk through the file, and update any mentions of TLSv1.1 and AES128 to TLSv1.2 and AES256 respectively. So now the config file will be up to date and secure. When editing sensitive files, throw in the [c]. It saves more time in the end than it takes. It allows you to review the matches first, and press a to accept all.

Commenting Sections

And now say you want to comment out a block. Turn on line numbers with:

:set nu

And then add a comment marker to the front of the line:

:0;/END_NETWORK_OPTIONS/s/^/#/g

This takes us from the given line number to the search term “END_NETWORK_OPTIONS“. The block of code is now commented out. When it comes to working with YAML files and other quick config tools, it helps to have begin and end comments. This way, when editing with Vim, you can quickly comment out large sections of code.

Conclusion

When in doubt, usr_27.txt will take you to the help page for cleaning up your search terms for a more robust replacement scheme.

SHARE:
nv-author-image

Samuel Berry

I'm a writer, in the sense that there are words written and things needing explaining. Years of schooling, running on twelve now, taught me one thing, I like taking the complicated down to complex. So, I'm writing about Linux. One of those things that starts as complicated, and after a few years turns into complex. Until the next new thing rolls out anyways. Working in IT, I learned advocating for your product is the only way to ensure adoption. Providing user manuals, pictures, diagrams, and everything else possible to our community. That's what builds the "user-friendly" experience. Not only design, but inclusion. Talk to me about Linux, I'm here to learn by teaching.

Leave a Reply

Your email address will not be published. Required fields are marked *