Master Vim: Essential Commands, Modes, and Tips for Efficient Editing
Learn the core concepts of Vim, including its editing, insert, and command-line modes, essential navigation shortcuts, file operations, visual selection, multi-file handling, and customization options, with clear examples and code snippets to boost your text editing efficiency.
Vim is a text editor compatible with vi, used for editing plain text and especially useful for programming.
Basic Modes
Editing mode: default mode when opening a file
Insert mode:
Command-line mode: built-in command line interfaceBasic Usage
# vim [options] [file ...]
# Open a file and place the cursor at the beginning of line #
+/PATTERN: Open a file and place the cursor at the first line that matches PATTERNMode Switching
Editing mode (default)
Editing mode → Insert mode:
i : insert at cursor position
a : append after cursor
o : open a new line below cursor
I : insert at beginning of current line
A : append at end of current line
O : open a new line above cursor
Insert mode → Editing mode:
ESC
Editing mode → Command-line mode:
:
Command-line mode → Editing mode:
ESCClosing Files
1. In editing mode:
ZZ : save and quit
ZQ : quit without saving
2. In command-line mode:
:q quit
:q! force quit without saving
:wq write and quit
:w write (save)
:x write and quit (same as :wq)
:w /PATH/TO/SOMEFILE write to specified fileCommand Mode Cursor Navigation
Character-wise Movement
h : left
j : down
k : up
l : right
#COMMAND : move # characters in the specified directionWord-wise Movement
w : move to start of next word
e : move to end of current or next word
b : move to start of current or previous word
#COMMAND : move # words in the specified directionLine Start/End Movement
^ : jump to first non‑blank character of the line
0 : jump to column 0 (line start)
$ : jump to end of lineLine Jump
#G : jump to line number #
1G, gg : first line
G : last lineSentence Jump
) : next sentence
( : previous sentenceParagraph Jump
} : next paragraph
{ : previous paragraphScreen Scrolling
Ctrl+f : scroll forward one screen
Ctrl+b : scroll backward one screen
Ctrl+d : scroll forward half screen
Ctrl+u : scroll backward half screen
Enter : scroll down line by lineCurrent Screen Position
H : top of screen
M : middle of screen
L : bottom of screen
zt : place cursor line at top of screen
zz : place cursor line at middle of screen
zb : place cursor line at bottom of screenVim Editing Commands
Character Editing
Default mode:
x : delete character under cursor
#x : delete # characters starting at cursor
xp : swap character under cursor with the next one
r : replace character under cursor (rCHAR to replace with CHAR)
~ : toggle case of character under cursorDeletion Commands
d : delete (can be combined with motion)
d$ : delete to end of line
d^ : delete to first non‑blank character of line
d0 : delete to start of line
dw : delete to start of next word
de : delete to end of current/next word
db : delete to start of current/previous word
D : same as d$
dd : delete current line
#dd : delete # lines starting from current linePaste Commands (p, put, paste)
p : paste after cursor (or below current line if yanked whole line)
P : paste before cursor (or above current line if yanked whole line)Yank (Copy) Commands
y : yank (copy) similar to d
y$ : yank to end of line
y^ : yank to first non‑blank character of line
y0 : yank to start of line
ye : yank to end of current/next word
yw : yank to start of next word
yb : yank to start of current/previous word
yy : yank whole line
#yy : yank # linesChange Commands (c)
c : change (delete then enter insert mode)
c$ : change to end of line
c^ : change to first non‑blank character of line
c0 : change to start of line
cb : change to start of current/previous word
ce : change to end of current/next word
cw : change to start of next word
cc : change whole line
#cc : change # linesOther Editing Operations
Visual Mode
v : characterwise visual selection
V : linewise visual selection
d, c, y can be used after selection to delete, change, or yank
u : undo last change (or #u to undo # changes)
Ctrl+r : redo undone changes
. : repeat last change
vimtutor : start Vim tutorialVim Command-line Mode
(1) Address Ranges
:start_pos[,end_pos]
#: specific line number (e.g., 5 for line 5)
. : current line
$ : last line
#,# : range from line # to line #
#,+# : range from line # to line # plus offset (e.g., 3,+7)
.,$-1 : from current line to second‑last line
1,$ : whole file
% : whole file (same as 1,$)
/pattern/ : first line matching pattern after cursor
/first/,$ : from first match to end of file
/pat1/,/pat2/ : from first match of pat1 to first match of pat2
Commands can follow a range, e.g., d to delete, y to yank, c to change, w /PATH/ to write range to file, r /PATH/ to read file into range(2) Search
/PATTERN : search forward for pattern
?PATTERN : search backward for pattern
n : repeat search in same direction
N : repeat search in opposite direction(3) Search and Replace
s/old/new/flags
old : pattern to find (regular expressions allowed)
new : replacement text (no regex, but can use back‑references)
flags:
i : ignore case
g : replace all occurrences in the line
Alternative delimiters can be used, e.g., s@@@ or s###
Example:
%s@<t[[:alpha:]]\+>@T\1@g
%s@<t[[:alpha:]]\+>@&er@gVim Multi‑File Features
Multiple Files
vim FILE1 FILE2 ...
:next : go to next file
:prev : go to previous file
:first : go to first file
:last : go to last file
:wqall : write all files and quit
:wall : write all files
:qall : quit all filesMultiple Windows
-o : split windows horizontally
-O : split windows vertically
Ctrl+w ARROW : move between windows
Ctrl+w s : split current window horizontally
Ctrl+w v : split current window verticallyCustomizing Vim
Settings in command‑line mode affect only the current Vim session.
Permanent settings:
Global: /etc/vimrc
User: ~/.vimrc
Common options:
set number (or set nu) : show line numbers
set nonumber (or set nonu) : hide line numbers
set showmatch (set sm) : highlight matching brackets
set noshowmatch (set nosm) : disable bracket highlight
set autoindent (set ai) : enable automatic indentation
set noautoindent (set noai): disable automatic indentation
set hlsearch : highlight search results
set nohlsearch : disable search highlight
syntax on : enable syntax highlighting
syntax off : disable syntax highlighting
set ignorecase (set ic) : ignore case in searches
set noignorecase (set noic) : case‑sensitive searches
:help : open help
:help {subject} : open help for a specific subjectSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
