Vim Macros, Create Your Own Automations!

Learn it by creating a JavaScript's "to ES6 arrow function" converter.


You’ll convert your function with a simple command!

Macros is a very useful feature that makes it simple for you to automate some tasks on the go. It works by recording some actions (file editing, movements, etc), and giving you a shortcut to apply the same actions whenever and wherever you want.

It’s a powerful tool that can be used in many ways but, for the sake of understanding the basics we’ll keep it simple.

A Macro is a sequence of commands executed non-interactively, and you can record it into a named register of your choice (…more on named registers) to use it whenever you want.

To illustrate the usage of a macro we’re not performing simple tasks like commenting lines, or removing blank spaces because these simple tasks can be achieved by ‘find and replace’ (:s command). We’re going to use macros on a simple but real life example that could demand it, we’ll create a shortcut to convert a JavaScript function to a JavaScript ES6 arrow function.

How to use it:

First of all you just need to record a macro assigning it to a named register. It’s done by starting the macro typing q<register> (ex.: for register e, type qe) performing your actions, and then, stop recording typing q.

After that, the sequence of your commands will be recorded on register and you’ll use it simply typing @<register> (ex.: @e).

Creating the ‘to ES6 arrow function’ converter:

First of all, open a new example.js file and write the sample functions below:


Now we’ll start recording our macro on register e, convert the first function to ES6 arrow function syntax, and stop recording.

(Before start: Make sure your cursor is sitting on line 1 and you’re on NORMAL mode. Be aware that you can perform the actions on your own pace, no rush, there’s no time interruption.)

Here’s the sequence explained:

  • qe — start recording macro on register e (you’ll notice that you’ll get a message under your statusline indicating that recording is on)


  • 0 — move cursor to the first column
  • :s/var/const/g and hit enter— substitute var by const
  • :s/function//g and hit enter— find the word function and remove it
  • % — move cursor to the character )
  • a — enter on INSERT mode with the cursor after character )
  • hit <Space> and type => — insert the arrow syntax
  • hit <Esc> back to NORMAL mode
  • 0 — move cursor back to the first column
  • q — stop recording macro

Now you’re done, if you access your registers you’ll see that the sequence of your commands are stored on register e . It’s the macro!


Using the macro:

To use the macro, you just move the cursor to the line you want it to act and simply type @e.

Right now we could just jump to line 5 and apply it to byeFunc function as well (and you can try it right now), but on this example I’ll show some more efficiency of our newly created macro, using it combined with some other commands.

Let’s make the macro be applied in all functions of the file on a single shot!

First of all, hit u to undo the function’s transformations until you have the first function state the same as before recording the macro:


Even after undoing the changes, you can check your registers and you’ll see that your macro is already there:


Now, apply the command :g/function/normal @e:


Use the macro created to search all functions and replace by arrow functions.

Combining these commands basically you’re telling vim to find any line that has the word function and apply the macro recorded on register e.

(To know more about macros, global or normal command, hit the_:help <command>. Make sure you get used to:help section visiting it very often)

That’s it! Now you loaded another magic power to your vim skills!

  • Vim Documentation (or open vim and :help )
  • “Mastering Vim Quickly” Book (and screencasts) by Jovica Ilic. It’s an awesome book, make sure you subscribe to the newsletter to get free Vim tips and book excerpts on your email.

(👆 This is an affiliate link. If you got any value from this post and are also interested in buying the MVQ book/screencasts, please consider visiting the affiliate link and you’ll be gifting me a coffee ☕ or a beer 🍺.)


  • Follow me on Twitter to get more posts like this and other quick tips in your feed.
  • is the TRUE and FOREVER FREE home for the previously used publication. At you’ll always find all posts without restrictions.
  • If you have any doubts or tips about this article, I’d appreciate knowing and discussing it via email.
  • Do you have any other Vim tips? Would you like to publish that in this blog? Please send an email to vim drops.
  • As English is not my native language, I apologize for the errors. Corrections are welcome.
  • Contact: vim [@] alldrops [.] info.

Read more on vim drops: