5 tricks for Vim Search and Replace

Top 5 Vim Search And Replace Commands

Hi, there readers! Today we will look into vim again, specifically how we can do search and replace easily in vim. In this article, we will look at the top 5 ways you can search and replace and reach that no-mouse vim goal. Remember to unmap the arrow keys!

Also read: Vim Tutorial – All You Need, To Get Started with VIM

The / and ? command

The easiest and most straightforward way to search in vim is through the / and the ? commands. If you are inside vim:

  • Hit the Esc and / one after the other
  • Then enter the word you want to search for
  • Hit the Enter key will take you to the occurrence of the search word after the cursor.
  • To move between occurrences you can press the n key and to move backwards you press the N key.

You can even search for two words that are side by side by just typing in both the words, this will show only those occurrences where the words are side by side.

If you want to turn off the highlighting, use the following command in normal mode:


The ? command is very similar to the / command but it searches the whole file backwards. In this case, the n key searches backwards and the N key searches forwards.

Also check: 10 Ways to Exit Vim Editor

The * and the # command

If you want to search for the word under the cursor simply press the * key in normal mode. The cursor will be placed to the nearest occurrence of the word under the cursor. You can now press the * key again to search for the next occurrence, or you can also use the n and the N key to cycle through the search results forward or backwards.

The # is the same as * but it searches backwards.

You can search ignoring the case by issuing the command :

:set ic

In both of the above cases you can simply locate the word and in normal mode hit ciw (change inside word) to delete the word and go into insert mode. But when we want to substitute a lot of occurrences this method is rather cumbersome, for that, let us look further.

Also read: Emacs vs. Vim: Differences Between the Top Terminal Editors for Linux

The s command (substitute)

In vim, the s command stands for substitution. To substitute a word execute the following command in normal mode:


What this command will do is substitute only the first occurrence of the search word with the given substitution in the current line. To substitute the first occurrence in every line we add a % sign which signifies the current file, so the command becomes:


But we are still not done, because if you see closely, the above command only substitutes for only the first occurrences in the line, what we need is a total substitution, for that we add in the flag g that stands for global:


Similarly, if we just want to substitute all occurrences in the current line only, then we would have executed the command with the g flag but without the % sign at the beginning:


Advanced use of the s command

What if you only want to substitute a word say from lines 4 through 7? well, we can very simply replace the % sign at the beginning of the command to specifu\y the range of the s command, then the command is:


If we want to search the case sensitivity of the search word, we can add in the i for case-insensitive and the I flag for the case-sensitive replacements. The commands in action are:


If you simply want to confirm each change then we can add in the c flag for that, so if you are unsure about the range and places of substitution simply run the substitution command with the c flag on the whole file and confirm where necessary:


Now, in the confirmation message, you have several options with each substitution. To be specific you have the seven following options:

  • y –> yes, perform the substitution
  • n –> no, don’t perform the substitution
  • a –> change all occurrences after this one
  • q –> quit without making any changes
  • l –> make this the last substitution then quit
  • ctrl+Shift+e –> to move down the screen (to look for the occurrences)
  • ctrl+Shift+y –> to move up the screen

Now, all this is fine, but you might have noticed that the search word does not only match the full word but also some instances where it is just a portion of another word. To counter this we have to wrap the search word inside a < and a >, we also have to remember to escape these characters while doing so! So the command now is:


The g command

The g command stands for the global command. The syntax for this command is as follows:


What this does is, runs the given command for the lines that match the given pattern in the whole document. Let’s say we are working on the following file:

  -- Base
import XMonad
import System.Directory
import System.IO (hClose, hPutStr, hPutStrLn)
import System.Exit (exitSuccess)
import qualified XMonad.StackSet as W

Now for some reason, we want all the imports to be deleted, we can do this using the g command:


What this does is executes the d (delete) command on all the lines that has the word import. Similarly, we can also do the inverse of this, meaning we can delete all the lines that do not have the word import:


We can do some really advanced stuff with the g command. Say, we want to delete all the empty lines in a document, for that we run the following command:


Here the \s* means zero or more whitespace characters and $ means the end of the line, so essentially \s*$ means an empty line.


So there you go, now you are armed with some of the best searches and replace commands that vim has to offer. If interested in more such awesome vim articles feel free to search here!