Searching and Replacing in Vim Editor
Last Updated :
06 Dec, 2023
Vim is an excellent version of the legacy Vi editor for terminals. It is a complete command line-based text editor that has been the go-to choice for IT professionals for decades. Vim is the Vi-IMproved version of the original text editor which contains many modern features. In this article, we shall explore one such feature, the functionality to search and replace in a file. We will see various methods of searching and replacing based on different criteria such as letter case, regular expressions, etc.
Pre-requisites
- A Linux system.
- Terminal with Vim editor (It comes default in all Linux distros).
- Basic understanding of editing in Vim editor.
Creating a File in Vim
Firstly, we will create a file that we will use throughout the article to perform searching and replacing. It is easy enough to create a file in Vim, you just have to run the Vim command and pass the filename as an argument.
vim <filename>
We will create a file named geek for this tutorial.
vim geek
This will open the Vim editor and will look like the following:
Vim interface
Now, to type in the content, we will type 'i' in the command mode(which is the default mode). This will allow us to write content into the geek file. The content we are using is as follows;
Howdy!
This is a document.
This is made for geeksforgeeks.
Keep geeking!
Once, you have written this content into the file, you need to save it. To do this, you need to switch back to command mode, which can be done by pressing the ESC button. Once, you are in command mode, type the following command and press Enter:
:wq!
This will save your work.
Saving work in vim
Now, we can move to searching and replacing part of the tutorial.
Searching in Vim
Now, Vim provides various methods of searching but the default approach is that it searches for the first instance of the given string, whether it be a substring of a word or a word itself, and will highlight it. The syntax of searching is:
/<search word>
First, you need to open a document in Vim and then, enter command mode. After this, vim will switch to search mode and whatever you will type after '/', vim will search its first occurrence in the document.
For example, we will open a dummy file and search geek in it. To do this, we need to type the following in command mode.
/geek
Output:
Basic search in vim
As we can see, that vim highlighted the first instance of 'geek' in the file. Now, we shall look on different ways of searching.
1) Using case insensitive searching
By default, vim uses case sensitive searching however, it provides a command to change this to case insensitive searching. The command is as follows
:set ignorecase
This, allows to change the search to case insensitive. To enter this command, you have to be in command mode and then type in the entire command given above.
For example, after changing this, if we now search for GeEk then, we will still get the same results as above.
/GeEk
Output:
Case Insensitive search
Here, vim searches for the given string without accounting for the letter case.
Also, you can also turn back the case sensitive search by entering the following command in command mode.
:set ignorecase!
2) Exact word matching
Another method of searching in vim is to search the given word as a proper string and not a substring. For example, in previous examples, we searched for geek however, we got results even though it was part of the word geeksforgeeks. This is the default setting in Vim for searching however, it can be changed by wrapping the word we search for inside the given characters
\< word >\
For example, we will search for the first string instance of is in the document. In the default mode, it should highlight the 'is' inside the 'this' word of the first line but, because of exact word matching, it will highlight the second word in first line.
/\<is\>
Output:
Vim
Exact word searching in Vim
Replacing With Searching in Vim
Now, that we have learned how to perform searches in Vim, we will learn how to replace the searched words. To replace a searched word in vim, we have the :substitute command or :s in short. The syntax is as follows:
:%s/search_word/replace_word/g
Now, let us go through each field in this syntax to understand it better:
- %s - The first part after ':' decides the scale of replace. It can either be for the entire document (%), specific line numbers (e.g 3), or a range of lines (e.g. 3,7). The 's' is for the substitute command.
- search_word - The next field followed by the '/' is the word we want to replace, you can use the default searching, regex searching or exact word searching here.
- replace_word - Next, it is takes the world we want to replace the searched word.
- g - These are extra options; here 'g' means global which will replace every occurence in all specified lines instead of the first occurence. Also, there is an option 'c' which asks for confirmation at every search.
Now, we will look at some examples.
Example 1
We shall use the same document as before and now, we will search for the word 'is' and replace it with 'was' for all occurrences in the entire document. The command to do this will be as follows:
:%s/is/was/g
Replacing a searched word in vim
Vim asks for confirmation once if the 'c' option is not given with the global option. Now, when you are prompted like this, press enter and the command will work as follows:
Output of replacing
As we can see that all the instances of 'is', even those as substrings of words were changed to 'was' as explained in the searching section. Now, to keep the changes to your file, you need to enter the command to write and save.
:wq!
Example 2:
In this example, we shall search and replace for a specific lines instead of entire document. Here, we will change the word 'is' only in line 3, to 'was', instead of entire document without the global option, which means that only the first instance of 'is' in line 3 should be changed to 'was'.
:3s/is/was
Specific search and replace
As we can see, only the first instance of 'is' in line 3 is changed. Once done, you can save and exit with the command given in example 1.
Example 3:
In this example, we will show how to search and replace in a range of lines and prompt for confirmation for every instance. We shall search for the word 'geek' and replace it with 'gfg' in line from 3 to 4 while asking for every instance. The command is as follows:
:3,4s/geek/gfg/gc
Searching in a selected lines and prompting for replace conformation
After pressing ENTER after typing in the command, you will be prompted to confirm the replacement for each occurrence of the word 'geek'. If you want to change that occurrence press 'Y', if not then, press 'N'. After you give the response, you will primpted with the confirmation for next occurrence until there are none left. Once done, you can save and exit with the command given in example 1.
Conclusion
In this article, we learned about the vim editor. We learned how searching is done in Vim, what are the default search settings and how we can change them according to our requirements. Then, we looked into replacing the searched word with a new word. We explained the :substitute command in complete detail and gave multiple examples to explain different options available with it.
Similar Reads
Mastering Search and Replace in Vi Editor
Vi Editor, a powerful text editor renowned for its efficiency and versatility, is a staple tool for Unix/Linux users. Mastering its search and replace functionalities can significantly enhance productivity and streamline text editing tasks. In this comprehensive guide, we will delve into various tec
6 min read
Undo and Redo in Vi editor
Vi is a versatile text editor that's available for Unix and Linux systems. It's packed with powerful features, one of which is the ability to undo and redo changes you make to a file. This means if you accidentally delete something or make a mistake, you can easily go back and fix it. Understanding
6 min read
How to Search in Nano Editor?
The Nano editor is a command-line text editor widely used in Unix-like operating systems, including Linux and macOS. Despite its simplicity, it offers a powerful search functionality that allows users to quickly locate and navigate to specific text within a file. Mastering the search feature in Nano
6 min read
How to Search in Vim in Linux
For many Linux users and developers, Vim is one of the most efficient and versatile text editors. Among its strong points is its search function, enabling users quickly to locate and re-arrange text within files. Here we will look at different ways and commands to search smartly in Vim on a Linux sy
5 min read
Vim Editor in Linux
If youâre new to the world of Linux, one of the first things youâll encounter is the command line interface. And if youâre going to be working in the command line, youâll inevitably come across various text editors. Among them, Vim stands out as a powerful, yet notoriously complex, text editor. In t
7 min read
Quit VI Editor Without Saving Changes in Linux
In this article, we will cover how to quit the vi editor without saving your changes in Linux. First, we look at what Linux and VI editor and why we use them, what are its features and how can we use them. LinuxLinux was developed by Linus Torvalds in 1991 as a hobby project. It is an open-source (s
4 min read
vi Editor in Linux
The default editor that comes with the Linux/UNIX operating system is called vi (visual editor). Using vi editor, we can edit an existing file or create a new file from scratch. we can also use this editor to just read a text file. The advanced version of the vi editor is the vim editor. Table of C
9 min read
How to Indent Code in Vim Editor?
Vim is a popular text editor used by many developers for writing code, creating readme files, and writing scripts. It's open-source and offers a lot of ways to customize your experience. This makes it easier and more efficient to work with. Properly formatting and indenting your code is important be
4 min read
How To Select All Content In Vi Editor Linux
Vi Editor in Linux is a powerful tool for text editing, offering various modes for efficient editing. In this article, we'll delve into how to select all content within Vi Editor, simplifying your editing tasks. Understanding this functionality can significantly enhance your productivity when workin
3 min read
Joining Two Lines in VI Editor
Vi is a powerful command-line text editor used by programmers, system administrators, and writers alike. They offer a vast array of features, including the ability to manipulate text efficiently. One common task you might encounter when working with Vi is joining two lines together. This article wil
3 min read