Master Vim Basics: Essential Modes, Commands, and Tips for Efficient Editing
This guide introduces Vim’s three operating modes, multiple file‑opening methods, essential command‑mode shortcuts for navigation, search‑replace, and editing, as well as insert‑mode and command‑line commands, providing practical tips and code examples for Linux operators to boost productivity.
Background
Vim is a popular, full‑screen text editor widely used for development, compilation, proofreading and other text‑related tasks. It offers powerful features such as auto‑completion, navigation, highlighting and repetition, enabling faster and more efficient editing.
1. Three Modes
Vim operates in three modes:
Command mode : perform delete, copy, paste and other shortcuts.
Insert mode : edit file content.
Command‑line mode : execute commands such as search, save and quit.
2. Three Ways to Open Files
1) Open a specific file
<code>vim /test/a.txt</code>Tip: type :wq to exit and save.
2) Open a file and move the cursor to a specific line
<code>vim +3 /test/a.txt</code>The cursor stops at line 3 (default is the first line).
3) Open a file and highlight a keyword
<code>vim +/root /test/a.txt</code>The string
rootis highlighted upon opening.
3. Command Mode
After opening a file, Vim starts in command mode, allowing cursor movement, copy‑paste, search‑replace and other operations.
Note: In command mode, keystrokes are interpreted as commands, not text input.
3.1 Cursor Movement
Command
Action
h or ←
Move cursor left one character
l or →
Move cursor right one character
k or ↑
Move cursor up one character
j or ↓
Move cursor down one character
0 or Ctrl+6 or Home
Move cursor to beginning of line
Ctrl+4 or End
Move cursor to end of line
Ctrl+f or PgUp
Scroll screen down one page
Ctrl+b or PgDn
Scroll screen up one page
gg
Go to first line of file
G
Go to last line of file
nG
Go to line number n
3.2 Search and Replace
Command
Action
/pattern
Search forward for
patternn
Find next occurrence forward
?pattern
Search backward for
patternN
Find next occurrence backward
:%s/word1/word2/g
Replace all
word1with
word2in the file
:%s/word1/word2/gc
Replace with confirmation
:1,10s/word1/word2/g
Replace
word1with
word2from line 1 to 10
3.3 Copy, Paste, Delete
Command
Action
x
Delete character under cursor
X
Delete character before cursor
dd
Delete (cut) current line
3dd
Delete three lines
yy
Yank (copy) current line
3yy
Yank three lines
p
Paste after cursor line
P
Paste before cursor line
u
Undo last change
Ctrl+r
Redo undone change
.
Repeat last command
Tip: Numeric prefixes repeat commands, e.g.,
10jmoves down ten lines,
2ppastes two lines,
2dddeletes two lines.
4. Insert Mode
Press
iin command mode to enter insert mode (displayed as – INSERT –). Other keys:
Command
Action
i
Enter insert mode at cursor
o
Open a new line below cursor and enter insert mode
R
Enter replace mode (overwrites existing text)
Esc
Exit insert/replace mode back to command mode
When the lower‑left corner shows – INSERT – or – REPLACE –, you are in edit mode; exit with
Escbefore performing other actions.
5. Command‑Line Mode
Press
:to enter command‑line mode.
Command
Action
:w
Save file
:q
Quit Vim
:wq
Save and quit
:q!
Quit without saving
:set nu
Show line numbers
:set nonu
Hide line numbers
:w filename
Save to a new file (Save As)
:2,5 w filename
Save lines 2‑5 to a new file
:r filename
Read file content and insert after cursor
Finally, keep a Vim cheat‑sheet image for quick reference.
Source: Adapted from the public account “入门小站”.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
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.