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 w3wiki.
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 and Replacing in Vim Editor

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.

Similar Reads

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

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

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

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

Contact Us