Vim Introduction Guide¶
Why Use Vim?¶
Vim (Vi Improved) is a powerful, highly configurable text editor that runs in the terminal. While it has a steep learning curve, mastering Vim can significantly boost your productivity, especially for coding and scientific computing. Here’s why it’s worth learning:
Efficiency: Edit text without leaving the keyboard—no mouse needed. Commands are designed for speed once memorized.
Terminal-Based: Works over SSH, on remote servers, or in environments without GUI (e.g., HPC clusters).
Lightweight: Minimal resource usage, fast startup, and runs everywhere Linux is installed.
Ubiquitous: Pre-installed on most Linux systems; essential for system administration and quick edits.
If you’re coding in Python, R, or C++, Vim can be your go-to editor with plugins like YouCompleteMe for autocompletion or NERDTree for file browsing. Start with basics, and you’ll wonder how you lived without it!
Command Mode¶
Vim starts in Command mode, where you navigate and manipulate text. Press Esc to return here from other modes.
Cursor Movements¶
Navigate efficiently without arrow keys:
h: Move left (one character)j: Move down (one line)k: Move up (one line)l: Move right (one character)0or^: Jump to beginning of line$: Jump to end of linew: Move forward one word (to start of next word)b: Move backward one worde: Move to end of current word
Tip
Combine with numbers, e.g., 5j moves down 5 lines. Use H (top of screen), M (middle), L (bottom) for screen-relative movement.
Editing Commands¶
Deletion¶
Delete text (acts like cut—can be pasted):
x: Delete character under cursordd: Delete entire linendd: Delete n lines (e.g.,3dddeletes 3 lines)dw: Delete word from cursorDord$: Delete from cursor to end of lined0: Delete from cursor to start of line
Examples:
d2w: Delete 2 wordsdG: Delete from cursor to end of file
Copy and Paste¶
yy: Yank (copy) entire lineyw: Yank wordy$: Yank to end of linep: Paste after cursorP: Paste before cursor
Examples:
y3w: Copy 3 wordsyyp: Duplicate current line
Undo and Redo¶
u: Undo last changeCtrl + r: Redo undone change:earlier 5m: Go back 5 minutes in edit history
Insert Mode¶
Switch to Insert mode to type text. Press i, a, o, etc., then type. Press Esc to return to Command mode.
i: Insert before cursora: Append after cursoro: Open new line below and insertO: Open new line above and insertI: Insert at beginning of lineA: Append at end of line:r filename: Insert contents of another file below current line
Tip
Use Ctrl + w in Insert mode to delete last word, Ctrl + u to delete entire line.
Visual Mode¶
Select text visually for operations. Press v to enter, then move cursor to select.
v: Character-wise visual modeV: Line-wise visual mode (selects whole lines)Ctrl + v: Block-wise visual mode (rectangular selection)
Once selected, use commands like d (delete), y (yank), > (indent), etc.
Examples:
Select lines 10-20:
Vthen move to line 20, thendto delete.Comment out code: Select block, then
:s/^/# /to add # to start of lines.
Search and Replace¶
Basic Search¶
/pattern: Search forward for pattern?pattern: Search backwardn: Next matchN: Previous match*: Search for word under cursor forward#: Search backward
Options: Add \c for case-insensitive (e.g., /pattern\c).
Replace¶
Syntax: :[range]s/old/new/[flags]
Range: Where to search/replace
%: Entire file.: Current linen: Line nn,m: Lines n to m'<,'>: Visual selection
Flags:
g: Global (all matches per line)c: Confirm each replacementi: Case-insensitive
Examples:
Replace all “old” with “new” in file:
:%s/old/new/gReplace in lines 5-10:
:5,10s/foo/bar/gConfirm replacements:
:%s/old/new/gc
Regular Expressions¶
Vim supports regex for powerful patterns. Use \( and \) for groups, \1 to reference in replace.
Common Metacharacters:
.: Any character*: 0 or more of previous\+: 1 or more\?: 0 or 1^: Start of line$: End of line\w: Word character ([a-zA-Z0-9_])\d: Digit[abc]: Any of a, b, c[^abc]: Not a, b, c
Examples:
Match email:
/[a-zA-Z0-9._%+-]\+@[a-zA-Z0-9.-]\+\.[a-zA-Z]\{2,\}Replace multiple spaces:
:%s/\s\+/ /gSwap words:
:%s/\(word1\) \(word2\)/\2 \1/g
Tip
Use :help regex for full reference. Practice on regex101.com.
Advanced Features¶
Macros¶
Record repetitive actions:
qa: Start recording macro ‘a’Perform actions
q: Stop recording@a: Play macro@@: Repeat last macro
Example: Record indenting lines: qa then >> then j then q, then @a to apply.
Splits and Tabs¶
:vsplit filename: Vertical split:split filename: Horizontal splitCtrl + wthen direction (h/j/k/l): Switch panes:tabnew: New tabgt: Next tab
Configuration (.vimrc)¶
Customize Vim in ~/.vimrc:
syntax on " Syntax highlighting
set number " Line numbers
set autoindent " Auto-indent
set tabstop=4 " Tab width
set expandtab " Use spaces instead of tabs
set incsearch " Incremental search
set hlsearch " Highlight matches
Tip
Install plugins with vim-plug or Vundle for more features.