# Vim Commands 101: A Comprehensive Guide Vim is a highly configurable text editor built to enable efficient text editing. It's an improvement over the older Vi editor and is distributed with most UNIX systems. Vim operates in several modes, primarily Normal, Insert, and Visual. ## Understanding Vim Modes - **Normal Mode:** The default mode where you can run commands. You cannot insert text in this mode directly. - **Insert Mode:** Allows you to insert text into your file. - **Visual Mode:** Allows text selection for manipulation. ## Basic Navigation - **Move Cursor:** Use the `h` (left), `j` (down), `k` (up), and `l` (right) keys. - **Word Navigation:** `w` (next word), `b` (beginning of the word), `e` (end of the word). - **Line Navigation:** `0` (start of the line), `^` (first non-blank character), `
(end of the line). ## Mode Switching - **Enter Insert Mode:** `i` (insert before the cursor), `I` (insert at the beginning of the line), `a` (append after the cursor), `A` (append at the end of the line), `o` (open a new line below), `O` (open a new line above). - **Return to Normal Mode:** `Esc` or `Ctrl+[`. ## Editing - **Delete Character:** `x` (delete character under the cursor), `X` (delete character before the cursor). - **Delete Line:** `dd` (delete the whole line). - **Undo and Redo:** `u` (undo), `Ctrl+r` (redo). - **Copy and Paste:** `yy` (yank/copy line), `p` (paste after the cursor), `P` (paste before the cursor). ## Searching and Replacing - **Search:** `/pattern` (search for pattern), `n` (next match), `N` (previous match). - **Replace:** `:%s/old/new/g` (replace all occurrences of 'old' with 'new' in the file). ## File Operations - **Save File:** `:w` (write/save), `:w <filename>` (save as 'filename') - **Exit Vim:** `:q` (quit), `:q!` (quit without saving), `:wq` or `:x` (save and quit). ## Advanced Delete Commands - **Delete Word:** `dw` (delete word) - **Delete Line:** `dd` (delete line) - **Delete Selection:** `v` (enter visual mode), select text, then `d` (delete selection) - **Delete Multiple Lines:** `d` followed by a movement command (e.g., `d5j` to delete 5 lines down) - **Delete to End of File:** `dG` (delete from the cursor to the end of the file) - **Delete to Beginning of File:** `dgg` (delete from the cursor to the beginning of the file) - **Delete to End of Line:** `D` (delete from the cursor to the end of the line) - **Delete to Beginning of Line:** `d0` (delete from the cursor to the beginning of the line) - **Delete to End of Word:** `de` (delete from the cursor to the end of the word) - **Delete to Beginning of Word:** `db` (delete from the cursor to the beginning of the word) - **Delete Everything:** `dggdG` (delete everything in the file) ## Advanced Commands - **Split Windows:** `:split` (horizontal split), `:vsplit` (vertical split). - **Switch Windows:** `Ctrl+w w` (cycle through windows). - **Execute External Commands:** `:!command` (execute command in the shell). - **Replace every other blank space in a file:** `:g/^$/d ` (Useful for copying and pasting code) ## Customization and Learning More - **Open Vimrc File:** `:e $MYVIMRC` (edit Vim configuration file). - **Learn Vim:** `:help` (Vim's built-in help), `vimtutor` (tutorial for beginners). This guide covers the essentials you'll need to start navigating and editing with Vim. Vim's capabilities are vast, and mastering its more advanced features can make you significantly more productive in your text editing tasks.