Vim Essentials: Modal Editing, Motions, and a Repeatable Workflow
Chen Kai BOSS

Many people bounce off Vim because they try to memorize shortcuts without learning the underlying composition rules. Vim is not “ a bag of hotkeys ”; it ’ s modal editing + a small set of motions and operators that combine into a workflow you can repeat across files, languages, and machines. This post focuses on the 80% you ’ ll actually use daily, then shows you how to scale to the remaining 20% by composing patterns rather than hunting for one-off commands.

The core idea: modes + composable operations

Vim is built around a simple loop:

  1. Move to what you want to change (motion)
  2. Select the scope (implicitly by the motion, or explicitly via Visual mode)
  3. Apply an action (operator) like delete/change/yank

If you learn the grammar, you stop “ remembering keys ” and start “ speaking Vim ”.

The essential modes

  • Normal mode: navigation and commands (default when Vim starts)
  • Insert mode: typing text
  • Visual mode: selecting text (character/line/block)
  • Command-line mode: : commands (save, search/replace, settings, etc.)
  • Replace mode: overwrite characters (R)

Rule of thumb: spend most time in Normal mode; treat Insert as a temporary detour.

To return to Normal mode from anywhere: press Esc.

Operators and motions (the “ Vim grammar ”)

Common operators:

  • d delete
  • c change (delete + enter Insert)
  • y yank (copy)

Motions / text objects define “ how much ”:

  • w next word, b previous word
  • 0 start of line, ^ first non-blank, $ end of line
  • gg top of file, G end of file, {number}G go to line
  • text objects: iw inner word, ip inner paragraph, i" inside quotes, etc.

Examples you should internalize:

  • dw delete word
  • d$ delete to end of line
  • ciw change inner word
  • yip yank paragraph

File operations (save, quit, open)

These are command-line mode (:) commands:

  • :w save (write)
  • :q quit
  • :wq / :x save and quit
  • :q! quit without saving
  • :e filename open/edit another file
  • :saveas filename save as a new file (similar to :w <path>)

Movement and scrolling (fast navigation)

Basic cursor movement:

  • h/j/k/l: left/down/up/right (use this only when learning)
  • w/b: next/previous word
  • 0, ^, $: line start / first non-blank / line end
  • gg, G, {number}G: top / bottom / go to line

Screen/page movement:

  • Ctrl+f / Ctrl+b: page forward/back
  • Ctrl+d / Ctrl+u: half-page down/up

Practical tip: combine search with motion. Most movement should be “ jump ” not “ crawl ”.

Delete, yank, paste, undo/redo

Delete

  • x delete character under cursor
  • dd delete current line
  • d{motion} delete a range defined by a motion (dw, d$, dG, etc.)

Yank (copy)

  • yy yank current line
  • y{motion} yank a motion-defined range (yw, y$, etc.)

Paste

  • p paste after cursor (or below the line)
  • P paste before cursor (or above the line)

If you paste large blocks and auto-indentation/comments get in the way, consider temporarily enabling paste mode:

1
:set paste

Turn it off after:

1
:set nopaste

Undo/redo

  • u undo
  • Ctrl+r redo

Cut (move text) as “ delete then paste ”

There is no separate “ cut ” command: it ’ s just delete + paste.

Example: move a line elsewhere:

  1. dd (delete the line — it goes into a register)
  2. move cursor
  3. p to paste

Useful command-line mode settings

These help daily use and debugging:

  • :set number show line numbers
  • :set relativenumber relative numbers (great for 5j, 10k)
  • :noh clear search highlight
  • :map / :verbose map inspect key mappings (useful for conflicts)
  • :!<cmd> run a shell command (e.g., :!ls)

Windows, splits, and tabs (work on multiple views)

Splits:

  • :split / :sp horizontal split
  • :vsplit / :vsp vertical split
  • move between windows: Ctrl+w then h/j/k/l
  • :close close current split
  • :only close all other splits

Tabs:

  • :tabnew open a new tab
  • gt / gT next/previous tab

The swap file (.swp) warning

If Vim detects that a file is already open (or the last session crashed), it may warn you about a swap file. You can choose to recover, open read-only, or abort.

If you don ’ t need recovery, deleting the .swp file resolves the warning; if you do, follow Vim ’ s recovery prompt first, then clean up the swap file.

Macros: automate repetitive edits

Macros are the highest-ROI “ advanced ” feature because they turn repetition into a one-time recording.

  • q{register} start recording (e.g. qa records into register a)
  • perform edits (moves, deletes, inserts, etc.)
  • q stop recording
  • @{register} replay (e.g. @a)
  • @@ repeat the last macro
  • {number}@{register} repeat N times (e.g. 10@a)

Example workflow:

1
2
3
4
qa        (start recording)
... (do your edits)
q (stop)
10@a (apply to next 10 lines/blocks)

