Last updated on January 27th, 2022 at 06:11 pm.
Vim Tutorial for beginners – Learn to use Vim / Vi
In this guide you will learn how to use the Vim/Vi editor. At the end of this post you will be confident enough to move from the Nano editor. This is a tutorial with the basic Vim commands that will help you navigate the editor on a daily basis. This a Vim Cheat sheet that you can come back to anytime.
Vi vs Vim
Vi is the standard editor available in Linux and Unix systems since the 70s. Vim is an improved version of the Vi editor (available since the 90s).
Vim is usually included in modern Linux and Unix as Vi.
Some Vim Modes
It is important to know the vim modes. Here are 3 important modes you should be aware of.
Normal/Command mode : You will normally enter vim in this mode. Also called the command mode. If you are in any other mode, press escape key (Esc) to get to normal mode. In the normal mode you cannot enter text into your file. This brings us to the second mode.
Insert Mode : If you need to edit your file, this is the mode you need to be in. To get to insert mode, click i . In the insert mode you can edit your file.
Command Line Mode : In the CLI mode, you enter commands into Vim. While in the normal mode, start entering commands such as : or / . Watch the video above if you feel lost.
Insert mode / Editing a file in Vim
Once you open a file in vim, to edit it, you must enter insert mode:
i – insert text. Or
A – to insert text at the end of the line.
Quitting and Saving a file
Before entering these commands, ensure your press Esc to go to Normal mode.
:q! – Force quit without saving the changes .
:wq – Save and quit .
:w – save changes made to file.
:w filenam1 – save to a file named filename1.
Shift zq : Force quit Vim without saving.
Shift zz : Save and quit.
Editing a File in Vi/Vim
While in the terminal, either of the following will open Vim.
vi filename1 vim filename1
If you want to open a new and empty Vim window, just type vi or vim .
To edit a file remember to get into insert mode using i or A .
More editing options in Vim:
First go to normal mode/command mode.
dd – Deletes a line while in normal mode.
u – Undo the last action.
Ctrl r – Redo .
x – delete a single character.
yy – Yank / Copy a line.
p – (Small p) Paste below the current line. Paste a line after you do yy or dd.
P – (Capital P) Paste above the current line.
Ndd – delete N lines eg . 4dd deletes the next 4 lines
Nu – Undo the last N actions eg 5u will undo the last 5 actions
Remember to get into normal mode while running the above commands.
Add line numbers
:set number or :set nu – show line numbers.
:set nonumber or :set nonu – Hide line numbers
:set number! Or :set nu! – Toggles online numbers off or on.
Search in Vim
/string – Search for string. Then press enter and use n and N to navigate up and down. Press i to edit.
Find and Replace
:%s/string1/string2/gc – Find string1 replace with string2 . The gc at the end means that you must confirm each instance of the replace action.
:%s/string1/string2/g – Replace without confirming.
:%s/string1/string2/ – Also replaces without asking for confirmation.
Vim Navigation within a file
gg – Jump to first line in the file.
G – Jump to last line in the file.
Ctrl f – Navigate page down.
Ctrl b – page up.
0 – Go to first character of the line.
$ – Go to last character of the line.
w – Go to start of the word.
b – Go to the start of the previous word.
e – Move to the end of the word.
:tabnew : Create a new Vim Tab
:tabedit filename : Edit a file in anew tab.
gt – Switch between tabs.
That is a quick Vim tutorial for beginners. If you have any problems following this guide, watch the Vim Tutorial for beginners on YouTube. I hope this Vim Cheat sheet will help you migrate from Nano editor.