Unit 7 The vi Editor
Course code LX13
      Linux Basics
Objectives
 After completing this unit, students should be able to use
 vi to:
  Create and edit files
  Manipulate text within a file
  Set up defaults for the vi editor
  Execute command-line editing
  Define the uses for the other forms of vi
vi
     Full-screen editor
     Two modes of operation
      command
      text
     Utilizes one-letter command
     Does not format text
     Flexible search and replace facility with pattern matching
     Allows for user-defined editing functions using macros
Starting vi
 $ vi vifile
              ~
              ~
              ~
              ~
              ~
              ~
              ~
              ~
              ~
              ~
              ~
              ~
              "vifile" New file
Two Modes
Command                                      Input
mode                                         mode
   a      append text after cursor
   A      append text at end of line
   i      insert text at the cursor
   I      insert text at beginning of line
   o      add an empty line under cursor
   O      add an empty line above cursor
   s      substitute a character with text
                                              <ESC>
Cursor Movement
                    <ctrl-b>
                       H
                       k
                   <up-arrow>
      0                                 $
     b,B                              w,W
      h                                 l
<left-arrow>                     <right-arrow>
                  <down-arrow>
                        j
                        L
                     <ctrl-f>
                       G
Editing Text
  To delete a single character               x
  To delete to the end of the current word   dw
  To delete to the end of the line           d$
  To delete to the start of the line              d0
  To delete the whole line                   dd
  To delete a range of lines                 :20,40d
  Replace text by overtyping                 Rnewtext
  To change to the end of next word          c2w
  Undo the last change                       u
Adding Text
 Keystroke
  i          This file contains some lines.
             Line 2.
             And this is line 3.
             Line 4 follows line 3.
             The last line is line 5.
             ~
             ~
             ~
             ~
             ~
             ~
             ~
             "vifile" New file
Exiting the Editor
 Keystroke
             This file contains some lines.
  <esc>      Line 2.
             And this is line 3.
             Line 4 follows line 3.
             The last line is line 5.
             ~
             ~
             ~
             ~
             ~
             ~
             ~
             :wq
Search for a Pattern
 Keystroke
  <esc>      This file contains some lines.
             Line 2.
             And this is line 3.
  n          Line 4 follows line 3.
             The last line is line 5.
             ~
             ~
             ~
             ~
             ~
             ~
             ~
             /line
Find and Replace
 Keystroke
  <esc>      This file contains some lines.
             Line 2.
             And this is lINE 3.
             Line 4 follows lINE 3.
             The last lINE is lINE 5.
             ~
             ~
             ~
             ~
             ~
             ~
             ~
             :g/ line /s// lINE /g
Cut and Paste
 Keystroke   This file contains some lines.
  <esc>      Line 2.
             And this is lINE 3.
             The last lINE is lINE 5.
 dd          Line 4 follows lINE 3.
             ~
 p           ~
             ~
             ~
             ~
             ~
             ~
             "vifile" 5 lines, 108 characters
Copy and Paste
 Keystroke
  <esc>      This file contains some lines.
             Line 2.
             And this is lINE 3.
 yy          Line 4 follows lINE 3.
             The last line is lINE 5.
 p           Line 4 follows lINE 3.
             ~
             ~
             ~
             ~
             ~
             ~
             "vifile" 5 lines, 108 characters
vi - Executing Linux Commands
 You can run Linux commands from within vi.
 The vi command :! command will execute command in
 a subshell. After the completion of command, you will be
 placed back in vi.
 Examples:
  :! ls        will execute the ls command
  :! bash      will give you a shell prompt in which you
               can enter a series of commands.
vi Options
  Options entered in the vi session change the behavior
  of the vi editor:
  :set all
  :set autoindent/noautoindent
  :set number/nonumber
  :set list/nolist
  :set showmode/noshowmode
  :set tabstop=x
  :set ignorecase/noignorecase
  :set wrapmargin=x
  Options can be stored in $HOME/.exrc
  Macros can be written and new commands created
Example .exrc
       set showmode
       set tabstop=5
       set list
       ab lx linux
       ab thansk thanks
       ab sohw show
       ~
       ~
       ~
       ~
       ~
       ".exrc" 8 lines, 81 characters
vi Editors
 vi          Full-screen, full-function editor
 view        Read-only form of vim. Changes cannot be
             saved unless overridden with a force (!)
 ex          Subset of vim working in line mode, can
             access the screen editing capabilities of vim
 ed          Subset of vim working in line mode, can
             access the screen editing capabilities of vim
Command Line Editing
 Uses the same keystrokes as vi
 Can correct mistakes in the current line
 Uses editor keys to edit/re-enter previous lines
                      $ set -o vi
                h        j       k          l
vi Cheat Sheet
 Start vi             Change text
  $ vi file name      cw R
 Exit vi              Delete text
  :wq :x :ZZ :q       x dd dw
 Force                Copy/Paste
  !                   yy p
 Go to input mode     Undo
  i a o I A O s       u
 Go to command mode   Cursor Movement
  <Esc>               h j k l
Checkpoint
 1. When using the vi editor, what are the two modes of
    operation?
 2. While using vi, how do you get to command mode?
 3.   Which of the following could you use to enter in text?
 a.   a
 b.   x
 c.   i
 d.   dd
Checkpoint (2)
 4. vi can be used to globally change the first occurrence
    of a pattern on every line with a given pattern.
    True or False?
Unit Summary
 The VIM editor is started with the vi command
 The VIM editor starts a full-screen editor
 VIM has two modes of operation: text mode (input) and
 command mode
 VIM makes a copy of the file you are editing in an edit
 buffer. The contents are not changed until you save the
 changes
 Subcommands with the : , / , ? , ! read input from a line
 displayed at the bottom of the screen.