0% found this document useful (0 votes)
9 views53 pages

VI Material

Vi is a widely-used text editor for Unix-like systems, known for its keyboard-centric interface and efficiency. Vim, an improved version of Vi, offers additional features such as syntax highlighting and mouse support. The document provides detailed instructions on using Vi commands, modes, and editing techniques, making it a comprehensive guide for users.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views53 pages

VI Material

Vi is a widely-used text editor for Unix-like systems, known for its keyboard-centric interface and efficiency. Vim, an improved version of Vi, offers additional features such as syntax highlighting and mouse support. The document provides detailed instructions on using Vi commands, modes, and editing techniques, making it a comprehensive guide for users.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

M -7

Introduction
Vi is a screen editor for Linux, Unix and other Unix-like operating
systems. Pronounced (vee-aye), vi stands for visual instrument. It is a widely-used default text
editor for Unix-based systems and is shipped with virtually all versions of Unix. It exclusively
uses the keyboard and provides a very efficient interface for editing programs and scripts.

Vim (Vi Improved) as its name suggests, is a clone of Vi and offers yet
more features than Vi. It’s free and open source, designed for use both from a command-line
interface and as a standalone application in a graphical user interface (GUI).It’s highly
configurable and comes with notable features such as syntax highlighting, mouse support,
graphical versions, visual mode, many new editing commands and a large amount of extension
plus much more.

Why should We learn Vi

Vi is a text editor originally created for the Unix operating system. Instead
of menus, vi uses combinations of keystrokes in order to accomplish commands. As such,
there is no need for the use of a mouse or a touchpad — everything is done exclusively
with the keyboard.

It is installed by default (seen as a standard system utility) and is available


on all Linux distributions since it is part of the POSIX standard as well as the Single UNIX
Specification. All other editors (including nano, emacs) are optional or additional installations.
It is a lightweight application and can work in stripped down versions of Linux. It is a console-
based text editor which works without a Graphical User Interface. This comes in handy
especially when logging into a machine remotely, which is quite common on Linux.

The vi editor has two modes:

o Command Mode: In command mode, actions are taken on the file. The vi editor starts
in command mode. Here, the typed words will act as commands in vi editor. To pass a
command, you need to be in command mode.
o Insert Mode: In insert mode, entered text will be inserted into the file. The Esc key
will take you to the command mode from insert mode.

By default, the vi editor starts in command mode. To enter text, you have to be in insert
mode, just type 'i' and you'll be in insert mode. Although, after typing i nothing will appear on
the screen but you'll be in insert mode. Now you can type anything.To exit from insert mode
press Esc key, you'll be directed to command mode.
Vi Commands

vi /home/sssit/Downloads/file.txt

Look at the above snapshot, we are creating a new file file.txt (as this file doesn't exist)
and have entered the full path for the directory Downloads.

Command mode

This is what you'll see when you'll press enter after the above command. If you'll start
typing, nothing will appear as you are in command mode. By default vi opens in
command mode.

Look at the above snapshot, it is blank as it is a new file. To start typing, you have to
move to the insert mode. At the end of the terminal window, directory name and file
name are displayed.

gvim file_name : To open file in gvim (text editor)


Insert mode
To move to the insert mode press i. Although, there are other commands also to move to
insert mode which we'll study below.

Look at the above snapshot, after pressing i we have entered into insert mode. Now we
can write anything. To move to the next line press enter.

Once you have done with your typing, press esc key to return to the command mode.

To switch from command to insert mode:

Command Action

i Start typing before the current character

I Start typing at the start of current line

a Start typing after the current character

A Start typing at the end of current line

o Start typing on a new line after the current line

O Start typing on a new line before the current line


To save and quit
You can save and quit vi editor from command mode. Before writing save or quit
command you have to press colon (:). Colon allows you to give instructions to vi.

Type : wq to save and exit the file.

Look at the above snapshot, command: wq will save and quit the vi editor. When you'll
type it in command mode, it will automatically come at bottom left corner.

If you want to quit without saving the file, use :q. This command will only work when
you have not made any changes in the file.
Look at the above snapshot, this file is modified and hence on typing :q it displays this
message at bottom left corner.The above file can be saved with the command :!q. It
discards the changes made in the file and save it.

Look at the above snapshot, we have typed :!q, it will save our file by discarding the
changes made.

exit vi table:

Commands 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)

