vi

From Initq

Jump to: navigation, search

vi is a screen-oriented text editor written by Bill Joy in 1976 for an early BSD release.

The name vi is derived from the shortest unambiguous abbreviation for the command visual in ex; the command in question switches the line editor ex to visual mode.

Contents

vi Modes

Ex mode
To manipulate files you need to be in ex mode. you enter ex mode by typing (:).
Insert Mode
You enter text in teh insert mode. you exit insert mode by hitting escape.

Basic Text-Editing Procedures

  • i is used for insert. now you are in insert mode where you can start typing text.
  • <num>yy will yank (copy) the number of lines, then you can paste them with p.
  • <num>dd will delete (cut) the lines, you can paste them with p.
  • p is for paste below the cursor and P is to paste above the cursor.
  • r is to replace one character and R is to go into replace mode.
  • u is for undo.
Case Change
use ~ to change case
Searches
/ for forward search and ? for backward search.
Changes
cc to change the whole line, cw to change one work. it will delete the work or line for you to insert.
Go to line
H takes you home to the first line of the current page, to go to the top most line do :1, G takes you to the ground floor of the file (last line), L takes you to the bottom line of the screen.
Global replacements
%s/original/replacement
Edit new file
 :e edits new file
Include existing file
 :r filename
Execute an external command
 :!ls
Quit
 :wq to write and quit, :q to quit (will not work without saving), :q! to force exit without save.

Drawing comment boxes

I like to put a big comment box at the top of each of my procedures. For example:

       /******************************************************
	 * Program -- Solve it -- Solves the worlds problems.  *
	 *     All of them.  At once.  This will be a great    *
	 *   program when I finish it.                         *
	 ******************************************************/

Drawing these boxes like this is tedious at best. But Vim has a nice feature called abbreviations that makes things easier.

First, you need to create a Vim initialization file called ~/.vimrc. (At first this may look like a ex initialization file. It is. The Vim command is actually a mode of the ex editor.)

The ~/.vimrc file need to contain the lines:

        :ab #b /************************************************
        :ab #e ************************************************/

You may also use the old vi rc file .exrc. vim will read .exrc or .vimrc. So to create a comment box, go in insert mode, enter #b<enter>. The screen looks like:

        /************************************************

Enter the comments, including the beginning and ending "*" characters. Finally end the comment by typing #e<enter>. This causes the ending comment to be entered.

Note

Some other useful commands that programmer may want to put in their ~/.exrc include:

	:set autoindent
	:set autowrite
	:ab #d #define
	:ab #i #include
	:ab #b /************************************************
	:ab #e ************************************************/
	:ab #l /*----------------------------------------------*/
	:set sw=4

The autoindent setting causes Vim to indent new lines by a similar amount to the one next to them. This is very useful for entering programs. The autowrite setting tells Vim to write the old file out when switching from one file to another.

The abbreviations #d, #i, and #l, define useful terms for programmers.

Finally, the command set sw=4 sets the shift width (the number of characters text is moved sideways for the shift command (<< and >>)).

This is very useful if you use a 4 space indentation for your C or C++ programs.

Removing ^M

  •  :%s/[ctrlkey+v and ctrl-key+M]//g

Turn Highlighting off

  •  :syntax off
Personal tools