Code folding (focus on the part that matters)

Fold commands:

  • zR open all folds
  • zM close all folds
  • za toggle fold under cursor
  • zc close fold
  • zo open fold

Folding is extremely useful for large config files or long functions when you want to work locally.

Search and replace (with ranges)

Basic patterns:

  • :s/old/new/ replace the first match on the current line
  • :s/old/new/g replace all matches on the current line
  • :%s/old/new/g replace across the whole file

Target a line range:

  • :10,20s/foo/bar/g replace only between lines 10–20

Tip: start with a narrow range, verify, then expand. Search/replace is powerful and easy to misuse.

Customization: a minimal, safe .vimrc

Vim becomes comfortable once you set a few defaults:

1
2
3
4
5
6
7
8
9
10
set number
set relativenumber
set expandtab
set tabstop=4
set shiftwidth=4
syntax on
set cursorline

" Trim trailing whitespace on save (safe default for many projects)
autocmd BufWritePre * :%s/\s\+$//e

If you use Neovim, the equivalent config often lives at ~/.config/nvim/init.vim (or init.lua).

A practical practice plan (how to actually get fluent)

  1. Spend one week using only: hjkl, w/b, 0/^/$, dd, dw, ciw, /search, :%s.
  2. Add macros (qa, @a) for repetitive edits.
  3. Add a few text objects (iw, ip, i", i)) to reduce “ manual selection ”.
  4. Only then start customizing keymaps — avoid early over-customization.

Once these are muscle memory, Vim stops feeling “ hard ” and starts feeling “ fast ”.


Advanced text objects (precision editing without Visual mode)

Text objects let you operate on semantic units (words, sentences, paragraphs, blocks) without manually selecting boundaries.

Inner vs "a" (around)

  • i (inner): excludes delimiters
  • a (around): includes delimiters

Examples:

  • ciw change inner word (cursor anywhere in the word)
  • ci" change inside quotes (cursor must be inside "...")
  • ca" change around quotes (includes the quotes themselves)
  • dip delete inner paragraph
  • di( delete inside parentheses
  • da{ delete around braces (includes { and })

Common text objects

Text object Meaning Example
iw / aw inner/around word ciw change word
is / as inner/around sentence dis delete sentence
ip / ap inner/around paragraph yip yank paragraph
i" / a" inside/around "..." ci" change string
i' / a' inside/around '...' di' delete single-quoted
i( / a( inside/around (...) da( delete with parens
i{ / a{ inside/around {...} ci{ change block
i[ / a[ inside/around [...] di[ delete array
it / at inside/around HTML tag cit change tag content

Why this matters: You stop thinking "select then act" and start thinking "act on semantic unit".


Registers: where deleted/yanked text goes

When you delete or yank, the text goes into a register (a clipboard-like slot).

Common registers

  • Unnamed register (""): last delete/yank
  • Named registers ("a to "z): manual storage
  • Numbered registers ("0 to "9): yank history
  • System clipboard ("+ or "* on Linux/Mac, "+ on Windows): share with OS

How to use registers

Yank into a named register:

1
"ayy    " yank line into register a

Paste from a named register:

1
"ap     " paste from register a

View all registers:

1
:reg

Copy to system clipboard (if Vim compiled with +clipboard):

1
2
"+yy    " yank line to system clipboard
"+p " paste from system clipboard

Pro tip: Use named registers to collect multiple snippets before pasting them in sequence.


Marks: bookmarks for fast jumps

Marks let you jump back to specific positions in a file (or across files).

How to set marks

  • m{letter} set a mark (e.g., ma sets mark a at current cursor position)
  • lowercase (a-z): local to current file
  • uppercase (A-Z): global across files

How to jump to marks

  • `{letter} jump to exact position (line and column)
  • '{letter} jump to line (first non-blank character)

Example workflow:

1
2
3
ma          " mark current position as 'a'
(... navigate elsewhere ...)
`a " jump back to mark 'a'

Special marks:

  • `. jump to last edit position
  • `0 jump to position when Vim last exited
  • `" jump to position when last editing this file

Visual mode variants (character, line, block)

Visual mode has three variants:

  1. Character-wise (v): select characters
  2. Line-wise (V): select whole lines
  3. Block-wise (Ctrl+v): select rectangular blocks (amazing for columnar edits)

Block mode examples

Use case 1: Comment out multiple lines

  1. Ctrl+v enter block mode
  2. jjj... select lines
  3. I# insert # at the start
  4. Esc apply to all selected lines

Use case 2: Delete a column

  1. Ctrl+v enter block mode
  2. Select the column
  3. d delete

Use case 3: Append to multiple lines

  1. Ctrl+v enter block mode
  2. jjj... select lines
  3. A; append ; to the end
  4. Esc apply to all

Why block mode is powerful: It solves "edit the same column on 20 lines" problems that would require regex or macros in other editors.


Buffers vs windows vs tabs (mental model)

Many Vim beginners confuse these three concepts:

Concept What it is Commands
Buffer A file loaded into memory :ls, :b <name>, :bnext, :bprev
Window A viewport showing a buffer :split, :vsplit, Ctrl+w motions
Tab A layout of windows :tabnew, gt, gT

Mental model:

  • One file = one buffer (even if not visible)
  • You can have multiple windows showing the same buffer
  • Tabs organize window layouts

Common workflow:

1
2
3
4
:e file1.py       " open file1 in current buffer
:split file2.py " open file2 in a new horizontal split
:vsplit file3.py " open file3 in a vertical split
Ctrl+w w " cycle between windows

Search patterns and flags (find anything)

Basic search uses / (forward) or ? (backward):

1
2
3
4
/pattern   " search forward
?pattern " search backward
n " next match
N " previous match

Search flags

  • \c case-insensitive (e.g., /foo\c)
  • \C case-sensitive
  • \< word boundary start (e.g., /\<foo\> matches foo but not foobar)
  • \> word boundary end

Highlight search results

1
2
:set hlsearch    " highlight matches
:noh " clear highlights

Search and replace with confirmation

1
:%s/old/new/gc   " replace all with confirmation (y/n/a/q)

Tips:

  • Start with /pattern to verify what you're matching
  • Then run :%s//new/g (empty pattern reuses last search)

Common pitfalls and how to fix them

Pitfall 1: Auto-indentation breaks paste

Symptom: Pasted code gets progressively indented.

Fix: Use paste mode:

1
2
3
:set paste
(paste your code)
:set nopaste

Or use "+p (system clipboard) which auto-detects.

Pitfall 2: Accidentally enter Replace mode

Symptom: Typing overwrites characters instead of inserting.

Cause: Pressed R or Insert key.

Fix: Press Esc to return to Normal mode.

Pitfall 3: Swap file warning on every open

Symptom: Vim always warns about .swp files.

Cause: Previous session didn't exit cleanly.

Fix: Delete orphaned swap files:

1
find . -name "*.swp" -delete

Or disable swap files (not recommended for large edits):

1
:set noswapfile

Pitfall 4: Lost in a deep undo tree

Symptom: u doesn't undo what you expect.

Cause: Vim has a tree-based undo, not linear.

Fix: Use :earlier / :later to travel through time:

1
2
:earlier 5m    " go back 5 minutes
:later 10s " go forward 10 seconds

Vim vs Neovim: should you switch?

Neovim is a fork of Vim with:

  • Modern architecture (better plugin API, async support)
  • Built-in LSP (Language Server Protocol) client
  • Lua configuration (in addition to Vimscript)
  • Active development and community

When to use Neovim:

  • You want built-in LSP (autocomplete, go-to-definition, diagnostics)
  • You prefer Lua over Vimscript for config
  • You want the latest features

When to stick with Vim:

  • Your system already has Vim (servers, containers)
  • You prefer stable, widely-deployed software
  • You don't need advanced features

Bottom line: Both are excellent. Start with Vim (it's everywhere), switch to Neovim if you need LSP or modern plugin ecosystems.


Learning resources (curated)

  1. vimtutor: Run this command in your terminal. It's a 30-minute interactive tutorial built into Vim.

  2. Vim Adventures (vim-adventures.com): A game that teaches Vim motions.

  3. Practical Vim (book by Drew Neil): The best book for intermediate users.

  4. Vim Golf (vimgolf.com): Optimize keystrokes for specific editing tasks (great for learning advanced tricks).

  5. ThePrimeagen (YouTube): Advanced Vim/Neovim tutorials and workflows.


Summary: Vim in 10 principles

  1. Modes are your friend: Normal mode is home base, Insert is temporary.
  2. Think "operator + motion": d + w = delete word.
  3. Use text objects: ciw, di", yip beat manual selection.
  4. Search first, then act: /pattern + n + cw is faster than scrolling.
  5. Repeat with .: For simple edits, . is the fastest macro.
  6. Record macros for complex repetition: qa + edits + q + 10@a.
  7. Master a few motions deeply: w/b, 0/$, gg/G, {/} cover 90% of navigation.
  8. Don't over-customize early: Learn the defaults first, then add keymaps.
  9. Use splits and buffers: Don't open 10 terminal tabs; use :split and :bnext.
  10. Practice deliberately: Spend 1 week forcing yourself to use only Vim for all editing.

Once you internalize the grammar, Vim becomes a text-editing language you speak fluently, not a tool you "operate".

  • Post title:Vim Essentials: Modal Editing, Motions, and a Repeatable Workflow
  • Post author:Chen Kai
  • Create time:2023-01-05 00:00:00
  • Post link:https://www.chenk.top/en/vim-essentials/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.
 Comments