Tutorials > How to install and use Vim Text Editor on Linux Ubuntu

How to install and use Vim Text Editor on Linux Ubuntu

Published on: 18 June 2020

Development Ubuntu

Introduction

Using a text editor to create or edit your own files is a such an obvious task that every operating system has always a preinstalled text editor. You may have had to edit files on your Cloud Server, regardless of whether they were configuration, html files from your websites or, more commonly, simple text files.

Among the various text editors in the world of Linux, Vim (or Vi IMproved) stands out for its versatility and for the functions it offers. In fact, Vim is able to speed up code writing, providing some shortcuts to perform all the operations of modification, deletion or replacement of the text. 

Vim Text Editor also allows you to install different plugins through which transforming this simple text editor into a real IDE for programming in different languages.

Initially, the user, approaching this tool for the first time, may feel confused by all the available commands. However, once you memorize the main editing commands, you won’t do without this tool! ! 

In this tutorial you will see how to install Vim Text Editor on your Linux Ubuntu 18.04 server and you will learn how to use its main commands to create and edit text files with their different uses.

To get started, connect to your server via an SSH connection. If you haven’t done so yet, following our guide is recommended to to  connect securely with the SSH protocol. In case of a local server, go to the next step and open the terminal of your server.

Installing Vim Text Editor

Install Vim by querying the official Ubuntu repositories directly. Then, type:

 sudo apt-get update 
 sudo apt-get install vim -y

Creating a document with Vim

To create a document and start editing it, just run the vim command followed by the file name:

 vim my_file 

When executing the command above, the “my_file” file will be generated (the extension hasn’t been indicated but can optionally be defined, i for example, by typing: vim my_file.txt ).

An editor, where writing the content of the document, will immediately open.

Edit files with VIM

Once you open the document, start writing the content. Once you have finished writing, press "Esc" and then type ": wq". Press "Enter" and your file will be saved in the path where you started the command (the meaning of ": wq" will be explained later).

This is the easiest way to use Vim as a text editor, but this tool has much more potential to show! !

Vim Text Editor Mode

The main difference between Vim and other text editors is that this tool can be considered as a 'modal' editor: depending on how you are using Vim, the same key combination can take on different functions. There are three modes:

  • Normal
  • Insert
  • Command-line.

Exit from any mode by pressing the ESC key, through which you will enter the "command line" mode.

Normal mode

With the normal mode it is possible to edit a text or navigate through the lines. In this mode, by pressing the h , j , k , l keys, you move through the text just as if you were pressing the directional arrows on the keyboard. More precisely:

  • h: move the cursor to the left
  • j: move the cursor down
  • k: move the cursor up
  • l: move the cursor to the right

Each command can be executed by placing a number next to it. For example the 3k command will move the cursor 3 lines up.

Visual mode

In this mode Vim can be used to modify portions of text that have certain conditions. There are 3 ways to use Vim in Visual mode:

  • Character (type "v")
  • Riga (type "V")
  • Block (type "CTRL + V")

In Character mode a line can be highlighted to copy, delete or modify it as you would do with any other editor. In fact, all you need to do is move the cursor to highlight the phrase from its start up to where the cursor will be placed.

For example, if you want to move a phrase, just enter this mode, select the phrase to be moved, press the d key, place the cursor where you want to paste it and finally press p. To modify a portion of the sentence, just like before, highlight the sentence and press the c key: the highlighted sentence will be deleted and you will enter the Insert mode to edit the text.

In Line mode, instead of highlighting a portion of a sentence starting from a single character, entire document lines can be highlighted. This makes it quicker to copy portions of text (once highlighted, press y ) or delete them (once highlighted, press d ). To paste them, just press p. In addition, in this mode, the indentation can be increased or decreased: after selecting it can be indented inwards or outwards using the > and < keys .

