Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Saturday, August 6, 2011

VI shortcuts Fasttrack

Let me summarize here with few vi commands for Fast reference:

Inserting text

esc + i insert text informant of the cursor
esc + a append text after the existing text
esc + O opens new line above the current line
esc + o opens new line under current line (insert mode)

Deleting text

esc + x deletes one character
esc + 5x deletes five charters
esc + dw deletes a word
esc + 5dw deletes five words
esc + dd deletes the whole line
esc + D deletes the line from cursor and forward
esc + d) deletes the sentence from cursor and forward
esc + d( deletes the sentence from cursor and backwards
esc + u undelete

Note: esc + d) or d( removes the sentence from cursor and forward/backwards
until it reaches a dot "."

Moving around in VI:

Make sure you are in command mode and the following letters will do:

j moves you down
k moves you up
h moves you left
l moves you right

Finding Text

Hit esc then type in a / you then go to the bottom of the screen
where you will see your / type in the text to look for.
ie. /Linux
that will find the word `Linux` in the open file.

Replacing Text

Hit esc and do: :start,stop,s/s_text/r_text/g

: indicates that this is an ex command
start is the starting line number
stop is the stopping point
s is the substitute command
s_text is the search string (the text you are looking for )
r_text is the text you are replacing with
g is global

Example:

Esc + :5,8,s/l/ll/g

This would replace all "l"'s with "ll" on lines 5 to 8.

Note to Replacing Text:
Line numbers can also be:
. current line
$ last line

Basic save & quit commands

Hit Esc and do a : where after you can type the commands.

w write (save)
q quit
! force

ie. :q! or :wq

To create control characters do:

Ctrl+V Ctrl+

Example:

Ctrl+V Ctrl+A

That will create a ^A character.

(These last 3 commands are very alike ed commands)

Another useful thing in VI is split-screen mode, so you can edit 2 files
at once, this is:

:split

Just press Esc and type ":split".