Home Blogs Vi Editor in UNIX

Vi Editor in UNIX

by Anup Maurya
7 minutes read
Vi Editor in UNIX

In this tutorial, we’ll learn about what is Vi Editor in UNIX, modes of Vi Editor and more.

In order to create a file or program we need an editor, In Unix based operating system we have Vi editor which helps to create a file, modify file and view a file.

What is Vi Editor

The vi Editor is a screen-oriented text editor originally created for the Unix operating system. The vi editor lets a user to create new files or edit existing files.

Syntax

  • vi < filename>
  • If the file doesn’t exist, a new file is created. If the file exists then the file is opened for editing
  • When vi command is given without an argument, the editor is opened for inserting data, which can be saved later.

Modes of Vi Editor

Modes of Vi Editor
Modes of Vi Editor

There are three modes of Vi Editor as follows

  • Command mode
  • Input mode
  • Last line mode

Command Mode

When given vi, the file is opened in the command mode by default.

Some of the available options are

  • r- replace one character
  • x- delete text at cursor
  • dd- delete entire line
  • 5dd – delete 5 lines
  • yy- copy a line
  • nyy – copy n lines
  • P – paste above current line
  • p – paste below current line

Input Mode

This mode allows the user to insert or modify or append text.

Options to switch from command mode to input or text mode

  • i – insert text at cursor
  • a – insert text after cursor
  • A – append text at the end

To change to command mode press <Esc>

Last Line Mode

This is invoked from the command mode.

When the user types : the cursor moves to the last line of the screen.

Options given in last line mode

  • :w – save
  • :wq – sabve and quit
  • :q! – quit without save
  • :w <filename> – saves a copy the file (save as in windows)
  • :set nu – sets line number
  • :set ai – set auto indent

Cursor Movement Commands

You must be in Command Mode to use commands that move the cursor. Each of these commands can be preceded with a Repeat Factor.

Examples:
8j will move the cursor down 8 lines
3w will move the cursor 3 words to the right.

CommandMoves the cursor
SPACE, l (el), or right arrowspace to the right
h or left arrowspace to the left
j or down arrowdown one line
k or up arrowup one line
wword to the right
bword to the left
$end of the line
(zero)beginning of the line
eend of the word to the right
beginning of previous line
)end of the sentence
(beginning of the sentence
}end of paragraph
{beginning of paragraph

Page Functions

:.=Returns line number of current line at bottom of screen
:=Returns the total number of lines at bottom of the screen
^gProvides the current line number, along with the total number of lines, in the file at the bottom of the screen.

Searching Commands

/stringSearch forward for occurrence of string in text
?string Search backward for occurrence of string in text
nMove to next occurrence of string in text
NMove to next occurrence of search string in opposite direction

Which is the default mode of vi editor?

Command mode is the default mode for vi .

Is vi editor open source?

Yes ! vi is a free and open-source text editor. Vi editor was developed in 1976 by Bill Joy, vi editor is a widely used text editor in Unix/Linux systems and is known for its efficiency and flexibility. An improved version of Vi editor was released later in 1991, popularly known as VI IMproved (VIM).

related posts

Leave a Comment