Tutorial corner

Informatica,ETL,oracle,sql/plsql

vi command in unix with example

Spread the love

vi is a screen-oriented text editor originally created for the Unix operating system. In below article we will go through vi command in unix with example . We will learn different mode of vi editor and how we can use different option while using vi command to edit files in unix.

vi command in unix with Example

Modes of VI command:

There are essentially three Modes in vi,

  • Insert Mode :To insert or append in file
  • Edit Mode  :  To edit the existing file
  • Command Mode:  where one is interacting with the operating system with actions like reading and writing files.
vi command in unix with example

Vi Command in unix

Most of the commands require just a simple letter or two, but be careful to note that vi is case sensitive, and capital letters do different things from lower case letters. It is not necessary to learn all of these commands, but eventually they will become second nature. The basic operations needed to create a file are related to each of those modes, so let’s look at them separately.

INSERT Mode

For adding text to a file  ( press Esc and then below command)  . The three most common ways to enter the Insert Mode are:

Letter Action
i
Starts inserting in front of the current cursor position
I
Starts adding at the front of the line
a
Starts adding after the cursor
A
Starts adding at the end of the line
o
Starts opening a new line underneath the cursor
O
Starts opening a line above the cursor.
<Esc>
Gets out of Insert Mode

EDIT Mode

Generally for moving the cursor and deleting stuff.

In the Edit Mode, the keys do not type letters, but do other actions such as cursor movement, deletions, copying lines, etc.

Letter Simple Cursor Movement
h
Moves cursor left one space Note: the Arrow keys do work locally, but sometimes mess up over a network.
j
Moves cursor down one line
k
Moves cursor up one line
l
Moves cursor right one space
Fast Cursor Movement
w
Moves the cursor a full word at a time to the right
b
Moves the cursor back to the left a word at a time
^
Moves the cursor to the front of a line
$
Moves the cursor to the end of a line
<ctrl>f
Moves the cursor forward a full page of text at a time
<ctrl>b
Moves the cursor backward a full page of text at a time
Modifying Text
x
Deletes the character under the cursor
dd
Deletes the line where the cursor is located (type d twice!)
n dd
Delete n consecutive lines ( n is an integer)
r
Replaces the character under the cursor with the next thing typed
J
Joins current line with the one below (Capital J!)
u
Undoes the last edit operation
<ctrl> r
Redo (Undoes the last undo operation)
Cut and Paste Operations
yy
Copies or yanks a line ( 5yy yanks 5 lines)
p
Puts the yanked text on the line below the cursor (lower case p)
P
Puts the yanked text above the current line (capital P)
Note: If vi is already in the input mode, text from that or another window may be highlited using the left mouse button, and copied into place by pressing the middle mouse button.

COMMAND Mode

For interacting with the operating system.
To enter the Command Mode, a colon ” : ” must precede the actual command.

Letter Action
: r <file>
reads a file from disk into the vi editor
: w <file>
writes current file to disk
: wq
writes the file and quits vi
: q!
quits without writing (useful if you’ve messed up!)

So in above tutorial , we have gone through the vi command in unix with example. We also see various mode of Vi command in unix.

Updated: December 20, 2015 — 9:05 am

The Author

Alisha Lamba

Hello Friends , I am Alisha Lamba .I love to write article on latest technologies like Informatica , ETL , data warehouse , SQL-PL SQL
Copyright 2015 - Tutorial Corner Frontier Theme