Ubuntu

How to Install and Use Vim Editor in Linux

How to Install and Use Vim Editor in Linux

A powerful tool for the command line, vim (Vi Improved) is a text editor that is used for editing programs and config files written in Python, shell, C, Perl, C++, etc. It is great for the development environment. This article will go through installing vim, using it for editing and managing documents, and uninstalling it.

Installing vim

The first step will be to update the package database using:

sudo apt update

You will be asked for your password before the packages update, enter your password to proceed.

Next, search for vim packages using:

sudo apt search vim

You will see a long list of available packages you don’t need to install all of them. We will be installing vim, vim-tiny can be installed instead as well.

To install vim, run:

sudo apt-get update
sudo apt install vim

You will be asked to confirm the installation, enter y.

You can check if vim installed by running:

vim --version

Opening vim

To open the vim editor, run:

vim

You can see the vim version along with some helpful commands.

Creating a file

To create a new file, use:

vim {filename}

When you run the above command, the file opens in the vim editor.

When you try typing in anything in the editor, you won’t be able to. That’s because vim opens in normal mode. Vim has four modes, we will look at each.

Normal mode

When you open vim it is in normal mode. You cannot enter text in this mode, you can only view the document. To enter normal mode, press Esc.

Visual mode

This mode allows you to select and manipulate text. You can enter visual mode by pressing either one of the following keys:

v : You can select text by moving your cursor up or down, left or right.

V : You can select the entire line by moving up or down.

CTRL v : This enters visual block mode, allowing you to expand and contract the selection.

Insert mode

This lets you edit text. You can enter insert mode through the following keys:

i : enter insert mode at the current cursor position

a : enter insert mode after the current cursor position

I: enter at the beginning of the current line

A: enter at the end of the current line

Command mode

This mode is used for saving documents, searching, quitting the program, etc. You can enter this mode using :.

Here a couple of common commands:

:wq -> to save and exit the document

:help -> shows you helpful tips

Editing a document

Now that you are familiar with the modes, let’s edit our file.

Open the file using:

vim {filename}

Enter insert mode by pressing i. Enter your text.

To save and exit the editor, press the escape key, followed by :wq, and then hit enter.

The following commands will help you with editing:

Command Use
:w Save
:q Quit
y Copy
yy Copy line
p Paste
d cut
dd Cut line
:/word Search forward for a word
:?word Search backward for a word
n Traverse through the whole document to look for the searched word
x Delete character
dw Delete word from the cursor on
dd Delete line
D$ Delete end of line
L Moves the cursor one character to the right
j Moves the cursor down by one line
k Moves the cursor up by one line

You can also add text to your file when creating it. Use:

echo {your text} >> {filename}

Uninstalling vim

To uninstall vim from your system, use:

sudo apt remove vim

You will be asked for your password before proceeding.

Next, you will need to enter y to confirm the uninstallation.

This article went through how to install vim, followed by some basics on using vim and then uninstalling it.

Similar Posts