To exit from vi, first ensure that you are in command mode. Now, type :wq and press
enter. It will save and quit vi.
i
The i command allows us to write before the current character.

Example:

Look at the above snapshot, our cursor is at e. We forgot to write word command before
word example. Hence, here we can use i command..

Look at the above snapshot, after pressing i, we wrote word command.


I
The command I allows us to write at the start of the current line.

Example:

Look at the above snapshot, we want to write word Hello at the starting of the line.

Look at the above snapshot, after pressing I, we have typed Hello at the starting of the
line.
A
The A command allows us to start typing at the end of the current line.

Example:

Look at the above snapshot, we want to move at the end of the current line.

Look at the above snapshot, after pressing A, we have written one more line
o
The o command allows us to start typing a new line after the current line.

Example:

Look at the above snapshot, we are at the middle of the line and want to start a new line.

Look at the above snapshot, after pressing o, we arrived at next line and typed there.
O
The O command allows us to start typing on a new line before the current line.

Example:

Look at the above snapshot, we are at the middle of the line and want to write before the
current line.

Look at the above snapshot, after pressing O, we have written in a new line before the
current line.
j
The j command works same as the downward arrow. You can move to the below
lines.

Example:

Look at the above snapshot, letter e is repeated twice. To correct that we need to go to
that line.

Look at the above snapshot, after pressing either j key or downward key, we moved to
that line and corrected it.
k
The k command works same as the upward arrow. You can move to the above lines.

Example:

Look at the above snapshot, our cursor is at letter e and we want to move up at the blank
line to write something.

Look at the above snapshot, after pressing either k key or upward key, we moved to the
upper line and have written over there.
h
The h command works same as the leftward arrow. You can move in leftwards direction.

Example:

Look at the above snapshot, our cursor is at the end and we want to move to the starting
of the second line.

Look at the above snapshot, after pressing either h key or leftward key, we're at starting
of the second line.
l
The l command works same as the rightward arrow. You can move in rightwards
direction.

Example:

Look at the above snapshot, we are at starting and we want to move to the end of the last
line

Look at the above snapshot, after pressing either l key or rightward arrow, we're at the
end of the last line.
nG
Here n denotes a number. You can type a number before G to jump to a particular line.

Example:

7G

Look at the above snapshot, our cursor is at line 1 and we want to go to the line 7.

Look at the above snapshot, in command mode after typing 7G (you'll not be able to see
the typed command 7G on the screen), we're at 7th line.
G
If you'll not type any number before G, then you'll be directed to last line of the file.

Look at the above snapshot, our cursor is at line 1.

Look at the above snapshot, after pressing G, we're at the last line of the file.
``
This command allows us to move to the previous position in the file.

Example:

Look at the above snapshot, we are at line number 9.

Look at the above snapshot, we are at line 5 but forgot to write something on line 9.
Look at the above snapshot, after typing `` we moved back to line 9.

x
The command x is used to delete current character.

Example:

Look at the above snapshot, our cursor is at letter d and we want to delete it.

Look at the above snapshot, after pressing x, letter d is removed.


r
The r key will bring you in insert mode for just one key press and then you'll be again
directed to command mode.

Example:

Look at the above snapshot, we want to


replace 7 from 6

Look at the above snapshot, after pressing r, type 6 to replace number 7.


xp
The command xp allows us to switch two characters. The character at the right of the
cursor will come at the place of the cursor, and character at the cursor will switch to the
right of the cursor.

Example:

Look at the above snapshot, our cursor is at character e. Word teh is wrongly spelt. To
correct it r can be used.

