Fundamentals 15 min read

Master Vim: Essential Commands and Tips for Efficient Editing

This comprehensive Vim guide explains how to start the editor, manage files, navigate, edit, search, replace, undo, copy, paste, work with windows, record macros, run shell commands, comment code, access help, and use other useful shortcuts, providing a complete cheat‑sheet 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 / have a history; press the up/down arrows after typing : or / to browse previous commands.

Starting Vim

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

File Commands

Open a single file: vim file Open multiple files: vim file1 file2 file3 … Open a new file in the current window: :open file Open a file in a new split window: :split file Switch to the next file: :bn Switch to the previous file: :bp List opened files (current file is shown in brackets): :args Open remote files (e.g., FTP):

:e ftp://192.168.10.76/abc.txt
:e \\qadrive\test\1.txt

Vim Modes

Normal mode – press Esc or Ctrl+[.

Insert mode – press i (shows --INSERT--).

Visual mode – shows --VISUAL--.

Navigation Commands

Basic cursor moves: h – left j – down k – up l – right (rarely used; w is preferred).

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

Line moves: 0 – first character of the line. ^ – first non‑blank character. $ – end of the line. gg – top of the file. G – bottom of the file. :<em>line</em> – jump to a specific line.

Screen scrolls: Ctrl+e / Ctrl+y – scroll one line down/up. Ctrl+d / Ctrl+u – scroll half a screen down/up. Ctrl+f / Ctrl+b – scroll a full screen down/up.

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. O – open a new line above.

Search Commands

/text

– forward search for text. ?text – backward search. n / N – repeat search forward/backward. :set ignorecase – case‑insensitive search. :set noignorecase – case‑sensitive search. * / # – search word under cursor forward/backward. :set hlsearch – highlight all matches. :set nohlsearch or :nohlsearch – turn off highlighting. :set incsearch – incremental search. :set wrapscan – continue search at file start/end.

Replace Commands

ra

– replace the character under the cursor with a. s/old/new/ – replace first old with new on the current line. s/old/new/g – replace all occurrences on the 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 the character under the cursor. 3x – delete three characters. X – delete the character before the cursor. dl – delete the character under the cursor (same as x). dh – delete the character before the cursor. dd – delete the current line. 10d – delete ten lines starting from the current line. D – delete from the cursor to the end of the line. d$ – same as D. :1,10d – delete lines 1‑10. :11,$d – delete from line 11 to the end. :1,$d – delete the entire file. J – join the current line with the next (removes the blank line).

Copy and Paste

yy

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

Visual mode ( v or V) + movement + y – copy selection. ddp – swap the current line with the line below. xp – swap the character under the cursor with the next one.

Cut Commands

Visual mode + d – cut selection. ndd – cut n lines starting from the current line. :1,10d – cut lines 1‑10. :1,10m20 – move lines 1‑10 after line 20.

Exit 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 new vertical window. Ctrl+ww – cycle to the next window. Ctrl+wj / Ctrl+wk – move to the window below/above. :close – close the current window (cannot close the last one). :q – quit Vim if it is the last window. :only – keep only the current window. q{letter} – start recording a macro; @{letter} – execute it.

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 and return to the shell.

Comment Commands

Prefix a line with # to comment it. 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 system. :help i, :help CTRL-[, etc. – get help for a specific topic.

Use Ctrl+] on a tag to follow a hyperlink, Ctrl+o to go back.

Other Non‑Editing Commands

.

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

Vim Tutorial

Unix: vimtutor Windows:

:help tutor
:syntax

– list defined syntax items. :syntax clear – clear syntax rules. :syntax case match – case‑sensitive matching. :syntax case ignore – case‑insensitive matching.

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.

productivitytext editorVimkeyboard shortcutsCommand Cheat Sheet
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.