1

I've discovered I can execute vim macros from the command line using the following command:

vim myfile -c "argdo norm @aZZ"

This command executed from the command line will execute a macro that I have stored inside register a and close the file. It works fine, however, I'd like to be able to execute a macro that I have as a string and not a macro that is already inside a register.

Let's say I see my macro using :reg on vim, and inside the register "a I see Goa^Ma^Ma^C^C. If I manually copy Goa^Ma^Ma^C^C to a different register with the command :let @l="Goa^Ma^Ma^C^C", this macro will not work on the register "l as it works on register "a. Is there any way of copying the value of a macro to my clipboard and paste it on another register without breaking it? Just so I can execute macros that I've extracted from my registers on the command line?

2
  • Hi, welcome! Your let command should work just fine. However, you need to input ^M and alike as single characters, with <c-v><c-m> for example.
    – Biggybi
    Mar 4 at 8:38
  • If the value is stored in the clipboard, you should be able to directly access it using vim <args> -c ':norm @+' <filenames> Mar 4 at 12:26
2

^X is display representation of byte value of #24 (as "X" is the 24th letter in alphabet). Hence instead of :let @l="Goa^Ma^Ma^C^C" you should write:

let @l = "Goa\ra\ra\3\3"

Escape rules are borrowed from C. Full description is under :h expr-quote.

4
  • Complicated to get right—why not ctrl-v insert the characters? Much easier
    – D. Ben Knoble
    Mar 4 at 13:30
  • @D.BenKnoble I don't think C-style escapes are too complicated.
    – Matt
    Mar 4 at 13:45
  • @Matt Thanks... But my issue is more about the situations that I create a very huge macro and I want to execute this macro on a bash script outside vim. The huge problem that I see is that even though I can see the macro that I've created on :reg, I still need a lot of rework to adapt this macro to become an executable macro string. Is that how it should be? If I create huge macros and I want to execute them as strings, do I need to dedicate the time for translating what I see on :reg to a string using the right characters? Or is there a more efficient way of doing it?
    – raylight
    Mar 5 at 18:08
  • @raylight See :h :source! But a long sequence of normal-mode commands will be hard to read.
    – Matt
    Mar 5 at 18:29
2

If you’re copying from one register to another, do that:

:let @l = @a

Or if you want to edit,

:put a
(Edit)
0"lD

The last sequence is to put the macro into register l but avoid adding a trailing newline.

Make sure to use literal control characters as necessary (<C-v> is useful there).

2
  • @Rich I think 0"lD works?
    – D. Ben Knoble
    Mar 4 at 17:08
  • I think I didn't explain it properly in my question, sorry. But in my specific case of use, this method won't work because I want to generate the string of the macro just so I'm able to execute it outside the file. This way, I could use vim macros even inside a process that is automatically receiving files on a server.
    – raylight
    Mar 5 at 18:10

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.