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

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.
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.

#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:


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?

-load_lib SSL
-SSL_version TLSv1.1
-SSL_ciphers AES256_SHA



-load_lib SSL
-SSL_version TLSv1.1
-SSL_ciphers AES128_SHA


We’ll parse this file with the following commands.


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:


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.


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.


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 *