vi

vi


Overview

vi is an editor that is on every UNIX box in the world. It can be obtained for Windows environments.

I do all of my editing using vi, using it for both documentation and coding. If you are going to work in an internetworked environment, you need to learn to edit text (ASCII) files rather than relying on an editor which creates binary files.

This website was written using vi and standard UNIX shell commands. The ability of vi to use regular expressions allows great swathes of data to be modified using just a few keystrokes.

One great thing about vi is that all the editing keys can be found within the alpha-numeric key set. What this means is that you are not constantly looking for such remote keys as F3, CTRL, End, etc...

Here are some examples:

  • Rather than using the Delete key to erase a character, in vi just type x.

  • To delete two chars, type 2x.

  • To delete a line you don't have to do Mouse-Mark-Delete or Shift-Mark-Delete, you can just type dd. To delete 5 lines type 5dd.

Over the course of days, months, and years, one saves a lot of typing, a lot of looking, and a lot of finger stretching.

Another neat aspect of vi is that the entire UNIX command set is pretty much built into the editor, as well as vice-versa, the vi command set is available from the UNIX command line. Brilliance.


 

 

Edit Mode

When you first enter vi, you are in command mode. To go into edit mode, where one can actually type characters, enter one of the following:

Description Command
Go into insert mode i
Go into replacement mode R
Go into append mode a
Go into append mode at end of line A
Go into append mode beneath current line o
Change word cw
Change 3 words 3cw

To go back into command mode, at any time, type an <ESC>.

 

 

Command Mode

Description Command
Move cursor right one word w
Move cursor left one word b
Move cursor left one char h
Move cursor right one char l
Move cursor down one line j
Move cursor down 20 lines 20j
Move cursor up one line k
Move cursor up 20 lines 20k
Move cursor to the end of a word e
Move cursor to end of line $
Move cursor to beginning of line ^
Move cursor to the end of the file G
Move cursor to the first line of the file :1
Go to line 550 :550
Delete word dw
Delete line dd
Delete to end of line D
Copy two lines to buffer 2yy
Paste p
Replace one char r
Join two lines J
Search for a string /string
Search again for same string n
Repeat the last command .
Undo a change u
Change case ~
Substitute abc with xyz :%s/abc/xyz/g
Substitute abc with xyz, if abc is at the beginning of a line :%s/^abc/xyz/g
Substitute abc with xyz, if abc is at the end of a line :%s/abc$/xyz/g
Substitute abc with xyz, if line contains string :/string/s/abc/xyz/g
Quit without saving :q
Forced quit without saving :q!
Quit and Save I :wq
Quit and Save II :wq!
Quit and Save III ZZ


 

Running vi commands from the command line

Many of the commands listed above will work from the command line. To enable set your SHELL environment variable to ksh and your EDITOR environment variable to vi. Run something like the following on the command line or in your $HOME/.profile file:

   SHELL=ksh
   EDITOR=vi
   export SHELL EDITOR
or
   ksh
   set -o vi

Once the environment is set, to get into vi editing mode, just type <ESC> and run whatever vi command you wish. For example, to pull up your last command without retyping it, run:

<ESC> k

To scroll through a list of your previously run commands run

<ESC> kkkkk...

To search for a previously run command use:

<ESC> /command

To search for string in a collection of files, and automatically edit all files containing string, run:

vi `grep string *.html | cut -d: -f1 | sort | uniq`

Once in the editor, you can do the things like the following:

  • Press   /   to search for string within the file
  • Press   :n   to go to the next file
  • Press   n   to rerun that search made in a previous file
  • Press   .   to repeat a change made in a previous file


 

Text Manipulation

What follows are examples of how these commands can be used on the text in your file. The last line of each example is the command you type in to affect the lines above in the text body.

To search for a string in a file

    STAPLES
    GREEN
    WATERS
    HAWKINS
    PARLIAMENT
   /string

To add a .csv suffix to a list of strings

    GLCONSOL
    GLCONTROL
    GLMASTREL
    GLRPTDEF
    GLRPTSEL
    GLTRANS
    GLTRANSREL
    GLUNITS
    GLZONE
   :%s/$/\.csv/

To remove a string

    GLTRANS
    GLTRANSREL
    GLUNITS
    GLZONE
   :%s/GLTRANS//