Advanced Patterns & Workflows

This is the chapter where everything comes together. You've learned modes, motions, text objects, registers, search, macros, and mappings — now it's time to see how experienced Vim users combine these tools to solve real-world problems.

The recipes here range from clever substitution tricks (like adjusting page numbers in a book index) to workflow patterns for multi-file refactoring and session management. Think of this chapter as a collection of battle-tested techniques that you can adapt to your own needs.

What you'll learn in this chapter:

  • Combine substitution commands with expressions to perform math and text transformations

  • Use marks and folds to navigate and organize large files efficiently

  • Execute multi-file search-and-replace workflows using vimgrep, argdo, and LSP rename

  • Manage sessions and multiple projects in Vim and Neovim

  • Apply a collection of practical tricks for everyday editing tasks

Advanced Substitution Patterns

Changing a Book Index

These patterns build on the search and regular expression foundations from (see Search, Replace & Regex).

Decrease all page numbers by 22:

:%s/Page \zs\d\+/\=submatch(0) - 22/
  • Page — literal match (context)

  • \zs — start the actual match here

  • \d\+ — one or more digits

  • \=submatch(0) - 22 — evaluate: matched number minus 22

Doing Math with Expression Register

For lines like 13 + 56, calculate and append the result:

Capitalize First Word After Punctuation

Removing Blank Lines in Specific Regions

Remove blank lines only inside function bodies (between ) and ^}). This combines the global command (see The Global Command) with a substitution range:

Blockwise Incrementing Numbers

Given repeated lines with id="1", create a sequence. This pairs well with macro-based repetition (see Macros):

  1. Visual block select the numbers (except the first): Ctrl+v, select down

  2. Press g Ctrl+a

Result:

Advanced Navigation

Marks

Marks let you bookmark positions in files:

Mark
Scope
How to set
How to jump

a-z

Local (per buffer)

ma

`a or 'a

A-Z

Global (across files)

mA

`A or 'A

Special marks:

Mark
Meaning

`.

Last change position

`"

Last position before closing buffer

`^

Last insert position

`[ and `]

Start/end of last yank or change

Folding

Neovim: Treesitter-based folding provides more accurate folds than indent or syntax methods:

Workflow: Multi-file Search and Replace

Method 1: vimgrep + cdo

Method 2: argdo

Method 3: LSP Rename (Best for Code)

Position cursor on a symbol and use the built-in LSP client (see LSP):

LSP rename is semantically aware — it only renames the actual symbol, not string matches.

Workflow: Working with Multiple Projects

Using Sessions

auto-session (Neovim)

auto-sessionarrow-up-right automatically saves and restores sessions:

Workflow: Spell Checking for Writers

Command
What it does

]s

Jump to next misspelled word

[s

Jump to previous misspelled word

z=

Show spelling suggestions

zg

Add word to dictionary

zw

Mark word as incorrect

zug

Undo zg (remove from dictionary)

Auto-correct in Insert Mode

Workflow: Abbreviations

Abbreviations auto-expand text as you type:

In Neovim Lua:

Workflow: Window Management

These commands extend the window management fundamentals covered in (see Buffers, Windows & Tabs).

Rotating and Rearranging Windows

Command
What it does

Ctrl+wr

Rotate windows forward

Ctrl+wR

Rotate windows backward

Ctrl+wx

Exchange current window with next

Ctrl+wH

Move window to far left (vertical)

Ctrl+wJ

Move window to bottom (horizontal)

Ctrl+wK

Move window to top (horizontal)

Ctrl+wL

Move window to far right (vertical)

Ctrl+wT

Move current window to a new tab

Working with Vertically Tiled Windows

When working with side-by-side vertical splits, these are useful:

Useful Tricks Collection

Delete Without Messing Registers

Increment/Decrement Numbers

Open URLs in Browser

Sort Lines

Execute Clipboard as Vim Commands

You can execute the content of the clipboard register as Vim commands:

This runs whatever text is in your system clipboard as if you typed it in command mode. Useful when someone shares a Vim command and you want to run it directly.

Quick Diff of Unsaved Changes

This shows you what changed since the last save.

Repeat Last Substitution

Insert Incrementing Numbers

Add line numbers or sequential IDs to a selection:

Record Position Before/After Operations

Use marks to bookmark important positions before complex operations:

Summary

This chapter brought together techniques from across the book into practical, real-world workflows. From expression-based substitutions and multi-file refactoring to session management and spell checking, these patterns represent the kind of fluency that distinguishes experienced Vim users. The key is not to memorize every recipe, but to recognize the building blocks so you can compose your own solutions.

Key takeaways:

  • Expression substitutions (\=) let you perform math and dynamic transformations directly in search-and-replace commands.

  • Multi-file workflows using vimgrep, cdo, and argdo make large-scale refactoring manageable without leaving the editor.

  • Marks, folds, and sessions help you navigate and organize your work across large codebases and multiple projects.

  • Small tricks like the black hole register ("_), g&, and gx compound into significant productivity gains over time.

Exercises

  1. Math substitution — Create a file with five lines like Price: 100, Price: 250, etc. Write a substitution command that increases every price by 15%.

  2. Multi-file search and replace — Create three text files in a directory, each containing the word foo in different places. Use vimgrep and cdo to replace foo with bar across all files (see Search, Replace & Regex).

  3. Marks and navigation — Open a long file, set marks a, b, and c at three different sections. Practice jumping between them using both `a (exact position) and 'a (line only). Then use `. to return to your last edit.

  4. Session management — Open three files and arrange them in splits. Save the session with :mksession. Close Vim, reopen it, and restore the session. Verify that all windows and buffers are restored.

  5. Blockwise increment — Create 10 lines of <li>item 0</li>. Use visual block mode and g Ctrl-a to turn them into a numbered sequence from 1 to 10 (see Macros for alternative approaches).

Last updated

Was this helpful?