# How to Use Vim Search and Replace in Linux

> The vim search and replace functionality in Linux consists of a series of very powerful tools - but also simple ones to get started easily.

- Author: [Bhagwad Park](https://www.namehero.com/blog/author/bhagwad-park/)
- Published: 2025-06-25
- Updated: 2025-06-25
- Category: [Linux Command Line](https://www.namehero.com/blog/category/linux-command-line/), [VPS Hosting](https://www.namehero.com/blog/category/vps-hosting/)
- Web page: https://www.namehero.com/blog/how-to-use-vim-search-and-replace-in-linux/

In all my years as a Linux admin, vim is the tool I’ve used most often. It’s the default text editor in Linux systems, and you’ll usually be working with file contents, from config files to log files, scripts, and more. If you work with multiple servers, then many of the config files will remain the same between servers, with only a few changes here and there. In our previous article, we saw how to [use sed to replace strings in files](https://www.namehero.com/blog/how-to-use-sed-to-replace-strings-in-files/), but this is a blunt instrument. You don’t get the chance to review every instance of a search replacement. With vim however, you can approve of every replacement at once, and this is very useful when working with sensitive files. Here’s a complete guide to using vim to search and replace text.

### Terminology Clarification – vi vs vim

In the examples below, I’m going to be using the “vi” command to launch the “vim” application. This is because in most modern distributions, “vi” is a symlink to “vim”, and the two mean the same thing. You can confirm this is the case by just typing “vi” into your terminal, and you should see something like this:
 [![vi is a Symlink for Vim](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/06/23121824/image-15.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/06/23121824/image-15.png)vi is a Symlink for Vim

As you can see, it says “VIM” on top. “Vi” is an older program that vim replaced, and while there may be some systems that continue using it, modern systems treat the two as the same. If you don’t get the above message when you type “vi” into your terminal, it means you’re NOT using vim, and you should type the full name of the command when launching the text editor.

With that out of the way, here’s how to search and replace files.

### Searching for Text in vim

To search for text in vim, simply open a file in the “vim” or “vi” application like this:

```
vi test_conf.conf
```

or

```
vim test_conf.conf
```

Now let’s say we want to search for the phrase “password”. You can do this by:

1. Typing forward slash (/). This will move the cursor to the bottom.
2. Type the word you want – in this case, password
3. Hit “enter”
4. Press “n” to find the next occurrence, and the next, and so on.

Here’s an example:
 [![Searching for password text](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/06/23121921/image-17.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/06/23121921/image-17.png)Searching for password text

Now press “Enter”, and it’ll take you to the first instance of the “password” text like this:
 [![Instance of password](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/06/23121921/image-18.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/06/23121921/image-18.png)Instance of password

To find the next instance, press “n” (not “enter”). Now vim will take you to the next instance:
 [![Not Matching Exact Word](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/06/23121920/image-16.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/06/23121920/image-16.png)Not Matching Exact Word

Note that vim matches the exact string. It doesn’t bother checking whether or not it’s a complete word. Because of this, it matches the following strings as well:

1. prohibit-password
2. passwords
3. etc.

Not only that, it doesn’t ignore case. So you can see in the following screenshot that it *doesn’t* match “Password”, which starts with a capital “P”.
 [![Not Matching Case](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/06/23121921/image-17.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/06/23121921/image-17.png)Not Matching Case

To make vim match the case of a word, you need to use the backslash, followed by the letter name, when inputting the pattern that you want to match, like this:

```
/password\c
```

The above pattern search will match both “Password” as well as “password”.

There’s much more to searching with vim, but now we’ll move on to replacing strings.

### Replacing Text in vim

Now let’s say I want to replace all instances of the word “option” (small-case starting letter), with “Option” (upper-case starting letter).

The command to do this will be:

```
:%s/option/Option/g
```

Note that this command includes the colon (:), which puts vim into command mode. It should be the first thing you type – no backslash as with searching. Here’s a screenshot of what the command should look like before execution:
 [![Command to Replace Instances of option with Option](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/06/25123137/image-19.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/06/25123137/image-19.png)Command to Replace Instances of option with Option

Hitting the “Enter” key will replace all instances of “option” with “Option” in the entire file. Here’s a breakdown of how the command works:

```
:%s/old/new/g
```

%s – This is the *range* of the replacement. %s means you want it to cover all instances of the pattern in the entire file

“old” and “new” refer to the strings that you want to search and replace.

g – This instructs vim to replace all occurrences of the “old” pattern in the *current line*. Without this, vim would change only the first occurrence of the instance in each line.

### Understanding Ranges

The *range* in a vim command tells us which parts of the file should be affected by the search and replace command. In the example we just saw, we wanted our command to affect the entire file, so we started the command with %s. We can also restrict vim to the following areas.

**Current line only**

If we type “.s” instead of “%s”, vim will only make changes in the current line

**Specific line numbers**

If we want, we can instruct vim to only affect certain line numbers. For example,

```
:5s/pattern/replacement/
```

This will only replace the occurrences of the pattern on line 5. We can also use:

```
:10,20s/pattern/replacement/
```

This will only affect lines *between* 10 and 20. We don’t have a way to simultaneously affect two line numbers without also affecting everything between them.

**Pattern-Based Ranges**

If you want, you can even have vim search between two patterns of text, starting with the line containing the beginning pattern. Here’s the command to achieve that:

```
:/start_pattern/,/end_pattern/s/pattern/replacement/g
```

Note that I’ve also included the “g” at the end to indicate that I want vim to replace all occurrences of the search pattern in each line, and not just the first occurrence.

### Important Flags for Vim Search and Replace

The other aspect of search and replace is the flags, which affect how the tool functions. Here are some of the important ones:

g – We’ve already encountered this. It instructs vim to use the entire file as its context

c – This will ask you to confirm every replacement. You can press y for yes, n for no, a for all, q to quit, and l for last.

i – This ignores case in the search pattern

n – This is a test run, where instead of replacing the found text, vim instead reports the number of matches. It’s quite useful for counting the number of occurrences of a text in a search range.

## Using Regex with Vim

One of the most powerful features of the vim search and replace functionality is its ability to accept Regex expressions. These are powerful pattern-matching tools that go far beyond wildcards, if you know how to use them properly. To use a regular expression, simply use it as part of the search/replace string like this:

```
:%s/old_regex/new_text/g
```

Regular expressions can get very complicated, and it would take far too long to get into the details of all the ways you can pattern match with Regex. If you go down this route, always ensure that you test your regex on a small part of the file in case something goes wrong!

## Undoing the Results of a Search/Replace in Vim

Sometimes, things can go horribly wrong. If this happens, not to worry. Vim has an easy way to undo the results of any search/replace action. Just press “u” (not uppercase), and the entire replace function that just happened will be undone. It doesn’t matter if you made just a single replacement or hundreds of replacements – you can just press “u” and everything will go back to the way it was before.

So if worse comes to worst, you always have a last get-out-of-jail-free card!

## Conclusion

This quick introduction to the search and replace functionality in vim will help you get started, but we’ve only just scratched the surface. Even if we ignore all the complexities of regular expressions, there’s a lot more depth to it than shown here. It’s best to use the simpler functionality and then use references for more complex cases. There’s no point in trying to learn everything and every edge case all at once.

## Related posts

- [How To Search and Replace Using The Vim Text Editor](https://www.namehero.com/blog/how-to-search-and-replace-using-the-vim-text-editor/)
- [How To Search Using The Vim or Vi Text Editor](https://www.namehero.com/blog/how-to-search-using-the-vim-or-vi-text-editor/)
- [Curl Headers, Cookies, and Authentication](https://www.namehero.com/blog/curl-headers-cookies-and-authentication/)
