VI Commands
copyright© - Feng Wu, 1999


I. Introduction
1. History
2. Modes of Operation


Command Mode Input Mode Last Line Mode
ex X X
VI X X X
Command Mode input, append, open, change, replace
------------------------------------>
<------------------------------------
[Esc]
Input Mode
3. Correcting Text

erase, line kill keys
[ctrl]-W back up to the beginning of the Word
4. Command Case

case-sensitive
5. Work buffer

  • vi makes changes to a copy of your file in the work buffer
  • vi changes the disk file until you write the contents of the work buffer back to the disk
  • when you edit a new file, vi does not create the file until ...
  • to look at a file with vi but not to change it:
    • $ view <filename>
    • $ vi -R <filename> // Read-only
6. Abnormal Terminatin of Editing Session

:ZZ saves the contents and quits
:w <filename> names a file and writes it to the disk;
or, when you do not have write permission for the working file, or the working directory
:q quits
:q! quits without writing saving
[NOTE]: "!" is necessary only when you have made changes to a file
7. Recovering Text After a Crash

$ vi -r <filename> recover a file

II. The Display
1. Status Line

1) is the bottom line (24th)
2) it shows:
  • error messages
  • deletion and addition information
  • file status information
  • 2. Redrawing the Screen

    vi uses @ at the left of the screen to replace deleted lines
    [ctrl]-L (R) Redraw the screen (when screen becomes cLuttered) (?)
    3. ~

    vi marks lines that appear past the end of a file with a tilde (~)

    III. Command Mode - Moving the Cursor

    Unit of Measure: character, word, line, sentence, paragraph, screen
    Repeat Factor: a number precedes a cursor-movement command
    1. by Characters

    [ñ][space], l, [right-arrow] forward 1[/ñ] char[s]

    [NOTE] can not move past the end of the current line to the beginning of the next line
    [ñ]h, [left-arrow] backward 1 char

    [NOTE] can not move past the beginning of the current line to the end of the previous line
    2. by Words

    [ñ]w to the 1st letter of the next [ñ-th] word (group punctuation);

    to the next line if that is where the next [ñ-th] word is, unless the line ends with a [space]
    [ñ]W by blank-delimited Words, including punctuation

    [ñ]b backward to the 1st letter of the previous [ñ-th] word
    [ñ]B ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
    3. by Lines

    [ñ][return] to the beginning of the next [ñ-th] line
    [ñ][-] ... ... ... ... ... ... ... ... previous ... ...

    [ñ]j, [down-arrow] down one[/ñ] line[s] to the char just below the current one;

    if there is no char immediately, down to the end of the next line below

    [NOTE] will not pass the last line of text
    [ñ]k, [up-arrow] up ... ... ...
    4. by Sentences, Paragraphs

    [ñ]( to the beginning of the next [ñ-th] sentence
    [ñ]) ... ... ... ... ... ... ... ... previous ... ... ... ...

    [ñ]{ ... ... ... ... ... ... ... ... ... ... ... ... paragraph
    [ñ]} ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
    5. Within the Screen

    [ñ]H at the left end of the ñ-th line from Home (top) line
    [ñ]M to the Middle line
    [ñ]L to the ñ-th line from Last (bottom) line
    6. Viewing Different Parts of the Work Buffer

    [ctrl]-D scroll the screen Down half a screen
    ñ[ctrl]-D ... ... ... ... ... ... ... ... ñ lines
    [ctrl]-U ... ... ... ... ... ... Up ... ... ... ... ...
    ñ[ctrl]-U ... ... ... ... ... ... ... ... ... ...
    [ñ][ctrl]-F ... ... ... ... ... ... Forward 1[/ñ] whole screen[s]
    [ñ][ctrl]-B ... ... ... ... ... ... Backward 1[/ñ] whole screen[s]
    [ñ]G Goto the last[/ñ-th] line

    IV. Input Mode
    1. Insert Commands

    i insert before the current char
    I Insert at the beginning of current line
    [NOTE] sometimes, i, I overwrite text (displayed) on the screen, but not (stored) in the Work Buffer --> press [Esc] to re-display it
    2. Append Commands

    a append after the current char
    A Append ... ... ... end of the current line
    3. Open Commands

    o open a blank line below current line, within existing text, place the cursor at the beginning of the new line
    O Open ... ... ... ... above ... ... ... ... ... ... ... ... ... ...
    4. Replace Commands

    [ñ]r replace the current char with a single char follows 'r'
    [, repeats this step for ñ times]

    only one step, so doesn't need to press [Esc] to return to Command Mode
    [ñ]R Replace the chars following the current char with the chars follow 'R'
    [, repeats this step for ñ times]

    needs to press [Esc] to finish inputing and return to Command Mode

    [NOTE] work strangely if replace <tab> chars
    ([tab] is 1 char and will be replaced by 1 single char)
    5. Quote Commands

    [ctrl]-V enables the insertion of special chars into your text, and quotes the single char that follows it
    [ctrl]-L redraw the screen
    [ctrl]-W backs the cursor up a Word to the left
    [Esc] ends Input Mode

    V. Command Mode - Deleteing and Changing Text
    1. Undo Commands

    u undo whatever most recent change;
    or, restores only the most recently changed text
    U Undo - restores the current line to the way it was before any change
    2. Delete Character Commands

    [ñ]x deletes the current char [, repeat for ñ times]
    3. Delete Commands

    d[ñ]b to the beginning of the [ñ-th] word
    d[ñ]B ... ... ... ... ... ... ... ... ... ... Blank-delimited word

    d[ñ]w ... ... end ... ... ... ... ... ... ... word
    d[ñ]W ... ... ... ... ... ... ... ... ... ... blank-delimited Word

    d[ñ]( ... ... beginning ... ... ... ... ... sentence
    d[ñ]) ... ... end ... ... ... ... ... ... ... ... ... ...

    d[ñ]{ ... ... beginning ... ... ... ... ... paragraph
    d[ñ]} ... ... end ... ... ... ... ... ... ... ... ... ...

    d0 ... ... beginning of line
    [ñ]dd [ñ lines start with] the current line

    dH through the Home line on screen
    dM ... ... ... ... Middle ... ... ... ...
    dL ... ... ... ... Last ... ... ... ...

    dG ... ... ... ... end of Work Buffer
    d1G ... ... ... ... beginning ... ... ...
    4. Change Commands
    • replace existing text with new text
    • new text needs NOT to occupy the same space as the existing text (character, word, line, or paragraph)
    • enter the new text, and then press [Esc]

    c[ñ]b to the beginning of the [ñ-th] word
    c[ñ]B ... ... ... ... ... ... ... ... ... ... Blank-delimited word

    c[ñ]w ... ... end ... ... ... ... ... ... ... word
    c[ñ]W ... ... ... ... ... ... ... ... ... ... blank-delimited Word

    c[ñ]( ... ... beginning ... ... ... ... ... sentence
    c[ñ]) ... ... end ... ... ... ... ... ... ... ... ... ...

    c[ñ]{ ... ... beginning ... ... ... ... ... paragraph
    c[ñ]} ... ... end ... ... ... ... ... ... ... ... ... ...

    [ñ]cc [to the [ñ lines start from] the current line

    VI. Searching for a String
    1. the Search Commands

    ...
    2. Special Characters in Search Strings

    ...
    3. Search Command table

    /abc the next occurrence of the string "abc"
    /\<abc\> ... ... ... ... ... ... ... ... ... word "abc"
    /^abc the next line that starts with "abc" (?)
    /^[0-9][0-9] ... ... ... ... ... ... ... ... ... ... ... 2-digit number followed by a ")" (?)
    /\[abc] ... ... ... word ... ... ... ... ... ... "a", "b", or "c"
    / repeat the last search

    VII. Substituting One String for Another
    1. format :[address]s/search-string/replace-string[/g]
    2. address

    (nothing) the current line
    . ... ... ... ... ... ...
    ñ line ñ
    1 the beginning of the work-buffer / the 1st line
    $ ... end ... ... ... ... ... ... ... ... ...
    % ... entire ... ... ... (in some versions only)

    ñ1, ñ2 line ñ1 through ñ2
    .,.+ñ the current line through the ñ-th following line (totally ñ+1 lines)
    1,. the beginning of the work-buffer through the current line
    .,$ the current line through the end of the work-buffer
    1,$ the entire work-buffer
    3. /g flag

    /g affect every occurrence
    4. the Search-String and Replace-String

    s/abc/xyz 1st occurrence of the string "abc" on the current line by the string "xyz"
    s/abc/xyz/g every ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
    s/\<abc\>/xyz 1st ... ... ... ... ... ... word "abc" ... ... ... ... ... ... ... ... ... ... string "xyz"
    (it is not necessary to define "xyz" also as a word)

    VIII. Miscellaneous Command
    1. Join Command

    J
    • joins the line below the current one to the end of the current line
    • also inserts a [space] between them, and leave the cursor on the [space]
    • if, the current line ends with a ",", "!", or "?", inserts 2 [space]s
    2. Status Command

    [ctrl]-G displays:
  • the file name
  • current line number
  • total number of lines in the Work Buffer
  • the percent of the Work Buffer preceding the current line
    e.g.
    "test.c" [Modified] line 40 of 100 --40%--
  • 3. . Command

    . repeats the most recent command that made a change

    IX. The "Put", "Delete", and "Yank" Commands

    ......

    Good Luck !

    OU Home | Disclaimer | Copyright | Equal Opportunity | OU Web Policy