VI Commands in Linux Programming

Closing and Saving Files

When you edit a file in vi,you are actually editing a copy of the file rather than the original.the following selections describe methods you might use when closing a file,quitting vi,or both.

Quitting and Saving a File

the commands ZZ will allow you to quit vi and save the edits made toa file.you will then return to linux prompt.note that,you can also the following commands.

Sr.No

Command

Descriptions

01

:w

To save your file but not quit vi.

02

:q

To quit if you have not any edits.

03

:wq

To quit and save edits.

Quitting without Saving Edits

To erase all edits made to the file and either start over or quit.to do so,you can choose from the following two commands.

Sr.No

Command

Descriptions

01

:e!

Reads the original file back in so that you can start over.

02

:q!

Wipes out all edits and allows you to exit from vi.

Cursor Movement

You must be in command mode if you wish to move the cursor to another position in your file.if you have just finished typing text,you are still in insert mode and will nedd to pres Esc to return to the command mode.

Sr.No

Command

Descriptions

01

h

move left

02

j

move down

03

k

move up

04

l

move right

05

w

moves the cursor forward

06

b

moves the cursor backward

07

e

moves to the end of word

Screen Movements

To move the cursor to a line within your current screen uses the following command or keys.

Sr.No

Command

Descriptions

01

H

move cursor to the top line of the screen

02

M

move cursor to the middle line of the screen

03

L

move cursor to the bottom line of the screen

Inserting

Sr.No

Command

Descriptions

01

r

replace character under cursor with next character typed

02

R

keep replacing character until esc is hit

03

i

insert befor cursor

04

a

append after cursor

05

A

append at end of line

06

O

open line above cursor and enter append mode

Deleting

To delete a character,first place your cursor on that character.then you may use any of the following command.

Sr.No

Command

Descriptions

01

x

to delete character under cursor

02

X

to delete the character to the left of your cursor

03

Dd

to delete all the current line.

04

dw

to delete from the character selected to the end of the word

05

D

to delete from the current character to the end of the line

Copying Text with Yank

If you wish to amke a duplicate copy of existing text,you may use the Yank and put commands to accomplish this function.Yank copies the selected text into a buffer and holds it until another yank or deletion occurs.Yank is usually used in combination witha word or line object such as the ones shown below.

Sr.No

Command

Descriptions

01

Yw

copies a word into a buffer

02

yy

copies a line into a buffer

Cutting & Pasting Text

Sr.No

Command

Descriptions

01

yw

yanks current word

02

yy

yanks current line

03

y$

yanks to the end of the line

04

Y

yanks the current line

05

P

Pasting from the cursor position

Command for Quit Vi

Sr.No

Command

Descriptions

01

ZZ

writes the buffer to the file and quits vi

02

:wq

writes the buffer to the file and quits vi

03

:w filename and :q

writes the buffer to the filename with the content of the buffer and quits vi.

04

:w! filename and :q

overwrites existing the filename with the current of the buffer and quits vi.

05

:q!

quits vi whether or not changes made to the buffer since the last write command

05

:q

quits vi if changes made to the buffer were written to a file.

Misc Commands

Sr.No

Options

Descriptions

01

Crtl + G

Gives the line number of current position in the buffer and modification status of the line

02

.(dot)

repeats the action performed by the last command

03

u

undoes the action performed by the last command

04

U

restores all changes to the current line since you moved the cursor to the line

05

J

joins the line immediately below,the current line with the current line

06

~

changes character at current cursor position from upper case to lower and vice-versa

07

:sh

temporarily returns to the shell to perform some shell commands.type exit to return Vi

08

Crtl + L

Clears and redraws the current windows.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!