2 Minutes Tutorial: How to Use Vim in Terminal
As Linux users, we are involved in text processing extensively. Configuration files in Linux need to change frequently. Vim is the solution to any text editing. If nano is installed, it is another solution but not very effective. In this article, we will cover how we can effectively use vim while working. If you are interested in learning more Linux commands, Click Here for Top 25 Linux Commands for Beginners.
Vim is pre-installed on most of the Linux-based distributions. If not installed, use below commands:
sudo apt install vim
Alternatively, you can also use below commands:
git clone https://github.com/vim/vim.git
sudo make install
How to create text file using vim in 3 steps
Step 1: To initiate the editor, just type the below command:
Step 2: type "i"[enter]. You are able see "INSERT" that means now enter content in file. Type "I am just creating text file using vim".
Step 3: Press Escape, type ":wq! test.txt" and Enter.
":wq! test.txt" is used to write, save and quit. This will help you to return to the terminal window. When you use "ls" command, you are able to see text files in the same directory.
You can use below command for open a file. If file is not available, it will create it.
Simple Commands for Start Using Vim
|:wq!||save and exit|
|:q!||to exit the file without saving|
|v||to select characters|
|dd||to cut a line|
|yy||to copy a line|
|h||moves left in terminal|
|j||moves down in terminal|
|k||moves up in terminal|
|l||moves right in terminal|
Search and replace string in text files
For search, simply use / and search string. ? is used to search string in backward of text file. Vim has a similar feature of grep for pattern recognition.
|/<string>||search string in text file|
|?<string>||search backward from end of text file|
|:%s/<string>||search string in text file|
|:%s/<string>/<replace-string>||replace string in text file|
|:%s/<string>/<replace-string>/c||replace string but ask for confirmation|
This tutorial is ideal for beginners. Still long way to go. Vim is extremely useful to process text files. With the help of this tutorial, you are able to create new files, manipulate text files, search strings and exit safely. Do practice more and more and play with vim to learn it in more depth. But still if you know these commands, you are capable of performing 80 percentage of tasks.
Subscribe us to receive more such articles updates in your email.
If you have any questions, feel free to ask in the comments section below. Nothing gives me greater joy than helping my readers!
Disclaimer: This tutorial is for educational purpose only. Individual is solely responsible for any illegal act.