Let the mouse go! (pt. 2)

Fun with macros.

Pietro Scarampella
3 min readJul 6, 2021

--

In my previous article, I introduced the things that I’d love to know when I first start using VIM as my to-go editor inside my IDE and, now I want to introduce you to the macros world.

What is a macro?

A macro is a recording that allows you to repeat a sequence of commands to automate some actions, a way of refactoring some part of your code (or text).

A recorded macro is saved into a register that could go from letter a to z, to do so you hit q<letter> then input a series of commands and press q again to end the recording.
To replay a recorded macro, you hit @<letter> or <number>@<letter> to replay n times.

When using a macro?

I suggest you start with a simple task to enter into the right mindset. For me was adding a dot at the end of a sentence. We write a lot of documentation, and I always forget the punctuation at the end of sentences, especially when describing parameters.

/**
* A function with some parameters.
*
*
@param param1 as parameter one
*
@param param2 as parameter two
*
@param param3 as parameter three
*/

What we need to do is repeating a series of commands in a way that:

  1. We put the cursor at the end of the sentence.
  2. Adding a dot.
  3. Moving the cursor into the next line so we can repeat the macro multiple time.

To do so you first want to move your cursor at the line of the first missing dot, on parameter number 1.

Let’s assume that we are saving on the register `a` and start recording the macro with `qa`.

Now we go with the following:

  1. Enter the command $ to move at the end of the sentence.
  2. Enter the insert mode with append and inserting the dot a.
  3. Back to normal mode with escape.
  4. moving down 1 line with j
  5. ending recording with q

Now that you have recorded the macro you can replay it 2 times using 2@a to complete the sentences, resulting in something like this:

Cool? Hell no!

A more complex example

For a test I was setting up, I was given a list of points like this:

18.81799 35.7445
23.13299 16.8974
16.21079 0.0
4.51639 12.839
1.0195 30.0177
0.0 47.8785
16.79069 75.0814
17.37789 55.0006
0.09226 66.3364
1.20363 85.2964
5.23729 103.1604
10.80269 119.8994
49.83219 76.6024
48.60009 57.5435
32.11859 65.6744
34.12719 84.2464

Two numbers, space-separated, that I need to use as a list of Points where a point is defined like that:

data class Point(val x: Double, val y: Double)

So I did this way:
First I wrapped the raw list inside a listOf() constructor.

listOf(
18.81799 35.7445
23.13299 16.8974
16.21079 0.0
4.51639 12.839
...
)

Then, as before, we have to put out a macro that is repeatable for our scenario, let’s start recording our macro in the e register with qe and then:

  1. move your cursor to the start of the line with the command 0.
  2. move the cursor to the start of the first number with w
  3. Change to insert mode with i and input Point( then escape to normal mode.
  4. move your cursor to the first space with f<space>
  5. Change to insert mode with i and input the separating comma , then escape to normal mode
  6. Move to the end of the line with $
  7. Change to insert mode with a (append) and insert the closing parenthesis and a comma ), then escape to normal mode
  8. move down one line with j
  9. end macro recording with q

repeat that as many times as needed with <number>@e and here is the result:

That’s it!
Thank you again for your time!

Again, if you missed the previous article, here it is:

--

--