Look at the above snapshot, both the characters that is character at the cursor (e) and
character at the right of the cursor (h) are switched from their positions. Hence now
word teh becomes the.
dd
The command dd deletes current line completely.

Example:

Look at the above snapshot, our cursor is at line third. We want to delete this whole line.

Look at the above snapshot, after pressing dd command, third line is deleted.
D
The command D is used to delete from current character to the end of the line.

Example:

Look at the above snapshot, our cursor is at third line at letter a. We want to delete from
the current character till the end of the line.

Look at the above snapshot, after pressing D, all the characters are deleted from letter a to
the end line.
dG
The command dG is used to delete from current line to the end line of the file.

Example:

Look at the above snapshot, currently our cursor is at second line.

Look at the above snapshot, all the lines are deleted from second line to the end line.
u
This command allows us to undo or cancel our last command.

Example:

Look at the above snapshot, this is our script.

Look at the above snapshot, with the help of dG command we have deleted last lines.

Look at the above snapshot, after pressing u, last command dG disables and all the
content came back
(.)
The . command allows us to repeat last used command.

Example:

Look at the above snapshot, we have used D command to delete some part of line second.

yy : Copy
The command yy (yank yank) is used to copy a line. Move the cursor to the line you want
to copy and then press yy.

P : Paste
The p command paste a copied or cut content after the current line.

Example:

Look at the above snapshot, this is our original script.


Look at the above snapshot, first we have copied the second line using yy, then press p.
Copied line will paste after the current line.

The P command paste a copied or cut content before the current line.

Example:

Look at the above snapshot, this is our original script.

Look at the above snapshot, after copying the content using yy, we moved the cursor to
first line and then pressed P. Line is copied before the current line
ndd
Here n denotes a number. It will delete n number of lines after the current line.

Example:

2dd

Look at the above snapshot, this is our script and we want to delete first two lines.

Look at the above snapshot, after typing 2dd, two lines after the current lines are deleted.
nyy
The command nyy is used to copy n number of lines after the current line.

Example:

2yy

Look at the above snapshot, we want to copy two lines after the first line.

Look at the above snapshot, after pressing 2yy, last two lines are copied and with P key
we have pasted them before the first line.
0
The 0 sign bring you to the start of the current line.

Example:

Look at the above snapshot, currently our cursor is at the middle of the second line.

Look at the above snapshot, after pressing key 0, we're at the starting of second line.

The ^ sign also bring you at the start of the current line.
$
The $ sign will bring you to the end of the current line.

Example:

Look at the above snapshot, currently our cursor is at the middle of the second line.

Look at the above snapshot, after pressing $ key, we are at the end of the current line.
d0
Combining sign 0 with d command enables us to delete start of a line from current
character.

Example:

Look at the above snapshot, position of the cursor is somewhere in the middle of the line.

Look at the above snapshot, letters from starting to the current character are deleted
d$
Combining sign $ with d command enables us to delete from current character to the end
of the line.

Example:

Look at the above snapshot, position of the cursor is somewhere in the middle of the line.

Look at the above snapshot, letters from current character to the end of the line are
deleted.
J
The J command can be used to join two lines

Example:

Look at the above snapshot, we need to join first two lines of the above script.

Look at the above snapshot, after pressing J, both the lines are joined.
yyp
The yyp command repeat the current line.

Example:

Look at the above snapshot, our current line is the second line.

Look at the above snapshot, after pressing yyp command, current line is repeated.

ddpThe command ddp swaps two lines (current line and line below the current line).
Example:

Look at the above snapshot, last two lines are swapped from their positions.
w
The w command allows us to move one word forward. To move more than one word
forward, you can type number before w command.

Example:

9w

Look at the above snapshot, from the current word we want to move 9 words ahead.

Look at the above snapshot, we have moved 9 words forward.


b
The b command allows us to move one word backward. To move more than one word
backward, you can type number before b command.