In Block mode you can imagine to see the document divided into many columns. This makes it easier to identify whether a specific line is in the right position:This check can be very useful when writing a code that has some elements indented in different positions.

For example, verifying the correct indentation of a code block could be difficult with the naked eye, but by highlighting a reference 'column' it’s easy to verified if everything is in the right place. In the image below, the last print is not indented correctly (it should be 'moved' by 3 positions to the right).

Indentation example

Insert mode

The insert mode is the one through which Vim is used as a normal text editor, thus allowing you to add text, delete it, etc. Also, in this mode there are key combinations for inserting text in different points of the document.

Command Description
the Enters the insert mode at the point preceding where the cursor is placed.
TO Enters the text entry mode at the point where the cursor is placed.
Shift + A Enters the text entry mode at the end of the line where the cursor is placed.
Shift + I Enters the text entry mode at the beginning of the line where the cursor is placed
OR Creates a new line below the one where the cursor is placed and enter the text entry mode at the beginning of the new line.
Shift + O Creates a new line above the one where the cursor is placed and enter the text insertion mode at the beginning of the new line.

To exit the entry mode, simply press the "ESC" key.

To enter this mode, just type the command: i .

Command line mode

From this mode more complex commands, such as saving the changes made to the document or even closing Vim, can be executed.

These commands must be preceded by : (colon). Again, macros (combinations of commands) to be executed in series can be created.

For example, if you want to save and close a document, use the command  : wq where:

  • "W" (write) represents the request to write the changes made (save)
  • "Q" (quit) is used to close the document.

These commands can be combined to use the potential of this editor.

Below there is the list of commands provided by Vim Text Editor.

Command Description
the Enters the insertion mode at the point preceding where the cursor is placed.
THE Enters the insertion mode at the beginning of the line where the cursor is placed.
to Enters the insertion mode at the next point where the cursor is placed .
TO Enters the insertion mode at the end of the line where the cursor is placed.
x, dl Clears the character highlighted by the cursor.
X Deletes the character on the left of the cursor.
w Goes to the next word by placing the cursor on the first letter.
b Moves to the previous word by placing the cursor on the first letter.
0 Moves the cursor to the first character of the line where it is located.
$ Moves the cursor to the last character of the line where it is located.
r Enables editing of a character. Once the cursor is placed on the character, type the command followed by the character that will replace the one highlighted by the cursor.
R Enables continuous editing of the sentence. Once you type this command, each new character typed on the keyboard will replace the one highlighted by the cursor.
s Deletes the character highlighted by the cursor and enters insert mode.
S Deletes the line where the cursor is located and enters insert mode
ESC Exits insert mode and returns to normal mode.
u It undoes the last action performed.
Cr It reruns the last action performed
dd It deletes the entire line where the cursor is located and places the cursor at the beginning of the previous line.
dw It deletes the word where the cursor is located and positions itself on the first character of the next word.
d $ It deletes what is beyond the cursor on the same line.
cc It deletes the line where the cursor is placed and goes into insert mode.
cw By positioning the cursor on the first letter of the word, the whole word is deleted, the insertion mode is finally enabled.
cl Deletes the character where the cursor is positioned and goes into insert mode.
yy Copies the line where the cursor is placed.
yw Copies the character where the cursor is placed.
$ y Copies the word from where the cursor is placed.
p Pastes to the position after the cursor
P Pastes to the position preceding the cursor
or Creates a new row below the one where the cursor is placed
OR Creates a new line above the one where the cursor is placed
: w Saves the written text
: Q! , ZQ Stops editing the document by closing Vim without saving
: x,: wq, ZZ Saves the changes and closes Vim.
: help Opens a window containing useful tips for using Vim.
: help {argument} Opens a window containing instructions and descriptions on the requested topic
qz Allows you to record macros. Once the recording has started, it is possible to enter text and / or type commands to be execute in series. When you are done recording your Macro (combination of instructions), type the command z. To run the recorded macro, type the @z command.