Save Vim Macro

Search for a command to run...

No comments yet. Be the first to comment.
I've slowly adding more and more lua functions into my neovim configuration, and recently I noticed a pattern for a class of functions that reach out to run shell commands that can be abstracted away. https://youtu.be/8m5ipBuopPU Telegraph.nvim Check...

Kedro can have a chatty logger. While this is super nice in production so see everything that happened during a pipeline run. This can be troublesome

In 2021 I changed the way I navigate between tmux sessions big time. Now I can create, kill, switch with ease, and generally keep work separated into
People who are quick to toss team members under a bus are not well trusted or highly thought of and it will lead to some toxic team dynamics. Building Steam While collaborating on any project there are going to be decisions made that aren't necessari...

I create this blog with one person in mind, me. There are others like me This is not completely selfish, as there are likely many others out there that think similarly to me. Everyone comes from different backgrounds and varying levels of experience...

If you are like me, you have created a macro or two that is pure glory, and you forget how you made it after a day or so, or you immediately want to store it away as a custom keybinding. As with most things with vim, it's easy to do once you understand it.
One of the earliest things we all learn to do in vim is to create macros, custom sets of functionality stored in a register that can be replayed later.
To create a macro, get into normal mode, then type q followed by a letter that you want to store the macro under.
qq
Note: a common throw-away macro register is q because it's easy to hit qq from normal mode to start recording.
Macros can be replayed using @ followed by the letter that you stored the macro under.
@q
Registers are nothing more than a single character key mapping to a value of some text. As you yank, delete, or create macros in vim, it automatically stores text into these registers.
When you hit p paste it's simply pasting in the default register. You can also paste in any other register by hitting "qp where q is the register that you want to paste in.
To see what you have stored in each register, use the :reg command. This is a powerful tool that I have underutilized for a long time. It is really great to see what you have in each register.
:reg
a little magic
The magical shortcut that makes it easy is that control + r <C-R> followed by a register will paste that register wherever you currently are, including the command mode.
:nnoremap {binding} <C-R>{register}
relieve some of that Macro Pressure
Now that we understand that macros are simply strings of text placed into a register, it becomes a bit more intuitive to edit them after being created.
First, paste the contents of the register into your current working buffer.
<C-R>q
Then edit the macro and add it back to that buffer and delete it.
"qdd
If your macro had multiple lines in it, you might need to.
"qdj
"qd2j
One use case of editing a macro may be making it recursive after trying it out a few times. Macros can become recursive by simply calling themselves after running.
Paste in the macro.
<C-R>q
Go to the end of the line and add @q to get called again.
A @q
Replace the q register with the updated macro.
"qd
Note: don't use this in a shortcut as the macro may change. If you want to call the keybinding again, you will have to use noremap instead of nnoremap, but be careful as recursive remaps can be dangerous.
" record a macro
q{register}
" play a macro
@{register}
" list registers
:reg
" map a macro to a keyboard shortcut
:nnoremap {binding} <C-R>{register}
" edit a macro
<C-R>{register}
"{register}dd
" make a macro recursive
<C-R>{register}A@q<esc>"{register}dd