Archive

Archive for the ‘Vi’ Category

VI – Cheat Sheet


Vi is a powerful text editing tool that can be used quickly and efficiently to alter files, scripts, etc. Below are some useful cheat sheet commands for vi to speed up text editing:

Move commands

$      – move to end of line
0      – move to beginning of line
G      – move to last line of file
1G     – move to first line of file
h      – move cursor left
l      – move cursor right
k      – move cursor up
j      – move cursor down
w      – move to next word
W      – move to next blank delimited word
b      – move to beginning of the word
B      – move to beginning of blank delimited word
H      – move to top of the screen
M      – move to middle of the screen
L      – move to bottom of the screen
CTRL+f – move screen down one page
CTRL+b – move screen up one page
CTRL+d – move screen down 1/2 a page
CTRL+u – move screen up 1/2 a page

Replace text commands

cw     – change word
r      – replace letter
R      – replace until [esc] is pressed
C      – change to the end of the line
cc     – change the entire line

Insert text commands

i      – insert before cursor
I      – insert before line
a      – append before cursor
A      – append before line
O      – insert line above current line
o      – insert line below current line
yy     – yank line
y$     – yank from current position to end of line
p      – put yanked text after cursor
P      – put yanked text before cursor
[shift] + [insert] – insert text saved in clipboard

Delete text commands

dd     – delete entire line
x      – delete character to the right of the cursor
X      – delete character to the left of the cursor
D      – delete to the end of the line

File commands

:qw    – quit and save
:w     – save
:q!    – quit without saving

Search Commands

/string – Search forward for string
?string – Search backward for string
n       – Search for next instance of string
N       – Search for previous instance of string

other commands

~       – toggle between upper and lower case
J      – join lines
.      – repeat last text-changing command
u      – undo last change
U      – undo all changes to line

Vi – Substitute Word

March 8, 2011 3 comments

Sometimes it is necessary to replace a word with a new word. Vi gives you the option to do this without having to manually go through searching for each occurence of the word individually.

For example, you wish to update a pfile for a database TEST to be used for database DEV. You would use the following command:

$ vi initTEST.ora

(From within VI perform the next steps:)

:1,$ s/TEST/DEV/gc

What the Code Means:

:

– in vi this allows you to enter a command

1,$

– perform this command wherever you see the word to replace

s

– substitute command

/TEST/

– match word, the /tells Vi the beginning of the word is to follow, the / tells vi that the word has ended.

DEV/

– text to replace with

g

– change every occurance on the line not just the first one, this is the global flag

c

– ask before committing, this allows you to confirm you wish each occurance to be changed as vi picks it up.