1

Have a macro where a visually selected text will be made a markdown link and once the macro executes it leaves Vim in INSERT mode:

|  - VISUAL selection
i  - cursor position in INSERT mode
@l - invoke macro in "l" register

Some |text| saying stuff.   = @l => Some [text]() saying stuff.
                                                i

Register l holds the following:

:registers l
Type Name Content
  c  "l   S]%a()^Ohq<80>kb

Created it the following way:

  1. Visually select some text
  2. q then l
  3. S then ] (S is a visual selection command in vim-surround)
    ^ - normal mode cursor position
    
    |text|  =S]=>  [text]
                   ^
    
  4. % (jump to closing bracket)
  5. a and then type ()
  6. CTRL-O
  7. h
  8. CTRL-O then q (to finish the macro but to remain in INSERT mode)

So I selected this definition with the mouse (i.e., * register), and did let @l = "S]%a()^Ohq<80>kb" in the other Vim instance, resulting in

Some |text| saying stuff.   = @l => Some [text]()^Ohq<80>kb saying stuff.
                                                           i

It works if Vim is built with clipboard support (:let @+ = @l in one Vim instance, and :let @l = @+ in another), but I would like to learn the fundamentals of deciphering these representations.

UPDATE: There is a slight difference between the Vim representations of the recorded & clipboard-copied macros and the ones that are pasted manually: the sequences ^O and <80> are highlighted as special in the former case, but the string is shown with uniform color in the latter.

The l is the original recorded macro, + is created by :let @+ = @l, and c content has been typed in verbatim:

enter image description here


Found a couple other Stackoverflow threads with a similar premise, but they weren't helpful:

VIM macro editing

Tried to convert the ^O to <C-O> but it didn't work, and have no clue what <80>kb is.

Based on the creation steps above, I would have expected something like S]%a()^Oh^Oq instead of S]%a()^Ohq<80>kb anyway.

Reliable solution to copy/paste Vim macros

No replies, and based on what I understood, it is not applicable in my case.

Saving vim macros

Chewed my way through the answers and comments, but no joy.

toraritte
  • 4,130
  • 3
  • 26
  • 43

1 Answers1

2

That cryptic <80>kb is a recording artefact: you pressed <Backspace> at some point during the recording and that's how Vim represents it internally.

Recording really records everything you type, whether it makes sense or not, whether you want it in your macro or not. So those <80>kb are a dirty little secret: macros are not just text, which makes them not as portable as one would think. At least not without a bit of work.

The only way to make your macros portable is to sanitize them manually…

  1. Insert the content of register l in a buffer:

    :new
    "lp
    
  2. Edit away all the cruft with your usual editing magic.

  3. Yank it back to the desired register:

    0"+y$
    
  4. Get rid of that buffer:

    :bw!
    
romainl
  • 158,436
  • 18
  • 236
  • 264
  • I never pressed Backspace, and recorded the macro in multiple Vim instances with the same keystrokes to make sure that the macro contents are the same, but then this is something that is not documented as you say. As for step 2., how would one know which part is the cruft then?:) Only `let @l = "S]%a()"` seems to be working but the INSERT cursor ends up after the closing `)`. Plus it is working if simply passing the register throught the clipboart (`:let @l = @+`) so there should be a conversion, right? – toraritte Mar 11 '21 at 17:02
  • What looks like a command you would do is to be kept, what makes no sense is to be removed, simple as that. – romainl Mar 11 '21 at 19:37