Vim is a widely-used text editor in the Linux environment, renowned for its efficiency and powerful command-line capabilities. Mastering Vim's search and replace functions can significantly enhance your productivity when editing files directly from the terminal.

In this guide, we'll explore how to find and replace text in Vim using its command-line mode, complete with detailed instructions and examples.

To begin, open the text file you wish to edit in Vim by entering the following command in your terminal:

vim test.txt

Vim operates using different modes, with the two primary ones being Command mode and Insert mode. Command mode allows you to execute commands and navigate through the file, while Insert mode enables you to modify the file's content.

By default, Vim starts in Command mode when a file is opened. To switch to Insert mode and begin editing, press i. This allows you to insert or modify text within the file.

In Command mode, you can enter various Vim commands, which appear at the bottom of the terminal. This area serves as Vim's command-line interface, where you control your editing session with precision.

Step 1: To search for a specific string within the file, type the forward slash / followed by the text you want to find.

For example, to search for the word "dog", you would enter:

/dog

After executing the search command, Vim highlights the next occurrence of the string from the cursor's current position. To navigate to subsequent occurrences of the search term, press n. Once the search reaches the end of the file, Vim continues from the beginning and displays the message "search hit BOTTOM, continuing at TOP".

Step 2: To search for a string that includes a special character (such as a plus sign + or a space), precede the special character with a backslash \ in your search command.

For example, to search for "C++", you would enter:

/C\+

Step 3: To find and replace the first occurrence of a string on a specific line, place the cursor on that line and use the following command format:

:s/old_text/new_text

For example, to replace the first instance of "dog" with "tiger" on the current line, you would enter:

:s/dog/tiger

After executing this command, Vim replaces the first occurrence of "dog" with "tiger" on that line.

Step 4: To find and replace all occurrences of a string on the current line, append /g (which stands for "global") to the command:

:s/old_text/new_text/g

For example, to replace all instances of "cat" with "dog" on the current line, you would enter:

:s/cat/dog/g

Step 5: To find and replace all occurrences of a string throughout the entire file, use the following command:

:%s/old_text/new_text/g

For example, to replace every instance of "dog" with "mouse" in the file, you would enter:

:%s/dog/mouse/g

Vim will search the entire file and replace all occurrences of "dog" with "mouse". If the string you're searching for includes special characters or spaces, remember to precede those characters with a backslash \ to ensure Vim interprets them correctly.


Mastering these search and replace commands in Vim can greatly enhance your text editing efficiency within the terminal.