Adam. R

Getting started with Vim on Ubuntu: The beginning. Part 1

Created September 9, 2021

real use vim.jpg

What is VIM?

Vim is a text editor. You can write notes, reminders, emails in it. Though most of the users use it for editing source code files.

Vim emphasizes on mouse free interaction and placement of hand in the home row. Both of which foster interruption free eloquent typing. It has a peculiar feature of modal based editing: There are 2 major modes in Vim: command mode and insert mode. Command mode deals exclusively with edition of text: yanking, deleting and so on, whereas insert mode simply takes in input from user.

It also has a powerful macro system, composite commands and great support for extensibility through plugins. This makes Vim highly advanced, powerful and extensible text editor.


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.


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/file with Vim

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

vim file_name.txt

You can change the file ending to whatever, (.py for python, cpp for C++ and .html for HTML files.)

An editor, where writing the content of the document, will immediately open. file_name 1 vim.png It wont show the text but just type whatever you want!

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).


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.

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. View them here


If you found this usful then please comment and follow me! Also check out my website where I also post everything from here