I have several useful search, search and replace and global commands written in my personal Vim reference. How can I easily turn some of them into one macro without recording (retyping everything)?


Well, if you want turn them into a macro specifically, then this is pretty easy to do. The thing you need to know about macro registers is that they are exactly the same as text registers that you cut/copy/paste from. So if you had the following text on a line:


And you wanted to turn this into a macro, you could just go to the beginning of the line, type "aD (delete line into register 'a'). Now, you can type @a and this will run that text as a macro, exactly the same as if you had recorded it. There's an even easier way to do the same thing, which is to directly assign the register. For example:

:let @a="iHello\<esc>"


:call setreg('a', "iHello\<esc>")

But I'm guessing there is a simpler way to do what you want. If you just have some functions, or sets of keystrokes that you would like to be able to easily call, you could just make a new mapping. Preferably with <leader> to avoid conflicting with other mappings. Like this answer said, leader is essentially a namespace for your own mappings. I would add something like this to your .vimrc

nnoremap <leader>h iHello<esc>

However, if you would really prefer a macro, you could simply add let @a or setreg like I demonstrated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.