How to use vi editor in Linux/Unix?

How to use vi editor in Linux/Unix?
Assignment Tutorials

How to use vi editor in Linux/Unix?

What is a vi editor?

“vi” editor stands for “visual editor” it is pronounced as vee-eye editor. It is the most commonly used text editor on Linux/Unix-based systems. There are so many text editors available for Linux systems ranging from the tiny one “nano” to the massive one “emacs” editor.

But the vi editor has the following advantages over the other Linux/Unix based text editors making it so much popular:

  • vi editor is available globally on almost every Linux distribution in the world.
  • vi editor can be used in both modes CLI (command line interface) as well as GUI (graphical user interface).

But in reality, most of the Linux distributions do not have the original vi editor rather they have an improved version of vi editors called “vim” which stands for (vi improved). vim editor works similar to vi editors but has some additional features which are not there in vi editors.

How to get started with vi editor?

The first step is to check if the vi editor is installed on the local machine or not. To check this run one of the following commands on the command line of your Linux system.

:~$ vim or vi

# or 

:~$ vim --version or vi --version

If the vi editor already exists on your system then the first command will open a blank VIM document on the terminal window and the last command will give a long detailed output containing information regarding the installation of the vi/vim editor but you can just check the first line of the output. It will show the current version of the vi/vim editor on your local machine.

If the vi/vim editor is not there on your system , you can install it using the following command:

:~$  sudo apt-get install vim

Modes of operations on vi/vim editor

There are mainly three modes of operations on vi/vim editor:

1. Command Mode

Initially, when we open any file on vi/vim editor by default it opens in command mode. This mode is commonly used to type commands such as to move around the opened document, manipulate the text, and access the other two modes. In order to return to this mode, we need to press the “Esc” key on the keyboard.

  • For moving we can use the keyboard arrow keys or **[h, j, k, l, ^, $, w, b].
    • j (Downward), k (Upward), h (Left), l (Right)
    • H (Top of the screen)
    • M (Middle of the screen)
    • L (Bottom of the screen)
    • ^ (Beginning of line)
    • $ (End of the line)
    • w (One word forward)
    • b (One word back)
  • For text manipulation we can use [x, d, y, p] where d -> delete/cut, y -> yank/copy, and p -> paste.
    • Press ‘dd’ or ‘Ndd’ //To delete (go to the line to be deleted and press ‘dd’ or ‘Ndd’); where N is the number of lines to be deleted.
    • Press ‘x’ or ‘delete’ key //To delete a character (go to the character to be deleted and press ‘x’ or ‘delete’ key).
    • Press ‘yy’ or ‘Nyy’ //To copy (go to the line to be copied and press ‘yy’ or ‘Nyy’); where N is the number of lines to be copied.
    • Press ‘p’ //To paste the copied text (go to the line below which the copied text is to be pasted).

Also Read Database Architecture in DBMS

2. Insert Mode

Insert mode is used to add text to the document opened on the vi/vim editor.

One can switch from the command mode to the insert mode by pressing the following:

  • i -> Enters the insert mode right before the cursor
  • I -> Enters the insert mode at the beginning of the line
  • a -> Enters the insert mode right after the cursor
  • A -> Enters the insert mode at the end of the line
  • o -> Enters the insert mode on a blank line after the cursor
  • O -> Enters the insert mode on a blank line before the cursor
3. Ex Mode

Ex mode of vi/vim editor is used to view or change the settings and to run the file-related commands like opening, saving, closing, aborting changes to the file on vi/vim editor.

One can switch from the command mode to the ex mode by pressing the “:” colon character on the keyboard.

#write/save the changes to the current file

:w

#forcefully write/save the changes to the current file

:w!

#close/quit the current file if the file is not modified
#otherwise a warning is printed on the terminal window

:q

#forcefully close/quit the current file without saving the modifications to it
#No warning is printed on the terminal window

:q!

#save & close the current file

:wq

Do you want to know more about the vi/vim editor and remember all the vi/vim commands?
Get this Cheat Sheet for your future reference!

Leave your thought here

Your email address will not be published. Required fields are marked *

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
  • Attributes
  • Custom attributes
  • Custom fields
Click outside to hide the compare bar
Compare
Wishlist 0
Open wishlist page Continue shopping