Master Vim: Essential Commands, Modes, and Tips for Efficient Text Editing
This guide introduces Vim, the powerful vi-derived text editor, covering its purpose, command syntax, common usage examples, the three operational modes with detailed key commands for navigation, editing, searching, replacing, and saving, plus practical tips and troubleshooting tricks.
Vim: Text Editor
Function Description:
Vim is a text editor derived from vi.
Vim provides programming editing capabilities, highlighting syntax with colors to aid development.
Command Syntax:
vim [options] [file]Reference Examples:
Example 1
Edit a specified file:
[root@cnLinuxer ~]# vim readme.txtExample 2
Edit a specified file starting from line 5:
[root@cnLinuxer ~]# vim +5 readme.txtExample 3
Open a file and position at the last line:
[root@cnLinuxer ~]# vim + readme.txtExample 4
Recover a file after a crash:
[root@cnLinuxer ~]# vim -r readme.txtKnowledge Summary
Vim three modes
Vim has three modes: Command mode, Ex mode (last‑line mode), and Insert mode.
The modes switch logic and command chart:
Command Mode
Command mode is Vim's default mode when a file is opened.
Basic operations include cursor movement, deletion, copying, pasting, changing, scrolling, and exiting.
1. Cursor movement
(1) Move to last line: G
(2) Move to first line: gg
(3) Move to a specific line: {number}G
(4) Move up/down by lines: {number}↑ or {number}↓
(5) Move to first non‑blank character of the line: ^
(6) Move to end of line: $
(7) Move to beginning of line: 0
(8) Move left one character: h
(9) Move right one character: l
(10) Move up one line: k
(11) Move down one line: j
2. Delete, copy, paste, undo
(1) Delete current character: x
(2) Delete current line: dd
(3) Delete n lines: ndd
(4) Copy current line: yy
(5) Paste after current line: p
(6) Delete from cursor to end of line: D
(7) Undo last action: u
3. Scrolling
(1) Scroll down a page: Ctrl+f
(2) Scroll up a page: Ctrl+b
(3) Scroll half‑page forward: Ctrl+d
(4) Scroll half‑page backward: Ctrl+u
4. Exit
(1) Quit without saving: :q!
(2) Save and quit: :wq!
(3) Save as a new file: :w a.txt
Ex Mode (Last‑line mode)
Press : in command mode to enter Ex mode.
Basic operations include saving, saving as, exiting, searching, and replacing.
1. Save
Command: :w (write)
Effect: Saves changes made to the file.
2. Save As
Command: :w {filepath}
Effect: Saves the file to the specified path.
3. Exit
(1) Quit without saving: :q!
(2) Save and quit: :wq!
(3) Save as a.txt: :w a.txt
4. Search
Command: / or ? followed by the search string ( / for forward, ? for backward ).
Example: /fail searches for the string “fail”.
Use n for next match, N for previous; matches are highlighted.
5. Replace
:s/man/woman – replace first occurrence of “man” with “woman” on the current line
:s/man/woman/g – replace all occurrences of “man” with “woman” on the current line
:2,5s/man/woman/g – replace “man” with “woman” from line 2 to line 5
:%s/man/woman/g – replace “man” with “woman” throughout the file
Insert Mode
Press i in command mode to enter insert mode.
In insert mode, the following keys are used:
Character keys and Shift combinations to input characters
Enter to create a new line
Backspace to delete the character before the cursor
Delete to delete the character after the cursor
Arrow keys to move the cursor
Home/End to move to line start/end
Page Up/Page Down to scroll pages
Insert to toggle between insert and replace mode
Esc to exit insert mode and return to command mode
Additional Common Commands
1. Show line numbers
In Ex mode, type :set nu to display line numbers temporarily; to make it permanent, add echo :set nu > .vimrc to the home directory.
2. Redo/Undo
Undo: :u in Ex mode.
Redo: Ctrl+r.
Multi‑step undo: {count}u.
3. Recover from abnormal closure
Delete the swap file associated with the edited file:
rm -f .data.txt.swp
# Note: swap files usually have the format .filename.swp4. Difference between :wq and :x
In practice, :x is recommended. :wq updates the file’s modification time regardless of changes, while :x updates it only when the file content actually changes.
Signed-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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
