Linux_logo_errorbits.comvim Text Editor

vim is a very powerful and versatile modal text editor, it is used by novice, advanced and very experienced users also, in their day to day work or tasks. I might look difficult to understand the way this text editor works, but trust me, it’s not hard at all.

From the start you must know that vim has four modes:

insert mode – in which you type text as in any other editor (to enter in this mode press “i” key);

normal mode – provides ways to manipulate and navigate text (to enter in this mode press “ESC” key);

visual mode – allow to select text and to decide what to do with it (to enter in this mode press “v” key);

ex mode

At the top of the editor is a status bar which displays the mode that is active.

In normal mode you have to use h, j, k and l keys to move de cursor.

w – moved to the start of the next word

e – moves to the end of the word

b – moves at the beginning of the word

In vim movement keys can be combined with numbers:

5e – the same as pressing e key 5 times

Text can be inserted multiple times:

55i# ESC – inserts # for 55 times and returns to “normal mode”

To find a character or string and to move to next/previous one you must use f key:

f key can be combined with a number, for example 5fy will find the 5th occurrence of “y” character.

In text that is structured with parentheses ( ), [ ], { }, the symbol % (SHIFT + 5) can be used to match them.

To search for a string press / or ? and then the text you are looking for, use n and N to navigate to next/previous occurrence of it.

To find the next occurrence of the word under the cursor press *(SHIFT+8) and to find the previous one press #(SHIFT+3).

To get to the beginning of a line press 0 (zero) and to get to the end of it press $ (SHIFT+4).

To get to the beginning of a file press gg, to get to the end of it press G. H key moves the cursor to the top of the screen, M key to the middle of the screen and L to the bottom of the screen. ^U command can be used to move up half a screen, ^D to move down half a screen, ^F is for page down and ^B for page up.

To go directly to a line number press the number of it and G.

To insert text on a new line press o or O (this is not zero), editor will enter in “insert mode”, after you finish, press ESC to get back to “normal mode”.

The keys x deletes the character under the cursor.

The d key is used to delete text and it can also be combined with other keys for example d5e deletes the first 5 words at the right of the cursor. d key also copies the content and can be pasted to other location with p key.

To replace only one character without entering in “insert mode”, move the cursor under the character that must be replaced, press r key and then the character that you wish to replace the previous one.

S key is used to delete all the content on the current line and start to insert new text.

To repeat the previous command you have just to press . key.

Other very useful commands in vim are:

:q = quits (if no alteration has been made to the file)

:q! = quits without saving

:w = saves the file

:update = saves the file only it has been modified

:saveas ~path/to/folder/ = saves the file to your chosen location

These commands can be also combined, for example :wq saves the file then quits.

ZZ and 😡 = acts like :wq, saves and the quits

u = undo

CTRL+r = redo

:help = help about commands


Useful links:

Linux Beginner’s Guide Part 3

How to add a user to sudoers