Get Started with Vi Editor

Vi editor is a command line text editor. It’s a single window with text input and output only. It is the most popular and classic text editor in the Linux and it is available in all Linux Distributions.

There are lots of commands in Vi editor but in this article, you will know the basic operation of Vi editor. Basic operations include create/open, edit, delete and save. Start your terminal first to enter commands.

Create/Edit file in Vi editor:

Syntax: vi <New_filename> or <EXISTING_filename>
Example : vi myfile

This creates a new file if file does not exists, otherwise opens an existing file.

Now your Vi editor has opened. Before start editing, lets understand operation modes of Vi editor which are very important to understand first.

Operation modes

  • Command mode: By default, Vi editor opens in command mode. This mode allows us to move through a file, and to delete, copy, or paste lines or words. In this mode, whatever you type is interpreted as a command and not shown on screen.
  • Insert mode:  This mode enables you to insert text into the file. Everything that’s typed in this mode is interpreted as input and placed in the file.
  • Last Line Mode (Escape Mode): Last Line Mode is invoked by typing a colon [:], while Vi is in Command Mode. The cursor will jump to the last line of the screen and Vi will wait for a command. This mode enables you to perform tasks such as saving files, exiting etc. commands.

You can switch to the Insert mode from the command mode by pressing ‘i’ on the keyboard. To return to the command mode and save the changes you have made you need to press the Esc key.

Although, there are some other commands also to move to insert mode shown in below table.

Switch from command to insert mode

CommandAction
i

Start typing before the current character

I

Start typing at the start of the current line

a

Start typing after the current character

A

Start typing at the end of the current line

o

Start typing on a new line after the current line

O

Start typing on a new line before the current line

Now start writing text in file. I’ll explain more about editing file in the next section. You can just type some text in the file and once typing done you can press Esc to move to command mode for saving the file.

Save and Exit

Type the command from the following table according to your requirement and press enter then.  See there is colon before every command and when you press colon you will be moved to last line mode to enter command. For example – to save and exit from Vi editor type  :wq and press enter.

Command

Action

:wq

Save and quit

:w

Save

:q

Quit

:w fname

Save as fname

ZZ

Save and quit

:q!

Quit discarding changes made

:w!

Save (and write to non-writable file)

Copy and Paste Commands

Press Esc to come out from insert mode. You can run these commands to copy/paste characters and lines in command mode.

Command

Action

yy

Copies the current line.

yw

Copies the current word from the character the lowercase w cursor is on, until the end of the word.

p

Puts the copied text after the cursor.

P

Puts the yanked text before the cursor.

 

Copy/Cut past multiple lines

Sometimes it is required to the block of lines. To achieve this follow the following steps:

  1. Position the cursor where you want to begin cutting/copying.
  2. Press v to select characters (or uppercase V to select whole lines).
  3. Move the cursor to the end of what you want to cut/copy.
  4. Press d to cut (or y to copy).
  5. Move to where you would like to paste.
  6. Press P to paste before the cursor, or p to paste after.

Moving within a file

You can either use arrow keys or following keys to move within a file.

Command

Action

k

Move cursor up

j

Move cursor down

h

Move cursor left

l

Move cursor right

You can delete text using normal backspace and delete button while in insert mode. In command mode, you can use the following commands. Most commands in Vi can be prefaced by the number of times you want the action to occur. For example, 2x deletes two characters under the cursor location and dd deletes two lines the cursor is on.

Command

Action

x

Deletes the character under the cursor location

X

Deletes the character before the cursor location

dw

Deletes from the current cursor location to the next word

d^

Deletes from the current cursor position to the beginning of the line

d$

Deletes from the current cursor position to the end of the line

D

Deletes from the cursor position to the end of the current line

dd

Deletes the line the cursor is on

 

Leave a Reply