Vim is a lightweight and powerful text editor popular on Linux systems. I use Vim mainly for fast editing of small files, such as a configuration file or a script file but it can of course be used for any text editing. This is a beginners guide just scratching the surface of Vim.
Start Vim and create the file newfile.txt, or edit it if it already exists.
$ vim newfile.txt
newfile.txt +3 will open the file and put the cursor to line 3 directly.
The insert mode and normal mode
One thing making Vim a bit different from many other editors is the modes. I will only mention normal mode and insert mode in this article but there are more modes.
When you start Vim you will enter normal mode automatically.
To enter text you need to be in insert mode. Simply press the
i key or
insert button to enter insert mode. Now enter some lines in the editor.
Tips: Did you forget if you are in normal mode or insert mode? Check for the
==INSERT== message at the left bottom of your window.
To enter the normal mode again, press the
Navigate the text in normal mode
You navigate your cursor with your arrow keys or alternatively with the
h- move left
j- move down
k- move up
l- move right
It is totally fine to use the arrow keys, but the
hjkl alternative is recommended for fast navigation since normally the arrow keys are located far away from the alphabetical keys and thus require more time to reach. However, using the
hjkl instead of arrows requires some time to get used to. Give it a try!
Some more navigation options:
G- Go to the last line of the document
gg- Go to the beginning of the document
3G- Go to line 3
20l- Go to column 20 use
20l simply means repeat the l-command 20 times.
Search for a Word
To search for the word "Friend" from the normal mode, enter
/Friend and press enter. It the word Friend is present in the document, the cursor will go to its first location. If you are familiar with the
less program, you will be able to searach a document in Vim in a similar manner.
Note: The search is case sensitive by default.
Save and Exit Vim
To save the current file and exit you enter
:wq from normal mode. (If you are not in normal mode enter
esc and then
More Learning Resources
- In the terminal enter
vimtutorfor a beginner friendly introduction to Vim, including much more than this article.
- Vim adventure is a fun game for learning Vim commands. The first level is free, give it a try! If you buy the full game you will become an expert Vim user.
Top comments (1)