Vim is a free and open-source, screen-based text editor program. In many Linux distros, this is the default text editor. In this blog, I’m showing you some vim commands to be more productive using vim as a beginner, because vim is a great choice among Linux users but is difficult to use as a newbie.
Install vim using apt
The procedure is as follows:
- Open the terminal application, you can also press the
Ctrl+Alt+T
keyboard shortcut. - Update package database by typing the
sudo apt update
command. - Install vim on Ubuntu Linux, type:
sudo apt install vim
. - Verify vim installation by typing the
vim --version
command.
See more on installing vim Text Editor.
Vim Text Editor Mode
There are four modes in the vim editor, that is Normal, Insert, Command and Visual modes.
Normal Mode
By default, vim starts in “normal” mode, normal mode can be accessed from other modes by pressing the Esc
key.
Insert Mode
This is the second most used mode, once in insert mode, typing and inserting characters just like a regular text editor. You can enter it by using an insert command.
Command Mode
The command mode has a wide variety of commands and can do things that the normal mode can’t do as easily.
Visual Mode
The last one is visual mode which is used to make selections of text, selecting text allows commands to apply only to the selection, such as copying, deleting, replacing, and so on.
Vim Editor Basic Commands
- Open a terminal and write vim, it will open the vim text editor, in normal mode.
- To insert text in vim press
i
, now you are in insert mode and can enter some text.
- To save your text, exit from insert mode by pressing the
Esc
, which will change the vim to normal mode.
Now save your text by entering
:w
followed by a filename. Entering colon:
will change vim mode to command mode, after executing the command `:w' and filename, vim will go to normal mode automatically.You can quit now from the vim text editor by using
:q
command.If you want to save and quit at the same time use the
:wq
command.
We have now some text in our file and give it a name, now we open the file with vim vim_guide.txt
. Our vim text editor is open the file by default in normal mode, at this point we have some more options to enter in insert mode as per our requirement.
Insert commands include:
i
for ’insert’, this immediately switches vim to insert mode at the cursor position.a
for ’append’, this moves the cursor after the current character and enters insert mode.- 'o' inserts a new line below the current line and enters insert mode on the new line.
Shift + I
moves the cursor to the beginning of the line and enters insert mode.Shift + A
moves the cursor to the end of the line and enters insert mode.Shift + O
inserts a new line above the current one and enters insert mode on the new line.
We also use the previous commands from vim history, enter colon :
after that use the up / down arrow keys to see the commands in history.
To undo changes, press
u
in normal mode.To delete a text character at the cursor’s current position, press
x
in normal mode.To move at beginning of the line at the cursor position press
0
and press$
to move at the end of the line.To delete a whole line at the cursor position, press
dd
in normal mode. We can also use digits to delete more lines e.g.2dd
will delete 2 whole lines. These lines are saved in the vim buffer if you want to paste them, pressp
on the desired location in a file.To copy a line at the cursor's position, enter
yy
and then paste them by pressingp
. We can use digits with this command to copy more lines.To move up, down, left, or right use
h
,j
,k
,l
in normal mode.To move up, down, left, or right in insert mode use arrow keys.
Some Advance Vim Commands
We can copy content (text) from another file, to do so use the command
:r
and filename will copy all content to your current file. If both files are not in the same directory use the full path of the file.To suspend vim to the background, pressing
ctrl+z
in normal mode will suspend the vim text editor and you will be shifted to your shell. To activate your vim after doing some jobs in your shell pressfg
which means foreground, now your Linux shell will open vim as it is.To use the Linux command while your text editor is also open, press
:!
followed by the Linux command.To navigate multiple files at the same time use
:e
and filename, which will open another file. Use:bp
or:bn
to switch between files and use:bd
to close the current file.To replace a word in a file at all locations in go to command mode, press
:%s/word/word/g
the first word you want to change followed by a word to insert.To split two files at a time open the first file then the second file with
:split
and the filename, which will open the file horizontally, to switch between files usingctrl+ww
.To open the file vertically open the first file and then the second file with
:vsplit
filename, which will open a file vertically and switch between open files usingctrl+ww
.To edit two files at the same use
vim -o file1 file2
will open both files horizontally.To edit two files at the same time vertically use
vim -O file1 file2
.
We can copy and paste text by pressing v
to enter in visual mode and highlighting the text with arrow keys and pressing y
to copy and then pressing p
will paste them where we want.