Fundamentals 15 min read

Master Vim: Essential Commands and Tips for Efficient Editing

This comprehensive guide covers Vim’s command history, startup options, file navigation, editing modes, search and replace, movement shortcuts, undo/redo, copy‑paste, window management, macros, shell integration, commenting, help commands, and additional tips to boost productivity for developers.

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

Command History

Commands that start with : or / are stored in history; press the up/down arrows after typing : or / to browse previous entries.

Starting Vim

Run Vim from the terminal: vim – start Vim. vim filename – open or create filename.

File Commands

Open a single file: vim file. Open multiple files: vim file1 file2 file3 …. In Vim: :open file – open a new file in the current window. :split file – open a file in a new split window. :bn – go to the next buffer. :bp – go to the previous buffer. :args – list all opened files (current file is shown in brackets). :e ftp://192.168.10.76/abc.txt – edit a remote file via FTP. :e \\qadrive\test\1.txt – edit a network share.

Vim Modes

Normal mode (press Esc or Ctrl+[), Insert mode (press i), Visual mode (enter with v or V).

Navigation Commands

%

– jump to matching parenthesis. h, j, k, l – move left, down, up, right (repeatable with a count, e.g., 20j). w, b, e, ge – move by words. ^ – first non‑blank character of the line. 0 or Home – beginning of the line. $ – end of the line (e.g., 3$ moves to the end of the line three lines down). gg – go to the start of the file; G – go to the end. f<char> / F<char> – find next/previous occurrence of char on the line. : line Enter – jump to a specific line (e.g., :240). Ctrl+e / Ctrl+y – scroll down/up one line; Ctrl+d / Ctrl+u – scroll half‑screen; Ctrl+f / Ctrl+b – scroll full screen.

Insert Commands

i

– insert before cursor. I – insert at line start. a – append after cursor. A – append at line end. o – open a new line below. O – open a new line above.

Search Commands

/text

– forward search; ?text – backward search.

Press n for next match, N for previous. :set ignorecase / :set noignorecase – toggle case‑insensitive search. :set hlsearch / :set nohlsearch – highlight / clear matches. :set incsearch – incremental search. :set wrapscan – continue searching at file start/end.

Use * or # on a word under the cursor to search for it forward or backward.

Replace Commands

s/old/new/

– replace first occurrence in current line. s/old/new/g – replace all occurrences in current line. %s/old/new/ – replace first occurrence in each line. %s/old/new/g – replace all occurrences in the whole file. :10,20s/^/ /g – indent lines 10‑20 with four spaces. ddp – swap the current line with the line below.

Undo and Redo

u

– undo. U – undo all changes on the current line. Ctrl+r – redo.

Delete Commands

x

– delete character under cursor. 3x – delete three characters. X – delete character before cursor. dh – delete character to the left. dd – delete current line. 10d – delete ten lines starting from the cursor. D – delete to end of line. :1,10d – delete lines 1‑10; :1,$d – delete all lines. J – join the current line with the next (removes blank line).

Copy and Paste

yy

– yank (copy) current line. nyy – yank n lines. p – paste after cursor; P – paste before cursor. :1,10co20 – copy lines 1‑10 after line 20. :1,$co$ – duplicate the whole file at the end.

Enter Visual mode with v (characterwise) or V (linewise), move to select, then press y to copy. ddp – swap current line with the one below; xp – swap current character with the next.

Cut Commands

Enter Visual mode, select text, then press d to cut. ndd – cut n lines starting from the cursor. :1,10d – cut lines 1‑10. :1,10m20 – move lines 1‑10 after line 20.

Quit Commands

:wq

– write (save) and quit. ZZ – write and quit. :q! – quit without saving. :e! – reload the file, discarding changes.

Window Commands

:split

or :new – open a new horizontal window. :vsplit – open a vertical split. Ctrl+ww – cycle to the next window. Ctrl+wj / Ctrl+wk – move cursor to the window below / above. :close – close the 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, then replay with @ letter .

Executing Shell Commands

:!command

– run an external command. :!ls – list files in the current directory. :!perl -c script.pl – check Perl syntax without leaving Vim. :!perl script.pl – execute a Perl script. :suspend or Ctrl+Z – suspend Vim, return with fg.

Comment Commands

Prefix a line with # to comment it (in Perl scripts). 3,5s/^/#/g – comment lines 3‑5. 3,5s/^#//g – uncomment lines 3‑5. 1,$s/^/#/g or :%s/^/#/g – comment the whole file.

Help Commands

:help

or F1 – open the help window. :help i, :help CTRL-[ – get help for a specific command or key.

Use Ctrl+] on a highlighted tag to follow a link, Ctrl+o to return.

Other Non‑Editing Commands

.

– repeat the last change. :set ruler? – show if the ruler option is set. :scriptnames – list sourced script files. :set list – display invisible characters such as tabs and trailing spaces.

Vim Tutorial

On Unix: vimtutor. On Windows: :help tutor. Additional commands for syntax highlighting include :syntax, :syntax clear, :syntax case match, and :syntax case ignore.

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.

productivitycommand-linetext editorVimkeyboard shortcuts
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.