Example:

9b

Look at the above snapshot, from the current word we want to move 9 words backwards.

Look at the above snapshot, we have moved 9 words backward.


dw
The dw command is used to delete one word. But remember, to delete one complete word
cursor should be placed at the starting letter of the word.

Example 1:

Look at the above snapshot, we want to delete word example. Our cursor is placed at the
starting letter e.

Look at the above snapshot, after pressing dw command, word example is deleted.

Example 2:

Look at the above snapshot, here also we want to delete word example. But this time our
cursor is placed somewhere at the middle of the word.

Look at the above snapshot, after pressing dw command, word example is not completely
deleted. Only the letters starting from the cursor to the end of the word (including the
space) are deleted.
Example 3:

5dw

Look at the above snapshot, here we want to delete five letters after the current word.

Look at the above snapshot, after typing 5dw, five words after the cursor are deleted.

yw
The command yw is used to copy one word. Here also cursor has to be placed at the
starting to copy a complete word.

Example:

Look at the above snapshot, we want to copy word example.

Look at the above snapshot, after pressing yw key, move the cursor at place where you
want to copy the word. Here, we have copied it at the last with p command
/string
Here string has to be replaced by the searching word.

The /string allows us to do forward search for a string. If the same string is placed before
and after the cursor, it will only indicate the string after the cursor. When you'll start
typing /string in command mode, at lower left corner, typed word will be seen.

Example:

/readable

Look at the above snapshot, this is our normal script. We have to search
word readable from this script.

Look at the above snapshot, after typing /readable and pressing enter, mentioned word
on its first occurence will be automatically highlighted. Please note here, first occurence
is in forward search. The string readable is also present before the cursor, but it searches
forward.
?string
The ?string allows us to do backward search for a string. If the same string is placed
before and after the cursor, it will only indicate the string before the cursor. When you'll
start typing ?string in command mode, at lower left corner, typed word will be seen.

Example:

/readable

Look at the above snapshot, this is our normal script. We have to search
word readable from this script.

Look at the above snapshot, after typing ?readable and pressing enter, mentioned word
on its first occurence will be automatically highlighted. Please note here, first occurence
is in backward search. The string readable is also present after the cursor, but it searches
backward.
/^string
This command searches a string in forward direction but at the beginning of a line. If no
such word is present at the beginning of the line, it will show a no match message.

Example:

/^readable

Look at the above snapshot, after typing /^readable, cursor moved to the string which is
at the starting of the line.
/string$
This command searches string in forward direction but at the end of a line. If no such
word is present at the end of the line, it will show a no match message.

Example:

/readable$

Look at the above snapshot, after typing /readable$, cursor moved to the string which is
at the end of the line.
Syntax:<startLine,endLine> s/<oldString>\/<newString>/g
Note: Here g stands for globally. If the string is repeating more than once in a single line,
then g is to be used to replace it globally from all the places in the mentioned line range.

Example 1:

:1,$ s/readable/changed/

Look at the above snapshot, we have written the command :1,$ s/readable/changed/

After pressing enter key, string readable is replaced with string changed.
:g/string/d
To delete a certain string from the entire file.

Example:

We want to remove the occurrence of the string Verilog in the above shown code which
can be done with the command :g/Verilog/d

You can see that the string Verilog in all its occurrences will be deleted.
To delete the string for a particular range of lines we can use the command
:<startLine,endLine> g/string/d
:<startLine,endLine> s/^/<modification>/
Used to do any modifications (like giving 2 character length space, adding extra characters
to the pre-existing line etc.) at the beginning of the line.
Example:

We want to add Subject- in the beginning of the line from line 1 to 4 which is done using
the command :1,4 s/^/Subject-/

You can see that the necessary modification has been made.
Similarly if we want to make any modifications to the end of the line for a particular line
range we can use the command :<startLine,endLine> s/$/<modification>/

:0
To go to the beginning of the file.

:$
To go to the end of the file.

