vi editing  «Prev  Next»
Lesson 2 A review of vi
ObjectiveIdentify key concepts of vi.

Key Concepts of the UNIX `vi` Editor

This lesson provides a brief summary of key concepts and commands about vi. The `vi` editor, a modal text editor, has been a staple in the UNIX environment since its inception in the 1970s. Its unique interface, defined by a distinct separation of command and insertion modes, has made it an indispensable tool for system administrators and developers alike. Here's an overview of its key concepts:
  1. Modal Operation:
    • Command Mode:When `vi` is first launched, it is in command mode. In this mode, most keypresses are interpreted as commands, not as input to be inserted into the file.
    • Insert Mode:By pressing certain keys (`i`, `I`, `a`, `A`, `o`, `O`), the user can enter insert mode, where keypresses result in text insertion.
  2. Basic Movement Commands:
    • `h`, `j`, `k`, `l`: Move left, down, up, and right respectively.
    • `w` and `b`: Move forward or backward by word.
    • `0` and `$`: Move to the beginning or end of a line.
  3. Editing Commands:
    • `i`: Enter insert mode before the cursor.
    • `I`: Enter insert mode at the beginning of the line.
    • `a`: Enter insert mode after the cursor.
    • `A`: Enter insert mode at the end of the line.
    • `o`: Open a new line below and enter insert mode.
    • `O`: Open a new line above and enter insert mode.
    • `dd`: Delete the current line.
    • `yy`: Yank (copy) the current line.
    • `p`: Paste after the cursor position (or below the current line).
    • `P`: Paste before the cursor position (or above the current line).
    • `u`: Undo the last operation.
    • `Ctrl + r`: Redo the last undone operation.
  4. Search and Replace:
    • `/[pattern]`: Search forward for a pattern.
    • `?[pattern]`: Search backward for a pattern.
    • `:%s/[search]/[replace]/g`: Globally search for a string and replace it.
  5. File Operations:
    • `:w`: Write (save) the file.
    • `:q`: Quit `vi`.
    • `:wq` or `ZZ`: Write and quit.
    • `:q!`: Quit without saving changes.
    • `:e [filename]`: Open another file for editing.
  6. Setting and Configuration:
    • `:set number`: Display line numbers.
    • `:set no number`: Hide line numbers.
    • `:set list`: Display hidden characters.
    • `:syntax on`: Enable syntax highlighting (if supported).
    • `:set autoindent`: Enable automatic indentation.
  7. Visual Mode:
    • By pressing `v`, users can select text, making it easier to perform operations like yanking, deleting, or changing specific portions of text.
  8. Extensions and Variants:
    • `vim` (VI iMproved) is a widely-used extended version of `vi` that offers more features, plugins, and improved user experience.
  9. Command Line Execution:
    • `:!command`: Execute a shell command without leaving `vi`. For example, `:!ls` would list the directory contents.
  10. Markers and Jumps:
    • `m[letter]`: Mark the current position with a specific letter.
    • `'[letter]`: Jump to the marked position.

In conclusion, the `vi` editor, with its modal operation, range of commands, and powerful editing capabilities, has stood the test of time. Its efficiency, once mastered, makes it a go-to choice for many UNIX administrators and developers.


Key Concepts

  1. The UNIX command to open a file in the vi editor is vi file.
  2. You can use vi in either of two modes of operation. In command mode, [1] you use the keyboard for moving the cursor or for switching to text mode. In text mode[2] , you use the keyboard to add text to a document. When you press the Escape key while in text mode, you return to command mode.
  3. Commands from vi's underlying ex editor[3] start with a colon (:). They take effect when you press Enter. Regular vi commands do not appear on your screen. They take effect immediately after you press the key.

Key vi Commands

vi commandsDescription
:w, :q, :wq Write (save) changes, quit vi, or do both together
h, j, k, l Move one character at a time (left, up, down, right)
w, b Move forward or backward by words
W, B Move forward or backward by words, ignoring punctuation
), ( Move forward or backward by sentences
Ctrl-F, Ctrl-B, Ctrl-D, Ctrl-U Move one screen forward or backward, or move half a screen down or up
i, a, o Insert before the cursor, append after the cursor, or open a blank line
c, d, y Operators for changing, deleting, or yanking (copying) text. Combine an operator with a movement command to create an editing command.
p, P Put buffer text after or before the cursor
., u, U Repeat last edit, undo last edit, or undo edits to current line

Movement commands and editing commands accept numeric multipliers[4]. For example, 5W moves the cursor ahead five words, and 3dd deletes three lines. In the next lesson, you will learn to search for text.

[1]command mode: Command mode is one of two modes of operation in vi. In command mode, you can move the cursor and issue editing commands, but you must switch to text mode to add text.
[2]text mode: Text mode is one of two modes of operation in vi. In text mode, you can use your keyboard to add text to your document.
[3]ex editor: ex is a line editor that serves as an underlying program for the vi editor. A line editor is a program in which you see only one line at a time.
[4]multiplier:In vi, a multiplier is a number that you place before a movement command or editing command to extend the range of the command.

SEMrush Software 2 SEMrush Banner 2