Fundamentals 16 min read

Essential Vim Commands: A Comprehensive Guide for Efficient Editing

This guide presents a complete reference of Vim commands—including history navigation, file handling, mode switching, cursor movement, searching, replacing, undo/redo, copying, pasting, window management, macro recording, shell execution, commenting, and help—so users can master efficient text editing in Vim across Unix and Windows environments.

ITPUB
ITPUB
ITPUB
Essential Vim Commands: A Comprehensive Guide for Efficient Editing

Command History

Commands that start with : (Ex commands) or / (search) are stored in Vim's history. Press the up/down arrow keys after typing : or / to recall previous entries.

Launching Vim

Start Vim with vim. Open an existing file or create a new one with vim filename. Multiple files can be opened simultaneously, e.g. vim file1 file2 file3.

File and Buffer Commands

Open a new file in the current window: :edit file (alias :e file)

Open a file in a new split window: :split file or :vsplit file Switch to the next buffer: :bnext ( :bn)

Switch to the previous buffer: :bprevious ( :bp)

List all loaded buffers: :args (the current buffer is shown in brackets)

Edit a remote file, e.g. :e ftp://host/path/file.txt or a Windows share

:e \\server\share\file.txt

Vim Modes

Normal mode (press Esc or Ctrl+[) – the default editing mode. Insert mode (press i) – shows --INSERT-- in the status line. Visual mode (press v for characterwise, V for linewise) – shows --VISUAL--.

Navigation Commands

Bracket matching: % Basic cursor moves: h (left), j (down), k (up), l (right). A numeric prefix repeats the motion, e.g. 20j.

Word motions: 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 previous word).

Line motions: 0 (first character), ^ (first non‑blank), $ (end of line), gg (file start), G (file end).

Find a character on the current line: f{char} (forward), F{char} (backward). Example: fx jumps to the next x.

Jump to a specific line: :123 or 123G.

Scrolling shortcuts: Ctrl+e / Ctrl+y (one line), Ctrl+d / Ctrl+u (half‑screen), Ctrl+f / Ctrl+b (full screen).

Insert Commands

i

– insert before the cursor. I – insert at the beginning of the line. a – append after the cursor. A – append at the end of the line. o – open a new line below the current line and enter Insert mode. O – open a new line above the current line and enter Insert mode.

Search Commands

Forward search: /pattern. Backward search: ?pattern.

Repeat search: n (next), N (previous).

Case‑insensitive search: :set ignorecase. To make it case‑sensitive again: :set noignorecase.

Highlight all matches: :set hlsearch. Turn off highlighting: :set nohlsearch or :nohlsearch.

Incremental search (show matches while typing): :set incsearch.

Wrap around the file when the end/start is reached: :set wrapscan (default).

Search the word under the cursor: * (forward) or # (backward).

Replace (Substitution) Commands

Replace a single character: ra replaces the character under the cursor with a.

Substitute on the current line: s/old/new/ (first occurrence) or s/old/new/g (all occurrences).

Substitute in the whole file: %s/old/new/ (first per line) or %s/old/new/g (all).

Range‑limited substitution: :10,20s/^/ /g adds four spaces at the start of lines 10‑20.

Swap two adjacent lines: ddp.

Undo and Redo

Undo the last change with u. Undo all changes on the current line with U. Redo (undo the undo) with Ctrl+r.

Delete Commands

x

– delete the character under the cursor. 3x – delete three characters. X – delete the character before the cursor. dd – delete the current line. dj – delete the line below; dk – delete the line above. 10d – delete ten lines starting at the cursor. D or d$ – delete from the cursor to the end of the line.

Line‑range delete: :1,10d (lines 1‑10) or :1,$d (entire file).

Join two lines (remove the newline): J.

Copy (Yank) and Paste Commands

yy

– yank (copy) the current line. nyy – yank n consecutive lines. p – paste after the cursor (or on the line below if a whole line was yanked). P – paste before the cursor.

Copy a range of lines: :1,10co20 copies lines 1‑10 after line 20.

Duplicate the whole file at the end: :1,$co$.

Visual mode copy: press v (characterwise) or V (linewise), move to select text, then press y.

Swap current line with the next line: ddp. Swap adjacent characters: xp.

Cut Commands

In Visual mode, select text and press d to cut (the text is stored in the unnamed register). Range delete can also be performed with ndd (delete n lines) or :1,10d. Move lines with :1,10m20 (move lines 1‑10 after line 20).

Exit and Write Commands

Save and quit: :wq or ZZ.

Quit without saving: :q!.

Reload the file discarding unsaved changes: :e!.

Window Management

Horizontal split: :split or :new. Vertical split: :vsplit.

Switch windows: Ctrl+ww (next), Ctrl+wj (down), Ctrl+wk (up).

Close the current window: :close (cannot close the last window).

Close Vim entirely when only one window remains: :q.

Keep only the current window: :only.

Recording and Replaying Macros

Press q followed by a letter (e.g., qa) to start recording. Press q again to stop. Replay with @a. Macros cannot be nested.

Executing Shell Commands from Vim

Run an external command: :!command. Example: :!ls lists files in the current directory.

Check Perl syntax without leaving Vim: :!perl -c script.pl.

Execute a Perl script: :!perl script.pl.

Suspend Vim and return to the shell: :suspend or Ctrl+Z. Resume with fg.

Commenting in Source Files

For languages that use # for comments (e.g., Perl), you can comment lines with a substitution command. Examples:

Comment lines 3‑5: :3,5s/^/#/g.

Uncomment lines 3‑5: :3,5s/^#//g.

Comment the whole file: :%s/^/#/g or :1,$s/^/#/g.

Help System

Open the full help: :help or press F1.

Topic‑specific help: :help i, :help CTRL-[, :help 'number', :help <Esc>, etc.

Hyperlinks in help files appear between ||; follow with Ctrl+] and return with Ctrl+o (or Ctrl+t).

Other Useful Commands

Repeat the last command: ..

Check the value of an option: :set ruler? (useful for confirming settings in .vimrc).

List all sourced script files: :scriptnames.

Show invisible characters (tabs, spaces, end‑of‑line): :set list. Customize the display with set lcs=tab:> if needed.

Vim Tutor

Run the built‑in tutorial on Unix-like systems with vimtutor. On Windows, start it via :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.

Tutorialtext editorVim
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

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.