Master Vim: Essential Modes, Commands, and Tips for Efficient Editing
This guide introduces Vim on Linux, explains its modal editing concepts, walks through common commands for navigation, insertion, deletion, searching, copying, and file management, and covers recovery mechanisms and permission considerations for safe editing.
Vim Introduction
Vim is a powerful modal text editor on Linux; you can access its full help documentation by entering :help after launching Vim.
Vim Modes
Vim operates in several distinct modes, each with its own behavior:
Normal mode : the default editing mode; press Esc to return here from any other mode.
Command mode : entered with :, / or ? from Normal mode; commands are executed after pressing Enter.
Insert mode : for text entry; enter with i, a or other insert‑related commands.
Visual mode : select text blocks; v for characterwise, V for linewise, Ctrl‑V for blockwise selection.
Select mode : similar to typical Windows editors; rarely used on Linux and omitted from further discussion.
Normal Mode Commands
h– move left j – move down k – move up l – move right x – delete the character under the cursor dd – delete the current line u – undo the last change Ctrl‑R – redo an undone change U – undo all changes on the current line
Insert Mode Commands
i– insert before the cursor I – insert at the beginning of the line o – open a new line below the current line O – open a new line above the current line a – insert after the cursor A – insert at the end of the line
Basic Editing Workflow
Start Vim with vim filename. The bottom status line shows the file name, line count, character count, and cursor position. Press i to enter Insert mode, type your text, then press Esc to return to Normal mode.
Cursor Navigation
In Normal mode you can move the cursor with h, j, k, l or the arrow keys (the latter are slower). Use G to jump to a specific line number (e.g., 33G) or to the end of the file; gg or 1G jumps to the first line.
Search and Replace
Search forward: /pattern, repeat with n (forward) or N (backward).
Search backward: ?pattern.
Ignore case: :set ignorecase (disable with :set noignorecase).
Highlight matches: :set hlsearch (disable with :set nohlsearch or :nohlsearch).
Replace on the current line: :s/old/new/ (add g to replace all occurrences).
Replace in the whole file: :%s/old/new/g.
Copy, Cut, and Paste
Delete a line with dd then paste with p.
Yank (copy) with y followed by a motion, e.g., yw (word), yy (line), 3yy (three lines).
Paste the yanked text with p.
Exiting Vim
ZZ– save and quit. :wq or :wq! – write and quit (force with !). :q! – quit without saving. :e! – reload the file, discarding changes.
Protection Mechanism (Swap Files)
If Vim is terminated abruptly, a swap file .filename.swp remains. On the next start you may see a warning like:
E325: Warning
Found swap file ".file.swp"
Owner: root Date: Sun Jul 28 19:43:04 2024
File name: ~root/myfolder/file
Modified: Yes
User name: root Host name: localhost.localdomain
Process ID: 3884
... (options to recover, delete, or quit)This mechanism prevents data loss and warns about concurrent edits. You can recover with :recover or vim -r file, or delete the swap file if it is no longer needed.
Permission Note
Vim requires appropriate read/write permissions on the files you edit; lacking these permissions will produce errors.
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.
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.
