Vim tutorial- learn Vim the easy way
Vi or vim(vi enhanced) are text editors available in Linux distributions by default. Vim is very powerful and flexible. In fact, once you master it, vim is the only text editing tool you need to know.
Advantages of using Vim?
How to start using Vim ?
It's easy. You need to know the 3 operating modes of Vim and how to switch between them. Keystrokes behave differently in each command mode.
Moving to Start or End of Line in Vim
Press ^ to move the cursor to the start of the current line. Press `$` to move the cursor to the end of the current line.
Vim modes
In Vim, there are three modes to work with:
Command Mode.
When starting vim editor, you land in the command mode by default. This mode allows you to access other modes.
? To switch to other modes, you need to be present in the command mode first
Edit Mode
This mode allows you to make changes to the file. To enter edit mode, press 'I' while in command mode.
Note the '-- INSERT' switch at the end of the screen.
Visual mode
This mode allows you to work on a single character, a block of text, or lines of text.
Let's break it down into simple steps. Remember, use the below combinations when in command mode.
The visual mode comes into handy when you need to copy and paste or edit lines in bulk.
Extended command mode.
The extended command mode allows you to perform advanced operations like searching, setting line numbers, and highlighting text. We'll cover extended mode in the next section.
How to stay on track?
If you forget your current mode, just press ESC twice and you would be back in Command Mode.
领英推荐
Some common operations using Vim
1. How to copy and paste in Vim?
Copy-paste is known as 'yank' and 'put' in Linux terms.
To copy-paste, follow these steps:
2. How to search for text in Vim?
Any series of strings can be searched with vim using the / in command mode.
To search, use /string-to-match.
In the command mode, type :set hls. This will highlight the searches.
Let's search a few strings:
3. How to exit Vim?
That is a million dollar question! ?? But it is as easy as abc, trust me.
First, move to command mode(by pressing escape twice) and then use these flags:
Exit without saving → :q!
Exit and save → :wq!
Using hjkl for navigation
In early days of CLI, the keyboards didn't have arrow keys. Hence, navigation was done using the set of available keys, hjkl being one of them.
Although arrow keys would work totally fine, you can still experiment with hjkl keys to navigate. Some people find this this way of navigation comfortable.
Tip to remember: hang back, jump down, kick up, leap forward.
Time for action !!
Remember learning in tech is nothing without hands-on. To quickly access a Linux shell and practice what you have learned, hop on to Replit! It's free!
So, this is it ! You have mastered the basics. With this tutorial, you can easily view, edit and save your files using the Vim editor.
Wrapping up
Did you find this tutorial helpful? I would love to hear your thoughts and connect with you on any of these platforms.
Into Software Engineering most of the time | Machine Learning Engineer | Former Lecturer @United International University | Notebook Expert & Dataset Expert @Kaggle
1 年What's the exact benefit of writing a LinkedIn exclusive article according to you, Zaira? I am seeing many people start writing on LinkedIn exclusively. Till now I am a regular on freeCodeCamp and occasionally write for Dev.to and Hashnode, but I have never thought about LinkedIn as a learning platform till now.