Vim is a powerful and versatile text editor with numerous commands that can make editing more efficient. Here are some of the most useful commands for Vim:
- Basic movement:
- h, j, k, l: Move the cursor left, down, up, and right, respectively.
- w, b, e: Move the cursor forward (w) or backward (b) by words, or to the end (e) of a word.
- 0, ^, $: Move the cursor to the start of the line (0), to the first non-whitespace character (^), or to the end of the line ($).
- gg, G: Move the cursor to the start of the file (gg) or to the end of the file (G).
- :n, :n-: Jump to a specific line number ‘n’ or ‘n-‘ lines before the end of the file.
- Modes:
- i, a: Enter insert mode before (i) or after (a) the cursor position.
- I, A: Enter insert mode at the beginning (I) or end (A) of the line.
- v, V, Ctrl+v: Enter visual mode for character-wise (v), line-wise (V), or block-wise (Ctrl+v) selection.
- Esc: Exit insert or visual mode and return to normal mode.
- Editing:
- u, Ctrl+r: Undo (u) and redo (Ctrl+r) changes.
- y, d, p, P: Yank (copy) (y), delete (cut) (d), and put (paste) after (p) or before (P) the cursor.
- c: Change text by deleting and entering insert mode.
- r, R: Replace a single character (r) or multiple characters (R) by entering replace mode.
- x, X: Delete the character under (x) or before (X) the cursor.
- Search and replace:
- /, ?: Search forward (/) or backward (?) for a pattern.
- n, N: Move to the next (n) or previous (N) match in the search.
- :%s/pattern/replacement/g: Replace all occurrences of ‘pattern’ with ‘replacement’ throughout the file.
- File operations:
- :w, :w filename: Write (save) the file, or save it as ‘filename’.
- :q, :q!: Quit Vim; use ! to force quit without saving.
- :wq, :x, ZZ: Write and quit Vim.
- :e filename: Open another file named ‘filename’ for editing.
- Splits and tabs:
- :sp, :vsp: Split the window horizontally (:sp) or vertically (:vsp).
- Ctrl+w h/j/k/l: Move the cursor between split windows.
- :tabnew, :tabc: Create a new tab (:tabnew) or close the current tab (:tabc).
- gt, gT: Move to the next (gt) or previous (gT) tab.
These are just a few of the most useful Vim commands; there are many more to discover as you become more familiar with the editor.