Vim is a powerful text editor with a wide range of commands that can enhance productivity. Here are some of the top commands and keybindings you should know to work efficiently with Vim:
Basic Navigation
h, j, k, l: Move the cursor left, down, up, and right, respectively.
w: Move the cursor to the beginning of the next word.
b: Move the cursor to the beginning of the previous word.
e: Move the cursor to the end of the current word.
0: Move the cursor to the beginning of the line.
$: Move the cursor to the end of the line.
gg: Go to the beginning of the file.
G: Go to the end of the file.
Ctrl-f: Move forward one screen.
Ctrl-b: Move backward one screen.
Editing Text
i: Enter insert mode before the cursor.
a: Enter insert mode after the cursor.
o: Open a new line below the cursor and enter insert mode.
O: Open a new line above the cursor and enter insert mode.
x: Delete the character under the cursor.
dd: Delete the current line.
yy: Yank (copy) the current line.
p: Paste the yanked or deleted content after the cursor.
u: Undo the last change.
Ctrl-r: Redo the undone change.
cw: Change (replace) the word under the cursor.
ciw: Change the entire word under the cursor.
daw: Delete a word, including trailing space.
Visual Mode
v: Start visual mode (character-wise selection).
V: Start visual line mode (select entire lines).
Ctrl-v: Start visual block mode (column selection).
y: Yank (copy) the selected text.
d: Delete the selected text.
c: Change the selected text (delete and enter insert mode).
Search and Replace
/: Search forward for a pattern.
?: Search backward for a pattern.
n: Repeat the search in the same direction.
N: Repeat the search in the opposite direction.
:%s/old/new/g: Replace all occurrences of "old" with "new" in the file.
: Clear search highlighting.
File Management
:w: Save the current file (write changes).
:q: Quit Vim.
:wq: Save and quit Vim.
:q!: Quit without saving changes.
:e filename: Open or edit a specified file, replacing filename with the actual file name.
:x: Save and quit, similar to :wq.
:saveas filename: Save the current file with a new name, replacing filename with the desired new file name.
:bd: Close the current buffer without closing the window.
**:ls or :buffers: List all open buffers.
:bN: Switch to buffer number N, where N is the buffer number.
:set number: Display line numbers.
:set nonumber: Hide line numbers.
Window Management
:split or :sp: Split the current window horizontally, creating a new window above the current one.
:vsplit or :vsp: Split the current window vertically, creating a new window to the right of the current one.
Ctrl-w w: Switch between open windows.
Ctrl-w h: Move to the window to the left.
Ctrl-w j: Move to the window below.
Ctrl-w k: Move to the window above.
Ctrl-w l: Move to the window to the right.
Ctrl-w q: Close the current window.
Ctrl-w o: Close all other windows except the current one.
:resize N: Set the height of the current window to N lines.
:vertical resize N: Set the width of the current window to N columns.
Ctrl-w =: Make all windows equal height and width.
These commands and keybindings cover a wide range of basic and advanced functionalities in Vim. Learning and practicing them will significantly enhance your efficiency in using this versatile editor.
Comments