2) Editors and regular expressions


Chapters 11 and 12

Why vi? With sophisticated GUI editors around, I would not recommend using vi for normal text editing. But there are always situations (such as login to different or remote machines) when no other editor is available. Furthermore, vi facilitates regular expression-based substitution.

default is command mode
enter text-mode
  • insert: i (before cursor), I (in front of current line)
  • append: a (after cursor), A (at end of current line)
  • open: o (new line before current line), O (after current line)
  • change: cc (change current line)
  • leave text-mode press ESC
    enter ex mode press :
    leave ex mode press RETURN or ESC


    Open a file. Enter text mode. Type several lines. Leave text mode. Go to the second line and type something after it. Leave text mode. Go back to the second line and type something before it. Open a new line. Go to the first line and change it. Etc.

    Commands in command mode

    go right l, SPACE, right arrow
    go left h, BKSPC, left arrow
    go to next line +, RETURN
    go down j, Ctrl-n, Ctrl-j, down arrow
    go up k, Ctrl-p, up arrow
    go to beginning of the line ^
    go to end of the line $
    next word w
    previous word b
    go to beginning of the file H
    go to end of the file L
    next page Ctrl-f
    previous page Ctrl-b
    put current line on top of screen z RETURN
    delete character x
    delete line dd
    undo u
    repeat command .
    forward search /pattern RETURN
    backward search ?pattern RETURN
    repeat forward search n
    repeat backward search N
    clear screen Ctrl-r or Ctrl-l
    show status message Ctrl-g


    Try all of the above. Do several searches.

    Commands in ex mode

    save w
    quit and save wq or x
    quit without saving q!
    show mode set showmode
    show line numbers set number (set nonumber)
    show matching parenthesis set showmatch
    execute Unix command ! command
    search /search_pattern/
    repeat search //
    search backwards ?search_pattern?
    substitute (replace) s/search_pattern/replace_pattern/
    substitute globally 1,$s/.../.../g
    substitute globally and ask for confirmation 1,$s/.../.../gc
    set ignore case for searches set ic
    variable for replacement string &


    Try all of the above. Type some more text. Save it. Make changes. Etc.

    Files that vi uses

    /etc/termcap terminal settings
    .exrc user defined settings for commands, etc

    Regular Expressions

    pages 146 - 150

    . Any single character except a newline
    ^ The beginning of the line or string
    $ The end of the line or string
    * Zero or more of the last character
    \{m,n\} at least m times, at most n times
    \{m,\} at least m times
    \{m\} exactly m times
    [ ] any character found between []
    [^ ] any character not found between []
    [qjk] Either q or j or k
    [^qjk] Neither q nor j nor k
    [a-z] Anything from a to z inclusive
    [^a-z] No lower case letters
    [a-zA-Z] Any letter
    \ escape
    [:space:] a space
    \( \) remembering a pattern, referred to by \1, \2, etc


    Find the next occurrence of
    THE or the or The or
    a word that starts with b
    a number
    a punctuation mark
    a non-letter
    a line that starts with "the" and ends with "."
    the next double character

    Grep and sed

    Chapter 15

  • searching for file content
  • grep 'pattern' filenames
  • options: -l (lines are not displayed)
  • -w (search for words)
  • -i (ignore case)
  • -n (line number)
  • -c (count matches)
  • sed
  • useful for editing in shell scripts
  • sed 'edit command' filename
  • example: sed s/sun/moon/g file