Vim : The Exit Command
Date: 30 March 2025
Vim is a powerful command-line editor designed to boost your coding speed—even if you’re using it to write another editor within Vim itself.
This article will take you from struggling to exit Vim to customizing your own keybindings for quitting like a pro.
Table of Content
- Modes
Modes
Normal Mode
Cursor Movement
h: Move leftl: Move rightj: Move downk: Move up
Movement with Numbers
{number}h,{number}l→ Move left/right{number}times (e.g.,4hmoves 4 characters left){number}j,{number}k→ Move down/up{number}lines (e.g.,4jmoves 4 lines down)
Undo & Redo
u: UndoCtrl + r: Redo{number}u: Undo{number}times (e.g.,3uundoes 3 times){number}Ctrl + r: Redo{number}times
Searching
/X: Search forX, pressnto jump to the next occurrence,Shift + nto go back*: Search forward for the word under the cursor#: Search backward for the word under the cursor
Marking Points
mX: Mark the current position withX(whereXis any character)'X: Jump to the marked position
Other Useful Commands
zz: Center the current line in the window
Insert Mode
a: Insert after the characteri: Insert before the charactero: Insert on a new line below the current lineShift + a: Insert at the end of the lineShift + i: Insert at the beginning of the lineShift + o: Insert on a new line above the current line
Command Mode
- Press
:and enter the command (e.g.,:set numberto enable line numbers) :set relativenumber: Show relative line numbers:set mouse=a: Enable mouse support:%s/X/Y/g: Replace all occurrences ofXwithYin the entire file- To replace within a selected area in Visual Mode:
:s/X/Y/g
Configuration
- Location:
~/.vimrc
We can place any settings in ~/.vimrc to make them permanent.
set mouse=a
set number
set relativenumber
Visual Mode
- Press
vto enter Visual Mode d: Delete selectiondd: Delete the entire lineShift + d: Delete from cursor to end of the liney: Copy (Yank in Vim)yy: Yank the whole linep: Paste after cursorShift + p: Paste before cursor
Combining with Numbers
{number}p: Paste{number}times{number}d: Delete{number}lines
Change & Replace
c: Delete and enter Insert Modecc: Change the entire line (delete and enter Insert Mode)Shift + c: Delete from cursor to the end of the line and enter Insert ModerX: Replace the character under the cursor withX
Word and Line Navigation
w: Jump forward by one wordShift + w: Jump forward treating consecutive characters as a single worde: Jump to the end of the wordb: Jump backward by one word
Deleting Words
dw: Delete one wordd{number}w: Delete{number}words (e.g.,d2wdeletes two words)diw: Delete the word under the cursor
Line Navigation
0: Move to the beginning of the line$: Move to the end of the lineciX: Change the content inside an enclosure (e.g.,ci"to change inside" ")%: Jump between matching brackets ({},[],())tX: Jump just before the next occurrence ofXfX: Jump directly toXShift + tX: Jump beforeX
File Navigation
gg: Jump to the beginning of the fileShift + g: Jump to the end of the file{number}Shift + g: Jump to line{number}
Indentation & Repetition
=: Auto-indent.: Repeat the last command
Visual Line Mode
Shift + v: Select entire lines
Visual Block Mode
Ctrl + v: Select a block of text
Registers & Macros
Registers
:reg: Show all registers- Use
"<register><command>(e.g.,"appastes from registera)
Macros
qX: Start recording macro in registerXq: Stop recording@X: Execute macro stored in registerX- Macros can be nested by calling other macros inside them