Operations 14 min read

Master Vim: Essential Modes, Commands, and Tips for Linux Editing

This guide provides a comprehensive introduction to the Vim editor on Linux, covering its built‑in help, core modes, essential commands for inserting text, moving the cursor, deleting, searching, copying, replacing, exiting, and handling swap‑file protection, all illustrated with clear examples and images.

Raymond Ops
Raymond Ops
Raymond Ops
Master Vim: Essential Modes, Commands, and Tips for Linux Editing

This chapter shares the Vim editor in Linux, diving straight into the topic.

1. Vim Help

Vim includes a complete help system accessible by entering

:help

after launching Vim.

2. Vim Modes

Vim mode diagram
Vim mode diagram

Unlike most editors, characters typed in Vim are not inserted by default; the concept of modes is crucial. Vim has the following modes:

Normal mode – the default editing mode; press Esc from any other mode to return.

Command mode – for longer commands; enter : , / or ? from Normal mode, and finish with Enter .

Insert mode – for entering text; press i or a (among others) from Normal mode.

Visual mode – for selecting text blocks; v (character), V (line), or Ctrl‑V (block).

Select mode – similar to Windows editors; rarely used on Linux and omitted here.

1. Normal Mode

Normal mode illustration
Normal mode illustration

2. Command Mode

Command mode illustration
Command mode illustration

3. Insert Mode

i – insert at cursor.

I – insert at line start.

o – open a new line below cursor.

O – open a new line above cursor.

a – insert after cursor.

A – insert at line end.

3. Vim Examples

1. First Run Vim

<code>[root@localhost myfolder]# ll
总用量 4
-rw-r--r--. 1 root root 152 7月 27 23:28 file1
[root@localhost myfolder]# vim file1</code>

Opening

file1

shows a screen where the black block indicates the cursor, tildes (~) mark lines beyond the file, and the status line reports file name, line count, character count, and cursor position.

Vim screen
Vim screen

If the file is new, Vim displays a message indicating that the file is new.

<code>[root@localhost myfolder]# vim file</code>
New file screen
New file screen

2. Insert Text

Vim is a modal editor. In Normal mode every keystroke is a command; in Insert mode characters are inserted as text. Start Insert mode with

i

, then type freely. Press

Esc

to return to Normal mode.

Insert mode example
Insert mode example

Show the current mode with:

<code>:set showmode</code>
<code>:set noshowmode  // hide mode display</code>
Mode indicator
Mode indicator

3. Move Cursor

h – left

j – down

k – up

l – right

Arrow keys also work but reduce efficiency.

4. Delete Characters

x – delete character under cursor.

dd – delete the current line.

u – undo last change.

Ctrl‑R – redo.

U – undo all changes on the current line.

5. Other Editing Commands

Open a new line below with

o

(switches to Insert mode) or above with

O

(also switches to Insert mode).

6. Exit Vim

ZZ – save and quit.

:q! – quit without saving.

:e! – reload file, discarding changes.

:wq or :wq! – write and quit.

7. Specific Cursor Moves

G – go to line number before it (e.g., 33G ).

G – go to the last line.

gg – go to the first line (same as 1G ).

8. Simple Search

/string

searches forward;

n

jumps to next match,

3n

to the third match,

N

searches backward.

?

searches backward.

Ignore case:

<code>:set ignorecase   // enable ignore case
:set noignorecase // disable</code>

Highlight search results:

<code>:set hlsearch   // enable highlight
:set nohlsearch // disable
:nohlsearch    // clear current highlight</code>

9. Copy and Paste

Delete with dd then paste with p .

y copies to a register; p pastes.

yw – yank a word; y2w – yank two words.

yy – yank a line; 3yy – yank three lines.

10. Replace Characters

:s/UNIX/linux – replace first occurrence of UNIX on the current line.

:s/UNIX/linux/g – replace all occurrences on the current line.

:%s/UNIX/linux/g – replace all occurrences in the entire file.

4. Vim Protection Mechanism

If Vim is forced to quit, a swap file (e.g.,

.file.swp

) remains. On the next start you may see a warning like:

<code>E325: 注意
发现交换文件 ".file.swp"
所有者: root   日期: Sun Jul 28 19:43:04 2024
文件名: ~root/myfolder/file
修改过: 是
用户名: root   主机名: localhost.localdomain
进程 ID: 3884
正在打开文件 "file"
日期: Sun Jul 28 17:25:26 2024
(1) Another program may be editing the same file.
(2) An edit session for this file crashed.
使用 ":recover" 或 "vim -r file" 恢复。
删除交换文件 ".file.swp" 以避免再次出现此消息。</code>

The swap file prevents data loss from abrupt termination and avoids simultaneous edits. You can choose to (E)dit, (R)ecover, (D)elete the swap file, or (Q)uit.

Note: Vim requires appropriate read/write permissions; lacking them will cause errors.

LinuxCommand LineTutorialText EditorVim
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.