TT Lab
Get started
Learn Learning paths Courses

vim and tmux

vim Macros and Registers

Continue in TT Lab

Goal

You confirm for yourself what a macro is (just text held in a register) and learn how to design a repeatable macro.

Why it matters

A macro recorded with qa is your keystrokes stored as a plain string in register a. That is also why yank and macros share the same a–z space. Once you know this, you can pull a macro out to a file, edit it and put it back, and as in this lab you can submit it as a file for verification. A repeatable macro also has design principles: pin the starting point absolutely, move by content, and finish by moving to the next target. If any one of the three is missing, it goes off track from the second line.

Steps

Work in the /root/vmac directory. In a macro file, write the keystrokes on a single line.

  1. Put the following 6 lines in /root/vmac/list.txt. web01 / web02 / db01 / db02 / cache01 / queue01
  2. Save the macro keystrokes that turn each line into something like - web01 to /root/vmac/macro1.txt, and the result of applying it to /root/vmac/bulleted.txt.
  3. Save the macro that turns each line into "web01", to /root/vmac/macro2.txt, and the result to /root/vmac/quoted.txt. This macro must contain an ESC character.
  4. Save the ex command that adds a line number such as 1. or 2. to the start of each line to /root/vmac/number.ex, and the result to /root/vmac/numbered.txt.
  5. Put the following 6 lines in /root/vmac/hosts.txt. web01.prod.example.com / web02.staging.example.com / db01.prod.example.com / db02.staging.example.com / cache01.prod.example.com / queue01.staging.example.com Then save the command that adds # at the very start of only the lines that contain staging to /root/vmac/onlystaging.ex, and the result to /root/vmac/staging.txt.
  6. Save the command that gathers only the lines of hosts.txt that contain prod and appends them to the very end of the file (9 lines in total) to /root/vmac/collect.ex, and the result to /root/vmac/collected.txt.
  7. Put the following 5 lines in /root/vmac/table.txt. name=web01 / role=frontend / zone=seoul / port=8080 / tier=gold Save the macro that turns each line into something like NAME:web01, with the key in uppercase and the equals sign changed to a colon, to /root/vmac/macro3.txt, and the result to /root/vmac/table_out.txt.
  8. Save the commands that keep only the prod lines of hosts.txt and add - at the start and :80 at the end to /root/vmac/final.ex, and the result to /root/vmac/final.txt.

Notes

Create a practice list

Put the following 6 lines in /root/vmac/list.txt. web01 / web02 / db01 / db02 / cache01 / queue01

Exactly 6 lines. The order has to match too.

First macro: add a bullet

Save the macro keystrokes that turn each line into something like - web01 to /root/vmac/macro1.txt, and the result of applying it to /root/vmac/bulleted.txt.

I starts typing at the very start of the line. Even the single space at the end of the macro must be saved in the file as it is.

Put an ESC character in a macro

Save the macro that turns each line into "web01", to /root/vmac/macro2.txt, and the result to /root/vmac/quoted.txt. This macro must contain an ESC character.

The key that leaves Insert mode is also part of the macro. In vim, press Ctrl-V and then Esc, and that character goes into the file.

Number the lines

Save the ex command that adds a line number such as 1. or 2. to the start of each line to /root/vmac/number.ex, and the result to /root/vmac/numbered.txt.

The value changes from line to line, so a fixed string won't work. There is a syntax that makes the replacement part of a substitution evaluate an expression.

Apply only to lines that match a condition

Put the following 6 lines in /root/vmac/hosts.txt. web01.prod.example.com / web02.staging.example.com / db01.prod.example.com / db02.staging.example.com / cache01.prod.example.com / queue01.staging.example.com Then save the command that adds # at the very start of only the lines that contain staging to /root/vmac/onlystaging.ex, and the result to /root/vmac/staging.txt.

Combine a line filter with Normal-mode keys in the form :g/pattern/normal keys.

Gather scattered lines in one place

Save the command that gathers only the lines of hosts.txt that contain prod and appends them to the very end of the file (9 lines in total) to /root/vmac/collect.ex, and the result to /root/vmac/collected.txt.

A lowercase register overwrites and an uppercase register appends. After gathering, paste at the end of the file.

A macro that combines two actions

Put the following 5 lines in /root/vmac/table.txt. name=web01 / role=frontend / zone=seoul / port=8080 / tier=gold Save the macro that turns each line into something like NAME:web01, with the key in uppercase and the equals sign changed to a colon, to /root/vmac/macro3.txt, and the result to /root/vmac/table_out.txt.

Uppercase one word with a text object, then chain a character search and a single-character substitution.

Filter + prefix + suffix together

Save the commands that keep only the prod lines of hosts.txt and add - at the start and :80 at the end to /root/vmac/final.ex, and the result to /root/vmac/final.txt.

Combine the techniques you used earlier in order. It helps to choose the lines to keep first.