Fundamentals 15 min read

Master Vim: Essential Commands for Efficient Editing

This guide presents a comprehensive collection of Vim commands—including file operations, navigation, editing, searching, replacing, window management, macros, and shell integration—explaining how to start Vim, use command history, switch modes, and efficiently edit text across multiple files.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Vim: Essential Commands for Efficient Editing

Command History

Commands that start with : or / are saved in history; type : or / followed by the up/down arrows to recall previous commands.

Starting Vim

Enter Vim from the command line: vim – launch Vim. vim filename – open Vim and create or edit filename.

File Commands

Open a single file: vim file Open multiple files: vim file1 file2 file3 ... Open a new file in the current Vim window: :open file Open a file in a new split window: :split file Switch to the next buffer: :bn Switch to the previous buffer: :bp List all open buffers (current file is shown in brackets): :args Open a remote file (e.g., FTP): :e ftp://192.168.10.76/abc.txt Open a Windows share file:

:e \qadrive\test\1.txt

Vim Modes

Normal mode – press Esc or Ctrl+[; file name shown at the bottom left.

Insert mode – press i; --INSERT-- displayed.

Visual mode – enter with v or V; --VISUAL-- displayed.

Navigation Commands

Bracket matching:

%

Insert Commands

i

– insert before the cursor. I – insert at the beginning of the line. a – insert after the cursor. A – insert at the end of the line. o – open a new line below. O – open a new line above.

Search Commands

Forward search: /text – find text; n for next, N for previous.

Backward search: ?text – reverse search; n and N work similarly.

Ignore case: :set ignorecase Do not ignore case: :set noignorecase Highlight matches: :set hlsearch Turn off highlighting: :set nohlsearch or :nohlsearch Incremental search: :set incsearch Wrap around search:

:set wrapscan

Replace Commands

Replace a character under cursor: ra – replace current character with a.

Replace in the current line: s/old/new/ – first occurrence. s/old/new/g – all occurrences.

Replace in the whole file: %s/old/new/ – first occurrence per line. %s/old/new/g – all occurrences.

Indent lines 10‑20 by four spaces:

:10,20 s/^/    /g

Movement Commands

Basic cursor moves: h – left, j – down, k – up, l – right.

Word moves: w – forward to start of next word, b – backward to start of previous word, e – forward to end of word, ge – backward to end of word.

Line start/end: ^ – first non‑blank character, 0 – column 0, $ – end of line.

File start/end: gg – top of file, G – bottom of file.

Find character in line: fx – forward to x, Fx – backward.

Jump to a specific line: :240 – go to line 240, or 240G.

Scrolling: Ctrl+e – scroll down one line, Ctrl+y – scroll up one line, Ctrl+d – half‑page down, Ctrl+u – half‑page up, Ctrl+f – full page down, Ctrl+b – full page up.

Undo and Redo

u

– undo, U – undo whole line, Ctrl+r – redo.

Delete Commands

x

– delete character under cursor, 3x – delete three characters, X – delete character before cursor. dd – delete current line, 10dd – delete ten lines. D – delete to end of line, d$ – same effect.

Range delete: :1,10d – delete lines 1‑10, :11,$d – delete from line 11 to end, :1,$d – delete entire file.

Join lines (remove blank line): J – join current line with next.

Copy and Paste

yy

– yank (copy) current line, nyy – yank n lines. p – paste after cursor, P – paste before cursor.

Copy a range of lines: :1,10 co 20 – copy lines 1‑10 after line 20.

Copy whole file to end:

:1,$ co $

Cut Commands

Visual mode ( v or V) then d cuts the selection. ndd – cut n lines starting from current line.

Move lines: :1,10 m 20 – move lines 1‑10 after line 20.

Exit Commands

:wq

or ZZ – write and quit, :q! – quit without saving, :e! – reload file discarding changes.

Window Commands

:split

or :new – open a new horizontal window. :vsplit – open a vertical split. Ctrl+ww – cycle to next window, Ctrl+wj – move to window below, Ctrl+wk – move to window above.

Close window: :close – close current window (cannot close the last one), :q – quit Vim if it’s the last window, :only – keep only the current window.

Recording Macros

Press q followed by a letter to start recording, press q again to stop; replay with @a (if recorded as qa).

Executing Shell Commands

Prefix with :!: :!ls – list directory, :!perl -c script.pl – check Perl syntax, :!perl script.pl – run Perl script.

Suspend Vim: :suspend or Ctrl+Z, then fg to resume.

Comment Commands

In Perl, # starts a comment. To comment lines in Vim: 3,5 s/^/#/g – comment lines 3‑5. 3,5 s/^#//g – uncomment lines 3‑5. 1,$ s/^/#/g or :%s/^/#/g – comment the whole file.

Help Commands

:help

or F1 – open help, :help i – help for insert mode, :help <Esc> – help for the Escape key.

Other Non‑Editing Commands

.

– repeat last command. :set ruler? – show if ruler is set. :scriptnames – list sourced script files. :set list – display invisible characters.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxcommand-line
MaGe Linux Operations
Written by

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.

0 followers
Reader feedback

How this landed with the community

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.