vim
Vi IMproved, a highly configurable text editor.
Modes
Vim is a modal editor. Understanding which mode you are in is crucial.
| Mode | Description |
|---|---|
| Normal | Default mode. Used for navigation and simple editing. Press Esc to return here from any mode. |
| Insert | For explicitly typing and modifying text. Press i to enter. |
| Visual | For highlighting/selecting text. Press v to enter. Use arrow keys to extend/reduce selection. |
| Command Line | For executing commands (Saving, Exiting, splitting). Press : from Normal mode to enter. |
Opening, Closing, and Saving
| Command | Description |
|---|---|
vim FILE_NAME | Open or create a file in Vim from your terminal. |
:w | Save file. |
:q | Quit. |
:wq or :x! | Save modifications and quit. |
:q! or :ZQ | Quit the file without saving (force quit). |
Navigation (Normal Mode)
| Key | Action |
|---|---|
h j k l | Left, Down, Up, Right (keeps hands on the home row). |
w | Jump forward to start of word. |
b | Jump backward to start of word. |
0 | Start of line. |
$ | End of line. |
gg | Go to first line. |
G | Go to last line. |
:LINE_NUMBER | Jump to a specific line number (e.g., :42). |
:$ | Jump to the last line (Command Line Mode equivalent to G). |
Basic Editing
| Key | Action |
|---|---|
i | Insert text before the cursor. |
a | Append text after the cursor. |
o | Insert a new line below current line and switch to Insert Mode. |
O | Insert a new line above current line and switch to Insert Mode. |
x | Delete character under the cursor. |
dd | Delete the current line or highlighted text. |
y or yy | Copy (yank) highlighted text or the current line. |
p | Paste previously copied/deleted text after cursor. |
r | Replace a single character. |
u | Undo last action. |
Ctrl + r | Redo last action. |
Search and Replace
| Command | Description |
|---|---|
/pattern | Search forward for pattern (e.g., :/SEARCH_KEYWORD). |
n | Jump to the next match. |
N | Jump to the previous match. |
:%s/old/new/g | Replace all occurrences of old with new throughout the file. |
Split Mode (Window Management)
Vim allows you to view multiple files simultaneously by splitting your terminal view.
| Command | Description |
|---|---|
:split FILE_NAME | Horizontally open another file while one is already open. |
:vsplit FILE_NAME | Vertically open another file while one is already open. |
Ctrl + w w | Jump between different split windows. |
:q | Close the currently active split window. |
Advanced Power-User Tricks
Combining Commands (The "Vim Language")
Vim commands act like a language (Verb + Noun).
d(Delete) +w(Word) =dw(Deletes the current word).c(Change) +w(Word) =cw(Deletes word and drops you into Insert mode).d(Delete) +$(End of line) =d$(Deletes everything from cursor to end of line).y(Yank) +p(Paste) +p=ypp(Duplicates the current line).
Visual Block Mode
Want to edit multiple lines at the exact same time?
- Press
Ctrl + vto enter Visual Block Mode. - Select multiple lines using
jork. - Press
Shift + I(Capital I), type your text (it will only appear on the first line). - Press
Escand the text will magically propagate to all selected lines!
Essential Configuration (~/.vimrc)
Create or edit ~/.vimrc to make Vim much more modern by default:
" Show line numbers (Default for navigation)
set number
" Show relative line numbers (Makes jumping with '10j' or '5k' incredibly easy)
" set relativenumber
" Enable syntax highlighting
syntax on
" Highlight search results dynamically as you type
set hlsearch
set incsearch
" Convert tabs to 4 spaces
set tabstop=4
set shiftwidth=4
set expandtab
" Enable mouse support for resizing splits and clicking cursor
set mouse=a