vim Text Editor Tutorial

4 minutes read

vim 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

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

In this first script I will show you all the details and preparations that you will need to do prior creating an running your first Linux script. I will use VIM text editor (see the tutorial in the link), but you can use any editor that you might like.
To open a file in Ubuntu from the terminal, you can use the command-line text editor, such as Nano, Vim, or Emacs. Here is how you can do it:Open the Terminal: Press Ctrl+Alt+T to open a Terminal window. Navigate to the directory: Use the cd command followed b...
To enable HTTP2 in Nginx, you need to follow these steps:Make sure you have a version of Nginx that includes HTTP2 support. HTTP2 was introduced in versions 1.9.5 and later.Open your Nginx configuration file using a text editor, typically located at /etc/nginx...
The sed command, short for stream editor, is a powerful tool for performing string manipulation and text transformations in the Bash shell. It operates on a line-by-line basis and allows you to find and replace text, delete lines, insert or append text, and pe...
To merge XML files into one, you can follow these steps:Understand the XML structure: Familiarize yourself with the XML structure of the files you want to merge. Identify common elements that need to be merged or combined. Open the XML files: Use a text editor...
To open a text file in Linux without using a list, you can follow these steps:Open the Terminal: Launch the terminal application on your Linux distribution. You can typically find it in the applications menu or by searching for "Terminal". Navigate to ...