Sunday, Dec 10th

Last update12:59:40 PM GMT

VI Editor Basics

Write e-mail

VI editorVI, visual interface, is a visual editor for the standard UNIX operating system. vi is most commonly used for programming. The following is a table of some basic vi commands. All of the following commands can only be used from command mode. When you first start vi, you are in command mode. If you are ever unsure which mode your in, hit <esc> and you will be in command mode for sure.

argaiv1215

Some VI commands are:

Command

Function

vi

invokes vi editor mode

esc
:wq

write and quit

esc
:w

write file

esc
:w filename

write to another file

esc
:w!

overwrite existing file

esc
:q

quite without writing

esc
:q!

quit file (discarding edits)

a

append after cursor

A

append text to end of current line

c0

change to the beginning of line

cc

change the entire line

cw

change to the end of a word

C or c$

change to end of line

dd

delete line

dw

delete word

D

deletes from the cursor position to the end of the line

esc

terminate insert mode

h

left, one space

H

top line of screen

i

insert text at current cursor position

I

insert text at beginning of line

j

down, one line

k

up, one line

l

right, one space

L

last line of screen

n

repeat previous search

N

repeat search in opposite direction

o

open line below current line

O

open line above current line

p

puts the text that is in the buffer after the cursor position

r

replace one character

s

substitute

S

substitute entire line

u

undo last change

w

move to the next word

x

delete character under cursor

y

yank - copy text into the buffer

Ctrl/B

Scroll backward one screen

Ctrl/F

Scroll forward one screen

Ctrl/G

display current line number

/text

search for text

/

repeat forward search

backspace

move back one character

return

move down one line

. (period)

repeat last command

~ (tilde)

change the case of a letter

^ or 0 (zero)

move to beginning of line

$ (dollar)

move to end of line

Moving Text

Use d or y commands to put text into the buffer.  Then use the p command to insert it back into the document.

Global Replacement

:[beginning line number,ending line number]s/old/new/[g][c]

:s/Hate/Love/

Change the first occurrence of "Hate" in the current line to "Love"

:s/Hate/Love/g

Change every occurrence of "Hate" in the current line to "Love"

:1,5s/Hate/Love/

Change the first occurrence of "Hate" in lines 1-5 to "Love"

:7,23s/Hate/Love/g

Change every occurrence of "Hate" in lines 7-23 to "Love"

:1,$s/Hate/Love/

Change the first occurrence of "Hate" in all lines of the file to "Love"

:%s/Hate/Love/g

Change every occurrence of "Hate" to "Love" in all lines.

:%s/Hate/Love/gc

Change every occurrence of "Hate" to "Love" in all lines with confirmation.

Configuration file

The file .exrc in your home directory is used by the vi editor to load commands every time the editor is started. Set commands can be entered into this file to configure your vi session the same way each time.  These same commands can be entered in the vi editor if they have a preceding colon (:) character.

set number|nonumber

Determines whether line numbers appear before each line

set showmode|noshowmode

Determines if the editing mode displays on the bottom right of the screen

set ic|noic

Determines if pattern searches should ignore case

vi editing commands can also be used to edit a unix command line by first pressing the Esc key and then the "-" key to retrieve the last line. Pressing the "-" key again will retrieve the next previous command.

References

http://www.cbu.edu/ietc/faq/vifaq.html
http://www.cec.mtu.edu/newuserdoc/node9.html

Share this post



Add comment

Please refrain from using slang or abusive language in the comments.
To avoid waiting for your comment to be published after being reviewed, please log in as a registered user.


Security code
Refresh

Web Hosting