What is a VIM Commands Cheat Sheet?
A Vim commands cheat sheet summarises keyboard commands used in Vim, a highly configurable text editor used in command line interfaces (CLI) and terminal emulators. Because Vim doesn’t rely on a graphical interface like many text editors, it uses keyboard commands for virtually all functions, from moving the cursor to selecting text and even exiting the program.
Vim is known for its steep learning curve, as its keyboard commands could be more intuitive for beginners. However, it is also praised for its efficiency once you get used to it. A cheat sheet can be helpful for both beginners trying to learn the basics and experienced users who need a reminder on lesser-used commands.
The commands in Vim can be broadly grouped into several categories:
- Modes: Vim is a modal editor with different modes for different tasks. This is foundational to how Vim operates. The primary modes are:
- Normal mode: This is the default mode when you open a file with Vim. It's used to navigate the file and execute commands.
- Insert mode: In this mode, you can insert or append text into the file. You typically enter insert mode with commands like `i` (insert), `a` (append), and `o` (open new line), and you can return to normal mode with `esc`.
- Visual mode: This is for selecting and manipulating blocks of text. You can enter visual mode with `v` (character-wise visual), `V` (line-wise visual), or `ctrl+v` (block-wise visual).
- Command-line mode: This is for entering editor commands. You enter this mode by typing : in normal mode.
- Movement or Navigation Commands: These commands move the cursor around the file. Examples are `h`, `j`, `k`, and l to move left, down, up, and right, respectively. Other navigation commands include `w` (next word), `b` (beginning of word), `0` (beginning of line), `$` (end of line), `gg` (beginning of file), and `G` (end of file).
- Edit Commands: These commands modify text. They include `i` (insert text), `a` (append text), `x` (delete character), `dd` (delete line), `cc` (change line), `cw` (change word), `s` (substitute character), and `u` (undo change).
- Yank, Delete, and Put (Copy, Cut, and Paste) Commands: These commands manipulate selected text. `yy` (yank line), `dd` (delete line), and `p` (put, or paste) are among the most common. You can yank or delete specific parts of a line, such as a word or from the cursor to the end of the line, using commands like `yw`, `dw`, `y$`, and `d$`.
- Search and Replace Commands: These commands help you find and substitute text. /text searches for "text", `n` and `N` navigate to the next and previous instances, respectively. `:%s/old/new/g` replaces all instances of "old" with "new" in the entire file.
- File and Window Commands: These commands deal with files and windows. For example, `:w` saves a file, `:q` quits Vim, `:wq` saves and quits, `:e` filename opens a file, and `:split`, `:vsplit`, `ctrl+w h/j/k/l` manage and navigate multiple windows.
- Macros and Registers: Vim also supports recording sequences of commands into macros and using registers for more advanced cut/copy/paste operations.
- Advanced Commands: Vim supports a wide range of advanced commands, including executing shell commands from within Vim (`:!command`), setting options (`:set option`), and using Vimscript for complex automation and customization.