Archive for May, 2004

vi cheat sheet

Sunday, May 9th, 2004

VI Reference SOP

Creating and opening Files
Running a vi command without a file name will open a new file. Running a vi command with a existing file name will open that file for editing. For example, using vi test.txt will open a document called test.txt if it’s in the working directory.

VI Modes
Command mode treats input from the keyboard as vi commands. Command mode cannot be used for entering text. When a file is first opened, you start out in command mode and you will not see the words you are typing on the screen . To enter and edit text you have to switch to insert mode by pressing the i or a keys. Use command mode to move to the part of the file you want to edit and then use insert mode to enter text in the file. To switch back to command mode use the Escape key.

COMMAND MODE
Moving around the file
h Move the cursor one column to the left
i Move the cursor one column to the right
k Move the cursor one line up
j Move the cursor one line down
^ & B Go to the beginning of the current line
$ Go to the end of the current line
) Go to the next sentence
( Go to the previous sentence
} Go to the next paragraph
{ Go to the previous paragraph
:$ Go to the end of the file
w Move the cursor one character forward
W Move the cursor one word forward
: Go to the number you specify

Inserting and Appending Text :
i inserts text to the left of the cursor
I inserts text in the beginning of line
a appends text to right of cursor
A appends text to the end of the line

Adding New Line
o Add a new line below the current line
O Adds a new line above the current line.

Deleting Text
X deletes the text character to the right of the cursor
dd deletes the current line
d deletes the line specified by NUMBER
D delete all of the data until the end of the current line.

Replacing Words and Characters
r replace the character above the cursor
R replaces characters until Esc is pressed
C replaces till end of line.

Substitute
s substitutes the current character
S substitutes the entire line

Repeating Last Command
. repeats the last command

Undo the last change
u undo the last change
U undo changes to the current line

Copy and pasting lines
yy copy the current line into buffer
p pastes the information in the current buffer

Searching
:/name searches for the word name in the file
n continues search forward.
N searches backwards.
Search and Substite :s///g

Saving
:w is a simple save command
:wq saves & quits VI
:q! quit VI without saving any changes