The vi Editor

Why is it important to learn vi instead of using a GUI to edit files?
It is on almost every UNIX/Linux system you will encounter. Vi is the front end to the ex editor. There is a man page for both vi and ex. Features: Envoking vi
$ vi filename

Exercise: create a new file

$ cd
$ vi testfile
:q
$ cp /etc/fstab .
$ vi fstab

Getting around a file that exists in vi

Command Mode

Once you type the command vi  filename you leave the shell and move into vi's Command mode.

From the command mode you can:

NOTE: as long as you have not added a charater you are still in Command mode.

Moving the Cursor

Searching for text

Exercise: moving around the fstab file in our home directories

?swap

General movement within the file

Deleting text in Command mode: lines, words, or characters

dd remove a whole line
u undo the last change
U Restore the entire line (if the cursor has not left the line)
2dd delete the current line and the one below it
D delete the remainder of the line after the cursor
yy copy one line
2yy copy two lines
P paste the copied or deleted line(s) above the line with the cursor
p paste the copied or deleted line(s) below the line with the cursor
dG deletes to the end of the file after the cursor
dw delete current word - place cursor on first letter
3dw delete 3 words
x delete individual characters
d$ delete to the end of the line
dG delete to the end of the file

Insert Mode (also known as append or text mode)

Single letter commands for inserting text:

Exercise: practice using these single letter commands in insert mode

Leaving insert mode
Inserting Text
$ vi fstab

<Esc>

  • now you can move about with the arrow keys
  • Exit vi without saving changes
    :q!
  • Saving as you go

    :w
    Opening a line above or below the cursor

    <Esc>

    Summarizing the Append Commands
    Summarizing the Deleting Commands

    Moving Text: using yank and put

    Exercise: create a new file

    $ cd
    $ vi jack
    All work and no play makes Jack a dull boy.
    All work and no play makes Jack a dull boy.
    All work and no play makes Jack a dull boy.
    no play
    All work and no play makes Jack a dull boy.
    All work and no play makes Jack a dull boy.
    no play
    All work and no play makes Jack a dull boy.
    All work and no play makes Jack a dull boy.

    Executing Linux/UNIX Commands in vi

    $ cp /etc/passwd .
    $ vi passwd
    :!ls

    G
    o
    <Esc>
    !!date

    <Esc>
    :!sh
    <Esc>
    :r jack
    :wq

    Options for Changing How vi Operates (more advanced)

    $ cat /home/pattyo/.exrc
    set ts=4

    A few vi Customizing Options (do not use the ":" inside the .exrc file)

    $ vi jack
    :set nu
    :set ic
    /all
    :set noic

    /all
    :q

    Advanced Concepts

    Global Changes Substitution

    Two other examples for making global substitutions

    Deletion

    :g/^Jill/d
    :g/^$/d

    Sorting

    !}sort +1
    Moving large blocks of text from one file to another
    $ vi /tmp/jack
    "a5yy
    :vi newjack
    "ap