How to Use Vi and Vim Editor in Linux

How to Use Vi and Vim Editor in Linux

Vi and Vim both are most popular terminal-based text editor. Vim is an improved version of Vi and their syntaxes are identical. If you know how to use VI editor you will be able to do the same with Vim.

Installing Vi or Vim Editor on CentOS, AlmaLinux, RockyLinux, Ubuntu, Debian

On most linux distributions Vi editor comes pre-installed. However if you see any of the following errors that means you don't have Vi editor installed in your System.

 1[root@ifixlunux ~]# vi -version
 2bash: vi: command not found
 3OR
 4[root@ifixlunux ~]# vi -version
 5bash: /usr/bin/vi: No such file or directory
 6[root@ifixlunux ~]#
 7
 8[root@ifixlunux ~]# vim -version
 9bash: vim: command not found
10OR
11[root@ifixlunux ~]# vim /etc/my.conf
12bash: /usr/bin/vim: No such file or directory
13[root@ifixlunux ~]#

Installing Vi on CentOS/RHEL/AlmaLinux/RockyLinux

Login to the server as root or administrative privilege and type the following -

1yum install vim-minimal

Installing Vim on CentOS/RHEL/AlmaLinux/RockyLinux

1yum install vim-enhanced

Installing Vim on Ubuntu/Debian

To install Vim Editor on Ubuntu/Debian based systems, login to the server as root or administrative privilege and type the following -

1sudo apt-get install vim

When the installation is complete verify the version through the command vi -version and/or vim -version The output should be something similar to the following :

1[root@ifixlinux ~]# vi -version
2VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Dec 15 2020 16:43:23)
3[root@ifixlinux ~]#

Installing Vi on Ubuntu or Debian based Linux Distributions

To install Vi on Ubuntu or any other Debian based Linux Distribution login to the server as root or administrative privilege and type the following :

1sudo apt update
2sudo apt install vim-tiny

Installing Vim on Ubuntu or Debian based Linux Distributions

1sudo apt update
2sudo apt install vim

When the installation is complete verify the version through the command vi -version and/or vim -version

How to Use Vi and Vim Editor in Linux

Basic Syntax:

1vi filename.ext
  • The VI editor has two modes: Command and Insert. When you first open a file with Vi, you are in Command mode.
  • Command mode means you can use keyboard keys to navigate, copy the file contents but you won't be able to write anything while you are in command mode.
  • To enter Insert mode, press i on they keyboard. If you see -- INSERT -- at the bottom of the vi editor, you successfully switched to the insert mode. In Insert mode you can enter text, use the Enter key to go to a new line, use the arrow keys to navigate.
  • When you are done editing in insert mode press Esc key once to return to command mode again.
  • You can then type :wq from the keyboard and press Enter to save your edit and quit from vi editor.

Here's a video demonstration of editing file in Linux SSH using Vi editor Editing file in Linux SSH using VI editor

Vi Editor Cheat Sheet

The following Cheat Sheet will help you to master the VI editor and you will be able to edit files in Linux with less time and effort.

Vi Editor Basic Commands

Syntax Description
vi file_name Open the file with vi editor
i Switch to Insert mode
Esc Switch to command mode
:w Save and continue editing
:wq Save and exit
:q! Exit without saving

If you are absolute beginner the above commands are enough to do basic file editing but if you want to take it to the next level continue reading. You will be amazed to see how powerful and feature rich the Vi editor is!

Delete Multiple Lines in Vi or Vim

Note: Please remember the following commands will work only when you are at COMMAND mode of your Vi editor. Press ESC on your keyboard which will take you to the command mode if you aren't there already.

Syntax Description
dd Delete the line where your cursor is
2dd Delete 2 lines
3dd Delete 3 lines

Dealing With Words in Vi or Vim

Deleting multiple lines in Vi is pretty easy right? Let's see how we can do the same with words :)

Syntax Description
dw Delete the word where your cursor is
2dw Delete 2 words
3dd Delete 3 words
cw Change word
x Delete the character where your cursor is
r Replace character
R Overwrite characters from cursor onward
~ Make the selected character uppercase or lowercase

Vi Editor Undo Changes

To undo a change in Vi editor use the following commands when you are at the COMMAND mode.

Syntax Description
u Undo last change
U Undo all changes to the selected line

Search For Specific Text in Vi

To search for a specific text or string or pattern in Vi, go to the command mode first by pressing ESC on your keyboard and then press slash key / followed by your search term. e.g. If you want to search for the text tutorial within your file, type the following and press ENTER

1/tutorial

Go to a Specific Line in Vi or Vim

You can go to a specific line on your file using any of the following commands. Let's assume you have opened the file on your Vi editor and you are currently at the command mode. Type the following:

Method 1:

1gg12

The gg syntax followed by the line number will take you to that specific line. e.g. gg12 will take you to the line number 12.

Method 2:

1:12

You can also use : followed by the line number to jump to a specific line on your file. e.g. :12 will take you to the 12th line.

Find and Replace in Vi or Vim

We can use :s command in Vi or Vim to find and replace a specific text. The syntax is:

1:s/foo/bar/g

The command above will replace all occurrences of your searched text foo with the text bar. Here foo is the text that we want to replace with the text bar

Conclusion

Vi editor is one of the most popular text editors in Linux. With all the built-in features it allows you to edit your files much quicker than any other editor.