Advanced Vim: macros, registers and text objects
You multiply your editing speed with text objects, registers, macros, multiple files and windows, and write your own .vimrc.
Level 201 · 18 lessons · 21 practice labs
Lessons
The grammar
- Vim grammar: operator plus motion
- Text objects: iw, i", it, ip
- Marks and the jump list
- Short task: restructure a YAML file with text objects
Registers and macros
- Registers: named, numbered and the system clipboard
- Recording and replaying macros
- Applying a macro to hundreds of lines
- Short task: turn a messy list into CSV with a macro
Search and replace
- Regular expressions in Vim and \v mode
- Acting on chosen lines with :g and :v
- :s in depth: groups, confirmation and ranges
- Filtering through external commands: !sort, !column
- Short task: mask the IP addresses in a log
Files and windows
- Buffers and the argument list: working across many files
- Split windows and tabs
- Merging two configs with vimdiff
- Writing your own .vimrc
- Capstone: make a consistent change across ten config files in one session
Practice labs
- Change inside quotes in one move: Replace every password value in the JSON file with a masked value using the ci" text object.
- Reorder configuration blocks: Move configuration blocks into the requested order using the paragraph text object and marks.
- Update HTML tag contents in bulk: Quickly update the tag contents of the maintenance page with the it text object and marks without breaking its structure.
- Turn a messy list into CSV with a macro: Record and replay a macro to turn a list where each record spans three lines into one-line CSV records.
- Apply a macro to hundreds of lines: Write a macro that turns each line of a user list into a useradd command, apply it to the whole file and save it as a script.
- Edit a broken macro and rerun it: Paste a wrongly recorded macro from its register, fix it, load it back and apply it to the remaining lines.
- Bring a deleted line back from a register: List the registers with :registers, find the settings line removed two deletions ago in a numbered register and paste it back where it belongs.
- Move snippets with named registers: Copy three different config snippets into separate registers and paste them into the right places in the target file.
- Collect scattered lines in one register: Gather six scattered permission lines from a long config into one register by appending with its upper-case name, write the line count computed with the expression register into the header and paste the lot into a new file.
- Inspect a log and config side by side: Open the log and the config in split windows, find the setting the error points to and fix it.
- Move settings between split windows: Open the old and new configs in vertical splits, move between the windows to copy across the three missing settings and save both files.
- Review long files in step with a three-window layout: Open the log, the config and its backup in three windows, scroll the two configs together with scrollbind, resize and move the windows, fix the four differing lines and save the layout to a session file.
- Pass the whole file through an external command: Without leaving Vim, sort the host list with :%!sort and save the result.
- Sort a selected section with an external command: Sort only the allow-list section of the config with !sort -u and remove duplicate entries.
- Reformat one block inside a file with an external tool: Select the one-line JSON block embedded in a config in visual mode and reformat it through jq, undo the damage when the command fails and add a date line to the file with :r !.
- Mask the IP addresses in a log: Write a :s command in \v mode that replaces the last octet of every IPv4 address in the log with x before sharing.
- Keep only the error lines: Delete every line without ERROR using :v, then use :g to add a timestamp prefix to the remaining lines.
- Swap columns with capture groups: Use capture groups to turn name-surname records into surname, name and lowercase the email field.
- Merge two configs with vimdiff: Compare the server's and the repository's config versions with vimdiff and carry over only the security settings.
- Make a consistent change across ten files: Use the argument list and :argdo to change the old log path in ten config files in one session and save them all.
- Write a safe .vimrc for the team: Prepare a .vimrc with line numbers, search highlighting, tab settings, filetype indentation and modelines turned off.
About this course
You multiply your editing speed with text objects, registers, macros, multiple files and windows, and write your own .vimrc.
What you will learn
- The grammar
- Registers and macros
- Search and replace
- Files and windows
Prerequisites
Finishing the level 101 courses of the same path, or knowing those topics, is enough.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at level 201; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.