1. Basic Vi Commands:
Command | Description |
---|---|
i | Enter insert mode before the cursor. |
Esc | Exit insert mode and return to command mode. |
:wq or ZZ | Save changes and exit. |
:q! | Quit without saving changes. |
yy | Copy (yank) the current line. |
p | Paste the copied/yanked text after the cursor. |
2. Navigation:
Command | Description |
---|---|
h, j, k, l | Move left, down, up, and right, respectively. |
0 (zero) | Move to the beginning of the current line. |
$ | Move to the end of the current line. |
G | Move to the end of the file. |
gg | Move to the beginning of the file. |
:<line_number> | Move to a specific line. |
3. Editing Text:
Command | Description |
---|---|
x | Delete the character under the cursor. |
dd | Delete (cut) the current line. |
u | Undo the last change. |
Ctrl + r | Redo the last undone change. |
cw | Change word (delete from the cursor to the end of the word and enter insert mode). |
:%s/old/new/g | Replace all occurrences of ‘old’ with ‘new’ in the entire file. |
4. Searching and Replacing:
Command | Description |
---|---|
/pattern | Search forward for the specified pattern. |
?pattern | Search backward for the specified pattern. |
n | Move to the next occurrence of the search pattern. |
N | Move to the previous occurrence of the search pattern. |
:s/old/new | Replace the first occurrence of ‘old’ with ‘new’ on the current line. |
:s/old/new/g | Replace all occurrences of ‘old’ with ‘new’ on the current line. |
:1, $s/old/new/g | Replace all occurrences of ‘old’ with ‘new’ in the entire file. |
5. Saving and Exiting:
Command | Description |
---|---|
:w | Save changes without exiting. |
:q | Quit if no changes have been made. |
:wq or ZZ | Save changes and exit. |
:q! | Quit without saving changes. |
6. Visual Mode:
Command | Description |
---|---|
v | Enter visual mode to select characters. |
V | Enter visual mode to select whole lines. |
Ctrl + v | Enter visual block mode to select a rectangular block of text. |
7. Advanced Tips:
Command | Description |
---|---|
. (period) | Repeat the last command. |
:set number | Display line numbers. |
:set nonumber | Hide line numbers. |
:syntax on | Enable syntax highlighting. |
:syntax off | Disable syntax highlighting. |
8. Working with Multiple Files:
Command | Description |
---|---|
:e <filename> | Open a new or existing file. |
:bnext (or :bn) | Move to the next open buffer. |
:bprev (or :bp) | Move to the previous open buffer. |
:bd <buffer_number> | Close a specific buffer. |
:ls | List all open buffers. |
9. Copy and Paste Across Files:
Command | Description |
---|---|
:e <filename> | Open a new or existing file. |
:sp <filename> | Open a new file in a horizontal split. |
:vsp <filename> | Open a new file in a vertical split. |
Ctrl + w, w | Switch between split windows. |
Ctrl + w, h/j/k/l | Move to the left/down/up/right split window. |
:tabnew <filename> | Open a file in a new tab. |
gt or :tabnext | Move to the next tab. |
gT or :tabprev | Move to the previous tab. |
10. Macros:
Command | Description |
---|---|
q<letter> | Start recording a macro to register <letter>. |
q: | Stop recording the macro. |
@<letter> | Execute the macro recorded in register <letter>. |
@@ | Repeat the last executed macro. |
11. Marking and Navigation:
Command | Description |
---|---|
m<letter> | Set a mark at the current cursor position with <letter>. |
'<letter> | Move to the beginning of the line of the mark <letter>. |
<letter> | Move to the exact cursor position of the mark <letter>. |
:marks | Display a list of all marks. |
12. Working with Registers:
Command | Description |
---|---|
"ayy | Yank the current line into register ‘a’. |
"ap | Paste the contents of register ‘a’. |
:reg | Display the contents of all registers. |
13. Customizing Vi:
Command | Description |
---|---|
:set number | Display line numbers. |
:set nonumber | Hide line numbers. |
:set wrap | Enable line wrapping. |
:set nowrap | Disable line wrapping. |
:set ignorecase | Ignore case in searches. |
:set smartcase | Use case-sensitive search if pattern contains uppercase. |
14. Understanding Visual Block Mode:
Command | Description |
---|---|
Ctrl + v | Enter visual block mode to select a rectangular block of text. |
I | Insert text at the beginning of the selected block. |
A | Append text at the end of the selected block. |
r | Replace the selected block with a single character. |
15. Navigating Undo History:
Command | Description |
---|---|
:undo | Undo the last change. |
Ctrl + r | Redo the last undone change. |
:earlier <n>m | Go back <n> minutes in undo history. |
:later <n>m | Go forward <n> minutes in undo history. |