You are currently viewing Vi Linux Cheat Sheet : the Command Line Text Editor

Vi Linux Cheat Sheet : the Command Line Text Editor

1. Basic Vi Commands:

CommandDescription
iEnter insert mode before the cursor.
EscExit insert mode and return to command mode.
:wq or ZZSave changes and exit.
:q!Quit without saving changes.
yyCopy (yank) the current line.
pPaste the copied/yanked text after the cursor.

2. Navigation:

CommandDescription
h, j, k, lMove left, down, up, and right, respectively.
0 (zero)Move to the beginning of the current line.
$Move to the end of the current line.
GMove to the end of the file.
ggMove to the beginning of the file.
:<line_number>Move to a specific line.

3. Editing Text:

CommandDescription
xDelete the character under the cursor.
ddDelete (cut) the current line.
uUndo the last change.
Ctrl + rRedo the last undone change.
cwChange word (delete from the cursor to the end of the word and enter insert mode).
:%s/old/new/gReplace all occurrences of ‘old’ with ‘new’ in the entire file.

4. Searching and Replacing:

CommandDescription
/patternSearch forward for the specified pattern.
?patternSearch backward for the specified pattern.
nMove to the next occurrence of the search pattern.
NMove to the previous occurrence of the search pattern.
:s/old/newReplace the first occurrence of ‘old’ with ‘new’ on the current line.
:s/old/new/gReplace all occurrences of ‘old’ with ‘new’ on the current line.
:1, $s/old/new/gReplace all occurrences of ‘old’ with ‘new’ in the entire file.

5. Saving and Exiting:

CommandDescription
:wSave changes without exiting.
:qQuit if no changes have been made.
:wq or ZZSave changes and exit.
:q!Quit without saving changes.

6. Visual Mode:

CommandDescription
vEnter visual mode to select characters.
VEnter visual mode to select whole lines.
Ctrl + vEnter visual block mode to select a rectangular block of text.

7. Advanced Tips:

CommandDescription
. (period)Repeat the last command.
:set numberDisplay line numbers.
:set nonumberHide line numbers.
:syntax onEnable syntax highlighting.
:syntax offDisable syntax highlighting.

8. Working with Multiple Files:

CommandDescription
: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.
:lsList all open buffers.

9. Copy and Paste Across Files:

CommandDescription
: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, wSwitch between split windows.
Ctrl + w, h/j/k/lMove to the left/down/up/right split window.
:tabnew <filename>Open a file in a new tab.
gt or :tabnextMove to the next tab.
gT or :tabprevMove to the previous tab.

10. Macros:

CommandDescription
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:

CommandDescription
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>.
:marksDisplay a list of all marks.

12. Working with Registers:

CommandDescription
"ayyYank the current line into register ‘a’.
"apPaste the contents of register ‘a’.
:regDisplay the contents of all registers.

13. Customizing Vi:

CommandDescription
:set numberDisplay line numbers.
:set nonumberHide line numbers.
:set wrapEnable line wrapping.
:set nowrapDisable line wrapping.
:set ignorecaseIgnore case in searches.
:set smartcaseUse case-sensitive search if pattern contains uppercase.

14. Understanding Visual Block Mode:

CommandDescription
Ctrl + vEnter visual block mode to select a rectangular block of text.
IInsert text at the beginning of the selected block.
AAppend text at the end of the selected block.
rReplace the selected block with a single character.

15. Navigating Undo History:

CommandDescription
:undoUndo the last change.
Ctrl + rRedo the last undone change.
:earlier <n>mGo back <n> minutes in undo history.
:later <n>mGo forward <n> minutes in undo history.

Leave a Reply