VIM is Linux’s powerhouse text editor, widely used in coding, system administration, and scripting. It offers deep customization, version control integration, and terminal-based efficiency—no GUI needed. Install it first if missing: in Debian/Ubuntu, run `sudo apt install vim`, on Arch `pacman -S vim`, or via package managers for other distros. The default VIM is "vi" (vi is a subset of VIM); this guide teaches full VIM features that vi lacks.

What You'll Need

  • A Linux terminal (or WSL on Windows)
  • VIM installed (see installation above)
  • Basic keyboard familiarity (arrow keys, Ctrl+Alt+F1–F7 for TTY switches)
  • Optional: A file to edit or create (e.g., `echo "test" > file.txt`)

Step-by-Step

  1. Open VIM: Launch with `vim filename` (replace `filename` with your file) or open an empty file with `vim -p` (prefix). Press i to enter "insert mode" (editing text). Exit by typing Esc to return to command mode, then : followed by `:wq` (write and quit) or `:q!` to force quit without saving. Key for success: Always press Esc at the start of a session to confirm you’re in command mode. Type :set nu to enable line numbers in command mode (press Esc first).
  2. Navigate:
    • Move up/down: j, k (like arrow keys)
    • Move left/right: h, l
    • Jump to start/end of line: 0 (start), $ (end)
    • Move to beginning/end of file: gg, G
    • Search forward/backward: /search_term (forward), ? (backward)
    Example: Search for "error" in a file: : /error Enter → hit n to move forward, N to go backward.
  3. Edit text:
    • Insert mode: i (before cursor), a (after cursor), o (add below line)
    • Delete text:
      • Delete 1 char: x
      • Delete word: dw
      • Delete line: dd (copy to kill ring), D (only forward)
    • Undo/redo: u (undo), Ctrl + r (redo)
    Example: Delete a line: j to go to the line, dd, then p to paste below.
  4. File management:
    • Save: Esc → : → :w (write)
    • Save + quit: Esc → : → :wq
    • Rename file: Esc → : → :r newname
    • Delete file: From the terminal, use `rm filename` (VIM can’t delete files directly; use `:w !rm %` as a last resort).
    Note: VIM’s `:w !rm %` command is dangerous—it forces deletion without confirmation. Use with caution.
  5. Basic formatting:
    • Center line: v (visual mode) → select line → : → !center
    • Trim whitespace: gg → : → :%s/^[[:space:]]\+// (remove leading spaces)
    • Convert case: v → select text → : → :%s/./\U\&/g (uppercase)
  6. Marks and bookmarks:
    • Create a mark (e.g., 'a'): ma (in insert mode)
    • Jump to mark: 'a
    • List marks: :'marks'
    Use case: Bookmark a section in a long file for later reference.

Common Mistakes

  • Forgetting to press Esc: VIM commands only work in command mode (after Esc). Pressing i or a switches to insert mode. Always verify with :.
  • Overwriting files: Use `:w` (write) instead of `:wq` (write and quit) after every save. The `:wq` command forces a write if the file isn’t modified.
  • Deleting critical data: VIM’s `:w !rm %` command is irreversible. Use `:w !cp % tmp/` to backup first, or delete files directly via the terminal.
  • Ignoring case sensitivity in search: Use `:set ignorecase` (case-insensitive search) or `:set smartcase` (smart case matching).
  • Not using normal/ex mode consistently: Mixing insert/exit mode (e.g., typing w while in insert mode) can lead to errors. Always return to command mode (`Esc`) after editing text.

Tips

  • Learn keybindings: Master these 10 commands:
    • j, k (up/down)
    • h, l (left/right)
    • i (insert), Esc (exit)
    • x (delete), dd (delete line)
    • p (paste)
    • gg (start), G (end)
    • : (command mode)
    • :w (save), :q (quit)
  • Use plugins: Install Vundle to manage plugins like Solarized for themes or NERD Tree for file navigation.
  • Customize: Create a `.vimrc` file in your home directory to define shortcuts (e.g., `nnoremap w :w`).
  • Learn macros: Record a macro and repeat it: qq (start recording), move cursor, p (paste).
  • Use CTRL + F/C: Forward/backward page jumps: Ctrl + f, Ctrl + b (like scroll wheel).

Troubleshooting

If VIM acts unexpectedly or commands fail:

  1. Check file permissions: Run `ls -l filename` in the terminal. If you lack write access, use `sudo vim filename` or contact your system admin. VIM may refuse to edit restricted files.
  2. Update VIM: Run `sudo apt update && sudo apt upgrade vim` (Debian/Ubuntu) or `pacman -Syu vim` (Arch). Outdated versions may lack features.
  3. Restart the terminal session: VIM sessions can "hang" if plugins or configs conflict. Try opening a new TTY (`Ctrl + Alt + F1–F7`), logging in, and reopening VIM.
  4. Verify terminal capabilities: Run `stty size` to check screen dimensions. Small terminals may truncate lines or commands. Increase size with `stty size 24 80` (24 rows, 80 columns).
  5. Debugging plugin issues: Temporarily disable plugins by editing your `.vimrc` and removing plugin-related lines. Test if the problem persists.
  6. Force quit and retry: If VIM freezes, press `Ctrl + z`, then `fg` to return to the terminal. If it’s unresponsive, kill the process with `pkill vim` and restart.
  7. Check for conflicting modes: If special characters (e.g., `~`, `#`) cause errors, ensure your terminal supports UTF-8. Run `echo $LANG` and adjust if needed (e.g., `LANG=en_US.UTF-8`).

Frequently Asked Questions

How do I replace text with VIM?

Use visual mode: Press v to enter visual mode, select the text, then type :. Replace with `:s/old/new/g`. Example: Replace "hello" with "hi" in all lines: v → select "hello" → : → :s/hello/hi/g.

Can I use VIM to edit system files?

Yes, but with caution. VIM can edit `.conf` files, log entries, or scripts. Always back up critical files first: Esc → :w !cp % ~/backup/`basename %`. Avoid modifying system directories (e.g., `/etc`) unless you understand the consequences.

Why does VIM not highlight syntax?

VIM’s syntax highlighting depends on plugins or built-in support. Enable it by adding to your `.vimrc`: syntax on. For advanced highlighting, install plugins like Solarized or NERD Tree.

How do I undo an unsaved modification?

VIM does not undo unsaved changes. Always save (`:w`) or quit (`:q!`) after making modifications. If you accidentally lost changes, restore from backup or use `git` with `git checkout -- ` if you track history.

How do I save a VIM session?

VIM does not save sessions. To return to a file later, open it again with `vim filename`. To persist changes (e.g., cursor position), use external tools like `tmux` or `screen`.

What’s the difference between vi and VIM?

VIM is an enhanced version of `vi`. Key differences:

  • Plugins: VIM supports third-party plugins (e.g., syntax highlighting, file managers).
  • Customization: VIM allows editing `.vimrc` for advanced users (e.g., keybindings, colors).
  • Features: VIM includes macros, smart auto-completion, and better performance with large files.
If you only need basic editing, `vi` suffices. For power users, VIM is indispensable.