Master Vim: Essential Modes, Commands, and Tips for Linux Users
This guide introduces Vim's built‑in help, explains its multiple editing modes, and provides step‑by‑step commands for opening files, inserting text, navigating, deleting, searching, replacing, copying, exiting, and handling swap‑file protection on Linux systems.
Vim Help
Vim includes a comprehensive built‑in help system; after starting Vim type :help to open it.
Vim Modes
Vim operates in several distinct modes, each changing the meaning of typed keys.
Normal mode : default editing mode; commands are entered directly; press Esc to return from any other mode.
Command mode : accessed by typing :, / or ?; commands are executed after pressing Enter.
Insert mode : entered with i, a, o, etc.; typed characters are inserted into the file.
Visual mode : entered with v, V, or Ctrl‑V to select characters, lines, or blocks.
Select mode : similar to typical Windows editors; rarely used on Linux and omitted here.
Normal Mode
Command Mode
Insert Mode
i– insert at cursor I – insert at line start o – open new line below O – open new line above a – insert after cursor A – append at line end
Basic Editing Workflow
Opening a file
[root@localhost myfolder]# vim file1The screen shows a black block for the cursor, tildes (~) for empty lines, and a status line with file name, line count, and character count.
Inserting text
Press i to enter Insert mode, type the desired text, then press Esc to return to Normal mode. The status line shows --INSERT-- while in Insert mode.
Moving the cursor
In Normal mode use h (left), j (down), k (up), l (right). Arrow keys work but are slower.
Deleting text
Press x to delete the character under the cursor.
Press dd to delete the current line.
Press u to undo the last change; Ctrl‑R to redo.
Press U to undo all changes on the current line.
Other editing commands
o– open a new line below and switch to Insert mode. O – open a new line above and switch to Insert mode.
Exiting Vim
ZZ– save and quit. :q! – quit without saving. :e! – reload the file, discarding changes. :wq or :wq! – write changes and quit.
Jumping to a specific line
33G– move cursor to line 33. G – move to the last line. gg or 1G – move to the first line.
Searching
/pattern– search forward for pattern. ?pattern – search backward. n – repeat search in same direction; N – opposite direction.
Set ignorecase with :set ignorecase and disable with :set noignorecase.
Enable highlight with :set hlsearch and disable with :set nohlsearch or :nohlsearch.
Copying and pasting
Delete a line with dd then paste with p.
Use y to yank text into a register, then p to put it. yw – yank a word; y2w – yank two words. yy – yank a whole line; 3yy – yank three lines.
Replacing text
:s/UNIX/linux/– replace first occurrence of “UNIX” with “linux” on the current line. :s/UNIX/linux/g – replace all occurrences on the current line. :%s/UNIX/linux/g – replace throughout the entire file.
Vim’s Protection Mechanism
If Vim is terminated abruptly, a swap file .file.swp is left behind. On the next start Vim shows a warning like:
E325: Warning
Found swap file ".file.swp"
...The swap file protects against data loss and concurrent edits. Options include: (E) – edit the file. (R) – recover changes with :recover or vim -r file. (D) – delete the swap file. (Q) – quit without editing.
After recovery, delete the swap file to avoid future warnings. Note that Vim requires appropriate read/write permissions on the file being edited.
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.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
