Comprehensive Vi Editor Tutorial

Vi is a powerful, lightweight, and ubiquitous text editor in Linux and Unix systems. This tutorial covers the essentials of using Vi, from basic navigation to advanced editing techniques, suitable for beginners and intermediate users.

Table of Contents

  1. Introduction to Vi
  2. Starting Vi
  3. Modes in Vi
  4. Basic Navigation
  5. Editing Text
  6. Saving and Exiting
  7. Search and Replace
  8. Copy, Cut, and Paste
  9. Undo and Redo
  10. Advanced Features
  11. Customizing Vi
  12. Tips and Tricks
  13. Common Issues and Solutions

Introduction to Vi

Vi (and its enhanced version, Vim, short for “Vi IMproved”) is a highly configurable, modal text editor available on almost all Linux and Unix systems. Its efficiency comes from keyboard-driven commands, making it a favorite for developers and system administrators.

Key features: - Lightweight and fast - Modal editing (command, insert, visual modes) - Extensive plugin support (in Vim) - Available by default on most Linux distributions

This tutorial focuses on Vi commands, which are largely compatible with Vim.

Starting Vi

To start Vi, open a terminal and use the following commands:

  • Open a file: vi filename
  • Open a new file: vi newfile.txt
  • Open in read-only mode: view filename
  • Start Vim (if installed): vim filename

If the file exists, Vi opens it; otherwise, it creates a new file.

Modes in Vi

Vi operates in three main modes:

  1. Command Mode: Default mode for navigation and issuing commands. Most keys trigger actions (e.g., dd deletes a line).
  2. Insert Mode: For typing and editing text. Enter this mode with keys like i or a.
  3. Visual Mode: For selecting text blocks. Enter with v (character-wise), V (line-wise), or Ctrl+v (block-wise).

Switch between modes: - Command to Insert: i (insert before cursor), a (append after cursor), o (new line below) - Insert to Command: Esc - Command to Visual: v, V, or Ctrl+v - Visual to Command: Esc

Basic Navigation

In Command Mode, use these keys to move the cursor:

  • Arrow keys: Move up, down, left, right
  • h, j, k, l: Left, down, up, right (vim-preferred)
  • w: Move to the start of the next word
  • b: Move to the start of the previous word
  • 0: Move to the start of the line
  • $: Move to the end of the line
  • gg: Go to the first line
  • G: Go to the last line
  • :n: Go to line number n (e.g., :10 for line 10)

Pro Tip: Combine with numbers (e.g., 5j moves down 5 lines).

Editing Text

Enter Insert Mode to edit text: - i: Insert before cursor - a: Append after cursor - o: Open a new line below - O: Open a new line above - I: Insert at the start of the line - A: Append at the end of the line

In Command Mode, edit with: - x: Delete character under cursor - dw: Delete word - dd: Delete current line - D: Delete from cursor to end of line - r: Replace single character - R: Replace multiple characters (overwrite mode) - cw: Change word (delete word and enter Insert Mode)

Use numbers for repetition (e.g., 3dd deletes three lines).

Saving and Exiting

In Command Mode, use these commands (start with :): - :w: Save (write) the file - :w filename: Save as a new file - :q: Quit (if no changes made) - :q!: Quit without saving - :wq or ZZ: Save and quit - :x: Save and quit (only writes if changes were made)

Note: If you lack permissions, use :w! to force save (if possible).

Search and Replace

Searching

  • /pattern: Search forward for pattern
  • ?pattern: Search backward for pattern
  • n: Move to next match
  • N: Move to previous match

Example: /hello searches for “hello”. Use n to jump to the next occurrence.

Replacing

  • :%s/old/new/g: Replace all occurrences of old with new in the file
  • :%s/old/new/gc: Replace with confirmation for each occurrence
  • :s/old/new: Replace in the current line only

Example: :%s/foo/bar/g replaces all “foo” with “bar”.

Copy, Cut, and Paste

Vi uses “yank” for copying and “delete” for cutting: - yy: Yank (copy) current line - yw: Yank word - p: Paste after cursor - P: Paste before cursor - dd: Cut (delete) current line

Use numbers (e.g., 3yy copies three lines). In Visual Mode: - Select text with v, V, or Ctrl+v - y: Copy selection - d: Cut selection - p: Paste after cursor

Undo and Redo

  • u: Undo last change
  • Ctrl+r: Redo undone change
  • :u: Undo (alternative)

Note: Vi has a limited undo history; Vim supports more extensive undo.

Advanced Features

Multiple Files

  • Open multiple files: vi file1 file2
  • Switch files: :n (next), :p (previous)
  • List open files: :ls
  • Open another file: :e filename

Splitting Windows (Vim-specific)

  • :sp: Split horizontally
  • :vsp: Split vertically
  • Ctrl+w followed by arrow keys: Switch between windows

Macros

  • qa: Start recording macro to register a
  • Perform actions
  • q: Stop recording
  • @a: Replay macro
  • @@: Replay last macro

Command Repetition

  • .: Repeat the last change
  • Example: If you type dw to delete a word, . repeats it.

Customizing Vi

Vi and Vim can be customized via a configuration file: - For Vi: ~/.exrc - For Vim: ~/.vimrc

Example .vimrc:

set number          " Show line numbers
set tabstop=4       " Tabs are 4 spaces
set autoindent      " Enable auto-indentation
syntax on           " Enable syntax highlighting (Vim)

To apply settings in a session: - :set number: Enable line numbers - :set nonumber: Disable line numbers

Tips and Tricks

  • Jump to matching brace: Use % on {}, (), or []
  • Case-insensitive search: :set ignorecase
  • Highlight search results: :set hlsearch
  • Clear highlights: :noh
  • Execute shell command: :!command (e.g., :!ls)
  • Auto-complete (Vim): Ctrl+n or Ctrl+p in Insert Mode
  • Visual Block Mode: Ctrl+v for column editing (e.g., insert text across multiple lines)

Common Issues and Solutions

  1. Stuck in Insert Mode:
    • Press Esc to return to Command Mode.
  2. Permission Denied on Save:
    • Use :w! to force save (if you have sudo privileges, try :w !sudo tee %).
  3. Accidental Macro Recording:
    • Stop with q and avoid using @ until you clear the register.
  4. Lost Changes:
    • Check for swap files (e.g., .filename.swp). Recover with vi -r filename.
  5. Slow Performance:
    • Disable syntax highlighting (:syntax off) or use Vi instead of Vim for large files.

Conclusion

Vi is a versatile editor that rewards practice. Start with basic navigation and editing, then explore advanced features like macros and splits as needed. For more functionality, consider using Vim, which extends Vi with additional features.

For further learning: - Run vimtutor in the terminal for an interactive Vim tutorial. - Check the Vi/Vim man page: man vi or man vim. - Explore online resources like the Vim documentation or community forums.