Shift+*
To highlight a text's every occurrence in the file.
To move around a file:

Commands Action

j To move down

k To move up

h To move left

l To move right

To delete:

Commands Action

x Delete the current character

X Delete the character before the cursor

r Replace the current character

xp Switch two characters

dd Delete the current line

D Delete the current line from current character to the end of the line

dG delete from the current line to the end of the file


To repeat and undo:

Commands Action

u Undo the last command

. Repeat the last command

Command to cut, copy and paste:

Commands Action

dd Delete a line

yy (yank yank) copy a line

p Paste after the current line

P Paste before the current line

Command to cut, copy and paste in blocks:

Commands Action

<n>dd Delete the specified n number of lines

<n>yy Copy the specified n number of lines


Start and end of line:

Commands Action

θ Bring at the start of the current line

^ Bring at the start of the current line

$ Bring at the end of the current line

dθ Delete till start of a line

d$ Delete till end of a line

Joining lines:

Commands Action

J Join two lines

yyp Repeat the current line

ddp Swap two lines

To jump lines:

Commands Action

G Will direct you at the last line of the file

`` Will direct you to your last position in the file


Move forward or backward:

Commands Action

w Move one word forward

b Move one word backward

<n>w Move specified number of words forward

dw Delete one word

yw Copy one word

<n>dw Delete specified number of words

Search a string:

Commands Action

/string Forward search for given string

?string Backward search for given string

/^string Forward search string at beginning of a line

/string$ Forward search string at end of a line

n Go to next occurrence of searched string

/\<he\> Search for the word he (and not for there, here, etc.)

/pl[abc]ce Search for place, plbce, and plcce


Replace all

Syntax:

1. :<startLine,endLine> s/<oldString>/<newString>/g

Example:

Commands Action

:1,$ Replace forwa with backward from first line to the last line Commented [JS1]: @Naresh P It look like sentence
mentioned in action column is wrong as per command used
s/readable/changed/

:3,6 s/letters/neww/g Replace forward with backward from third line to the ninth
line

Text buffers:

Commands Action

"add Delete current line and put text in buffer a

"ap Paste the line from buffer a

Visual commands:

Commands Action

> Shift right

< Shift left``add

y Yank(copy) marked taxt

d Delete marked text

~ Switch case Commented [JS2]: @Naresh P Please Elaborate ~


command
Marking text (Visual mode):

Commands Action

v Start visual mode, mark lines

V Start linewise visual mode

o Move to other end of marked area

O Move to other corner of block

Ctrl+v Start visual block mode

aw Mark a word

ab a()block (with braces)

aB A{}block (with brackets)

ib Inner ()block

iB Inner {}block

Esc Exit visual mode


Working with multiple files:

Commands Action

:e filename Edit a file in a new buffer


:bnext (or :bn) Go to next buffer
:bprev (of :bp) Go to previous buffer
:bd Delete or close a buffer
:sp filename Open a file in anew buffer and split window

Ctrl+ws Split windows


Ctrl+ww Switch between windows
Ctrl+wq Quit a window
Ctrl+wv Split windows vertically
Ctrl+wh Move cursor to the windows to the left

Ctrl+wl Move cursor to the windows to the right

Ctrl+wj Move cursor to the windows below the one you’re in

Ctrl+wk Move cursor to the window above the one you’re in

:ls List all open buffers


:Num,Nums/^/#/ Placing “#” for particular range
Cntrl+P Auto fill in insert mode
Cntrl +W S Split horizontally

Cntrl+W V Split Vertically


Cntrl+W pipe To expand Horizontally(particular file)

Cntrl+W underscore To expand vertically (particular file)

Pound To highlight selected keyword in a file

Cntrl+W R To Rotate the files


J Merge two lines as one
:$ Go to the last line

n After finding pattern, going top to down

N After finding pattern, going bottom to up

X Delete a character

Dw Delete a word
Dd Delete a line

Cntrl + W = Divide all the files equally

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy