### Vi Modes Vi has different modes of operation: - Normal Mode (Command Mode): Default mode. Used for navigation, deletion, copying, pasting, and other commands. - Insert Mode: Used for inserting text into the file. Enter from Normal Mode using `i`, `a`, `o`, etc. Exit to Normal Mode with `Esc`. - Visual Mode: Used for selecting text blocks. Enter from Normal Mode with `v` (character), `V` (line), or `Ctrl+v` (block). - Command-Line Mode (Ex Mode): Used for advanced commands like saving, quitting, search/replace. Enter from Normal Mode with `:`. ### Entering Insert Mode From Normal Mode: - `i` : Insert text before the cursor. - `I` : Insert text at the beginning of the current line. - `a` : Append text after the cursor. - `A` : Append text at the end of the current line. - `o` : Open a new line below the current line and insert text. - `O` : Open a new line above the current line and insert text. ### Navigation (Normal Mode) - `h` : Move cursor left. - `j` : Move cursor down. - `k` : Move cursor up. - `l` : Move cursor right. - `w` : Move to the beginning of the next word. - `b` : Move to the beginning of the previous word. - `e` : Move to the end of the current word. - `0` (zero): Move to the beginning of the current line. - `^` : Move to the first non-blank character of the current line. - `$` : Move to the end of the current line. - `G` : Move to the last line of the file. - `gg` : Move to the first line of the file. - `nG` or `:n` : Move to line number `n`. - `Ctrl+f` : Page forward. - `Ctrl+b` : Page backward. ### Editing (Normal Mode) - `x` : Delete character under cursor. - `dw` : Delete from cursor to end of word. - `d$` : Delete from cursor to end of line. - `dd` : Delete current line. - `ndd` : Delete `n` lines. - `D` : Delete from cursor to end of line (same as `d$`). - `cw` : Change word (deletes word, enters Insert Mode). - `cc` : Change line (deletes line, enters Insert Mode). - `C` : Change from cursor to end of line (same as `c$`). - `r` : Replace single character under cursor. - `R` : Replace multiple characters (overwrite mode, press `Esc` to exit). - `u` : Undo last change. - `Ctrl+r` : Redo last undone change. - `.` : Repeat last command. ### Copy & Paste (Normal Mode) - `yy` : Yank (copy) current line. - `nyy` : Yank `n` lines. - `yw` : Yank word. - `y$` : Yank from cursor to end of line. - `p` : Put (paste) after cursor/line. - `P` : Put (paste) before cursor/line. ### Search & Replace (Command-Line Mode) - `/pattern` : Search forward for `pattern`. - `?pattern` : Search backward for `pattern`. - `n` : Repeat last search in the same direction. - `N` : Repeat last search in the opposite direction. - `:%s/old/new/g` : Replace all occurrences of `old` with `new` in the entire file. - `:%s/old/new/gc` : Replace all occurrences with confirmation `(c)` in the entire file. - `:n,ms/old/new/g` : Replace `old` with `new` globally between line `n` and line `m`. ### Saving & Exiting (Command-Line Mode) - `:w` : Save the file. - `:w filename` : Save the file as `filename`. - `:wq` or `ZZ` : Save and quit. - `:x` : Save and quit (only if changes were made). - `:q` : Quit (if no changes have been made). - `:q!` : Quit without saving (discard changes). - `:wqa` or `ZQ` : Save all open files and quit (in Vim). ### Visual Mode - `v` : Character-wise visual selection. - `V` : Line-wise visual selection. - `Ctrl+v` : Block-wise visual selection. - Once selected, you can use commands like `d` (delete), `y` (yank), `c` (change), `>` (indent), ` (unindent) on the selected text. ### Advanced Commands - `:set nu` : Show line numbers. - `:set nonu` : Hide line numbers. - `:set ic` : Ignore case in searches. - `:set noic` : Don't ignore case in searches. - `:r filename` : Insert content of `filename` below current line. - `:!command` : Execute shell `command`. - `J` : Join current line with the next one. - `xp` : Transpose two characters (delete, then paste after).