What it is
Vim is a modal text editor that allows for efficient text manipulation through a command-line interface, ideal for programmers and system administrators who spend a lot of time editing code or configuration files.
Installation
Linux
sudo apt update && sudo apt install vim # Debian/Ubuntu
sudo yum install vim # CentOS/Fedora/RHEL
sudo dnf install vim # Fedora
macOS
brew install vim
Windows
Vim is often included with Git for Windows. You can also download installers from the official Vim website or use package managers like Chocolatey:
choco install vim
Core Concepts
-
Modes: Vim has several modes, the most important being:
- Normal Mode: The default mode. Used for navigation and executing commands. Press
Escto return to Normal Mode from any other mode. - Insert Mode: For typing text. Press
i(insert before cursor),a(append after cursor),I(insert at beginning of line), orA(append at end of line) to enter Insert Mode. - Visual Mode: For selecting text. Press
v(character-wise),V(line-wise), orCtrl+v(block-wise) to enter Visual Mode. - Command-Line Mode: For executing ex commands (like
:w,:q). Press:to enter Command-Line Mode.
- Normal Mode: The default mode. Used for navigation and executing commands. Press
-
Motions: Commands that move the cursor (e.g.,
wto next word,$to end of line). -
Text Objects: Selections based on semantic units (e.g.,
iwfor inner word,apfor a paragraph). -
Operators: Actions that affect text (e.g.,
dfor delete,cfor change,yfor yank/copy). Operators are often combined with motions or text objects.
Commands / Usage
Entering and Exiting Vim
vim <filename>: Open or create a file.vim: Open Vim without a file.:q: Quit (only if no changes have been made).:q!: Quit without saving changes.:w: Save (write) changes.:wq: Save and quit.:x: Save and quit (similar to:wq, but only writes if changes were made).:qa: Quit all open windows/buffers.:wqa: Write all open windows/buffers and quit.
Navigation (Normal Mode)
h,j,k,l: Move left, down, up, right.w: Move to the start of the next word.b: Move to the start of the previous word.e: Move to the end of the current/next word.0(zero): Move to the beginning of the line.^: Move to the first non-blank character of the line.$: Move to the end of the line.gg: Move to the very beginning of the file.G: Move to the very end of the file.<N>G: Move to line number<N>(e.g.,10Gmoves to line 10).%: Move to the matching bracket/parenthesis/brace.Ctrl+f: Scroll down one page (forward).Ctrl+b: Scroll up one page (backward).Ctrl+d: Scroll down half a page.Ctrl+u: Scroll up half a page.zz: Center the screen on the current line.
Editing (Normal Mode - Operators + Motions/Text Objects)
i: Enter Insert Mode before the cursor.a: Enter Insert Mode after the cursor.I: Enter Insert Mode at the beginning of the line.A: Enter Insert Mode at the end of the line.o: Open a new line below the current line and enter Insert Mode.O: Open a new line above the current line and enter Insert Mode.r<char>: Replace the character under the cursor with<char>.R: Enter Replace Mode (overtype characters).x: Delete the character under the cursor.X: Delete the character before the cursor.dd: Delete the current line.<N>dd: Delete<N>lines starting from the current line (e.g.,5dd).dw: Delete from the cursor to the end of the current word.db: Delete from the cursor to the beginning of the current word.d$: Delete from the cursor to the end of the line.d0: Delete from the cursor to the beginning of the line (before the first character).d^: Delete from the cursor to the first non-blank character of the line.D: Delete from the cursor to the end of the line (d$).yy: Yank (copy) the current line.<N>yy: Yank<N>lines starting from the current line.yw: Yank the current word.y$: Yank to the end of the line.Y: Yank the current line (yy).p: Paste after the cursor/line.P: Paste before the cursor/line.c<motion>: Change text (delete and enter Insert Mode). Example:cwchanges the current word.C: Change to the end of the line (c$).cc: Change the current line (c<motion>for the whole line).u: Undo the last change.Ctrl+r: Redo the last undone change..(period): Repeat the last change.J: Join the current line with the next line.~: Swap case of the character under the cursor.gU<motion>: Make text uppercase. Example:gUwmakes the current word uppercase.gu<motion>: Make text lowercase. Example:guwmakes the current word lowercase.
Text Objects (Used with Operators)
i<text object>: Inner text object (e.g.,diwdeletes the inner word).a<text object>: Around text object (includes surrounding characters, e.g.,dawdeletes a word and the space after it).
Common text objects:
w: WordW: WORD (space-delimited)s: Sentencep: Paragraph(or): Parenthesized text{or}: Curly-braced text[or]: Bracketed text": Double-quoted text': Single-quoted text<: Angle-bracketed text
Visual Mode
v: Enter Visual Mode (character-wise).V: Enter Visual Line Mode (line-wise).Ctrl+v: Enter Visual Block Mode.- (In Visual Mode)
y: Yank the selected text. - (In Visual Mode)
d: Delete the selected text. - (In Visual Mode)
c: Change the selected text. - (In Visual Mode)
>: Indent the selected lines. - (In Visual Mode)
<: Unindent the selected lines. - (In Visual Mode)
u: Change selection to lowercase. - (In Visual Mode)
U: Change selection to uppercase.
Search and Replace (Command-Line Mode)
/pattern: Search forward forpattern.?pattern: Search backward forpattern.n: Move to the next match (in the direction of the last search).N: Move to the previous match.:%s/old/new/g: Replace all occurrences ofoldwithnewin the entire file.%: Range (entire file).s: Substitute command.g: Global flag (replace all on a line, not just the first).
:%s/old/new/gc: Replace all occurrences, prompting for confirmation each time.:<range>s/old/new/g: Replace within a specific range.10,20s/old/new/g: Replace on lines 10 through 20..,+5s/old/new/g: Replace on the current line and the next 5 lines.'/start_pattern/','/end_pattern/s/old/new/g': Replace between lines matching start/end patterns.
Window Management
:split <filename>: Split the current window horizontally.:vsplit <filename>: Split the current window vertically.Ctrl+wfollowed byh,j,k,l: Move between split windows.Ctrl+wfollowed byworCtrl+w Ctrl+w: Cycle through windows.Ctrl+wfollowed byr: Rotate split windows clockwise.Ctrl+wfollowed byR: Rotate split windows counter-clockwise.Ctrl+wfollowed by_: Maximize the current window height.Ctrl+wfollowed by|: Maximize the current window width.Ctrl+wfollowed bycor:close: Close the current window.Ctrl+wfollowed byoor:only: Close all windows except the current one.:tabnew <filename>: Open a new tab.gt: Go to the next tab.gT: Go to the previous tab.<N>gt: Go to tab number<N>.:tabclose: Close the current tab.
Buffers
:lsor:buffers: List all open buffers.:b <buffer_number>: Switch to buffer number<buffer_number>.:b <buffer_name>: Switch to buffer with a name containing<buffer_name>.:bn: Switch to the next buffer.:bp: Switch to the previous buffer.:bd: Delete (close) the current buffer.
Folding
zf: Create a fold.zo: Open a fold.zc: Close a fold.zr: Open all folds recursively.zm: Close all folds recursively.zj: Move to the next fold boundary.zk: Move to the previous fold boundary.
Macros
q<register>: Start recording a macro into<register>(e.g.,qarecords into registera).- (Perform actions)
q: Stop recording.@<register>: Play back the macro in<register>(e.g.,@a).@@: Repeat the last executed macro.
Shell Commands
:!command: Execute<command>in the shell.:r !command: Read the output of<command>into the current buffer.Ctrl+vfollowed byjork: Insert literal control characters.
Configuration (~/.vimrc)
set number: Display line numbers.set relativenumber: Display relative line numbers.set autoindent: Automatically indent new lines.set smartindent: Smarter auto-indenting.set tabstop=4: Set tab width to 4 spaces.set shiftwidth=4: Set indentation width to 4 spaces.set expandtab: Use spaces instead of tabs.set hlsearch: Highlight search matches.set incsearch: Show search matches incrementally as you type.set syntax on: Enable syntax highlighting.set mouse=a: Enable mouse support in all modes.set wrap: Enable line wrapping (default).set nowrap: Disable line wrapping.
Common Patterns
-
Copying a line and pasting it 5 times:
5yyP(Yank 5 lines, then paste below the current line)
-
Deleting all lines except the first 10:
11G dG(Go to line 11, delete from current line to the end)
-
Replacing all instances of "foo" with "bar" in the current file:
:%s/foo/bar/g -
Indenting multiple lines selected in Visual Mode: (Enter Visual Mode with
Von the first line, move down to the last line, then press>) -
Executing a command on lines containing a pattern:
:/pattern/normal dd(Find lines matching
patternand delete them) -
Saving and exiting from anywhere:
:w!<CR>:q!<CR>(Write forcefully, then quit forcefully.
<CR>is Enter/Return) -
Finding a file and opening it in a split window:
:sp **/somefile.txt(Uses globbing to find the file)
-
Quickly renaming a word under the cursor:
ciwnewword<Esc>(Change Inner Word to newword, then exit Insert Mode)
Gotchas
- Accidentally typing commands in Insert Mode: Always remember to press
Escto return to Normal Mode before executing commands. - Forgetting to save: Vim doesn’t auto-save. Use
:wor:wqregularly. - Mode confusion: Especially for beginners, getting stuck in Insert Mode or Command-Line Mode and not knowing how to get back to Normal Mode is common.
Escis your friend. - Case sensitivity in searches: By default, searches are case-sensitive. Use
\cin the search pattern (e.g.,/pattern\c) for case-insensitive search, or setignorecaseandsmartcasein your.vimrc. - Yanking vs. Deleting: Both
dandyplace text into registers (buffers).ddeletes and yanks,yonly yanks.ppastes from the "unnamed" register, which is usually the last deleted or yanked text. Ctrl+vin terminal vs. Vim: In many terminals,Ctrl+vis used for special character insertion. In Vim’s Normal Mode,Ctrl+venters Visual Block mode. If you want to insert a literalCtrl+vin Vim’s Insert Mode, you might need to pressCtrl+q(depending on terminal configuration).- The
.command: It repeats the last change. If your last change was typing "hello",.will type "hello". If your last change was deleting a line,.will delete a line. This can be powerful but sometimes surprising.