vi Editor Basics...

Getting Started

The command "vi" without any file name will open a new file where you can enter the text and edit but while coming out you will be asked to enter a valid file name to save the text.
"vi" with a existing file name will open that file for editing.

After a file is opened it is in command mode, that is - input from the keyboard will be treated as vi commands and you will not see the words you are typing on the screen. To enter the text you have to put vi in insert by pressing 'i' or 'a' after which you can add the text and whatever is being typed will be seen on the screen. To switch between these mode Esc key is used .

Esc i -- insert text mode  
Esc -- command mode 

If you don't want to save the work ":q" will take you out. For save and quit ":wq" is used. Simple ":w" saves the current file and don't exit and ":q!" , ":wq!" causes a forced quit from vi.

Editing commands

Moving around the file:

h -- cursor left     
l -- cursor right
k -- cursor up       
j -- cursor down
^ & B -- Beginning of line  
$ -- end of line
) -- Next sentence 
( -- Previous sentence
} -- Next Paragraph
{ -- Previous  Paragraph
:$ -- end of file
w -- one character forward
W -- one word forward
:20 -- go to Line no 20 or whatever number you give

Displaying file info:

^g -- give name of the file, current line and total lines of a file at the bottom.

Inserting and appending text :

i -- inserts text to the left of  cursor
I -- inserts in the beginning of line 
a -- appends text to right of cursor 
A -- appends to the end of line

Adding new line:

o -- add a new line below the current line
O -- adds a new line above the current line.

Deleting the text:

x -- deletes text above the cursor
X -- deletes text character on the right of cursor
20dd -- deletes 20
dd -- deletes current line
D -- delete till end of current line.
d#<moveDirection> -- delete to where the #<moveDirection> (j,k,l,h) specifies.
d/ -- delete till pattern is found (forward).
d? -- delete till pattern is found (backward).
d' -- delete till mark 'char'.

Replacing a character & word:

r -- replace the character above the cursor.
R -- replces characters until Esc is pressed.
cw -- replaces the word from cursor to the end indicated by $ sign.
C -- replaces till end of line.

Substitute:

s -- subistutes current charcater.
S -- substitutes entire line.

Repeating last command:

. -- repeats the last text.

Undo the last change:

u -- undo last change.
U -- undo changes to the current line.

Copy and pasting lines:

yy -- copies the current line into buffer.
5yy -- copies 5 lines from the current line.
p -- pastes the current buffer.

Recovering an unsaved vi file:

vi -r <filename> -- restores a unsaved / crashed file from buffer.

Searching:

:/name -- & return searches for the word name in the file 
n -- continues search forward.
N -- searches backwards.

Substitution:

:s/<search-string>/<replace-string>/g

Saving:

:w -- saves the text does not quit.
:wq -- saves & quit the editor.
ZZ -- save & quit the editor.
:q! -- Quit without saving.

Miscellaneous:

m <char> -- mark this location and name it char.
' <char> -- (quote character) return to "line" named char.
` <char> -- (back-quote character) return to "place" named char.
'' or `` -- (quote quote) return from last movement.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

editing commands

i -- cursor right

it should be "l"

corrected now

Thanks, it has been corrected.

Set shiftwidth

Below sets the shift-width to 4 when indenting entire text block in visual mode:

:set sw=4

Run a program from within vi

Run the program cmd.

:!cmd

Run an interactive shell.

:sh

You do not need to save the file, and it will be in its old place when you exit the shell.

Line numbering scripts in vim

In command mode of vim type: ':set number' (or, ':se nu') and to turn off numbering during edit ':set nonumber' (or, ':se nonu').

vi - Additional Tips...

xp -- to invert order of characters	
eas -- to pluralize a word
d0-- to delete till begining of line
:g/pattern/p -- to browse all lines with 'pattern'
:g/str/s//replace/[g][c] -- to substitute 'str' with 'replace' globally.
This is a tas^W	-- would back cursor to 't' of tas.
This is a tas^U	-- backs up cursor to beginning of line.

Note: Since these keys can be redefined check their status by command below and look for "werase" , "kill":

  $ stty -a
  • To copy a set of lines from a file:

    1. save current file use :w
    2. edit the file with required data, use `:e <filename>`.
    3. now yank lines into a buffer e.g. `"a3Y` would yank 3 lines into buffer `a`.
    4. edit original file use `:e! <filename>`.
    5. place cursor where using command `"ap` or `"aP`, the text in the buffer 'a' will be pasted.
    6. To switch back to the place in previous file being edited use: CTRL^ (hold control key while pressing ^ key), this is a short key for `:e #1` and can be used to move back and forth between files.
  • To comment in C language (/*...*/) you can map characters say v & V in the following manner:

      map v I/*^[$a*/^[^M
      map V /\/\*/^MNxx/\*\//^Mxx``
     

    "v" will comment a single line by placing /* and */ at the beginning and end of the line respectively, and "V" will remove these from a commented line or paragraph (you must be at or within the comments).

  • To print a certain range of lines straight to printer without having to save in between in a file :

      :#1,#2 w !lpr
      

    would send lines from #1 to #2 to the default printer.

  • Deleted lines can be recovered by going through the numbered buffers:

    The nubered buffers 1 through 9 hold the last 9 deletes.
    To look through the buffers :

    1. "1p -- this will echo the buffer #1
    2. u -- this will undo the paste
    3. . -- this will execute the last cmd "1p , but in this unique case adds 1 and execs. "2p another . would exec. "3p . When you hit the correct buffer, keep it!
Comment