2

My macros are getting a bit out of control and I would like to organize them into mappings.

Let's say I have a Vim macro stored in the register e. I open my .vimrc find the content of the macro by printing the content of the register "ep and try to map it under <leader>ne as follows nnoremap <leader>ne {content of the register}.

This does not seem to work. How should one proceed?

EDIT: I would like the mapping to be independent of the register e, so that e can change, but not the mapping.

4
  • 1
    I am aware about escaping characters and such, I can't believe there is no simple way of doing this.
    – Nre
    Commented Aug 14 at 18:30
  • 2
    Welcome to Vim! It would be helpful if you could write down the content of the macro and it's expected effect such that we can guide you better. Would that be possible for you? Commented Aug 14 at 18:48
  • What you did is exactly how it’s done. Add the actual content of that register and the exact mapping to your question so that we can help you better. Also, what exactly "doesn’t seem to work"? The definition of the mapping or the mapping itself? And how does it not work? What do you expect? What do you get instead?
    – romainl
    Commented Aug 15 at 8:31
  • Thank you for your answers. Yes, I could post the content of the macro, but I cannot copy it (it contains non-ascii characters I think) using the "ep command. Moreover, I wanted to know if there was a general procedure, so that I do not have to ask the same question for any obscure macro which may come up. Would there be another way to copy and paste the content of the macro by replacing the non-ascii characters?
    – Nre
    Commented Aug 15 at 19:03

2 Answers 2

3

Type the following command in the command line

:nnoremap <key>

Then with your cursor after the <key> to which you want to map your macro (including the trailing whitespace), type <Ctrl-R><Ctrl-R><register-name> (so if your macro is stored in register @a, type <Ctrl-R><Ctrl-R>a).

This will insert the macro literally into the command line and it should be possible to directly use the result and map it to your prefered key mapping.

See :h c_CTRL-R_CTRL-R

However, I am not sure this will work always in all edge-cases (like strange Key names, events, etc).

4
  • Thank you. A part from one or two different characters, it prints the same as the "ep command. Many non-ascii characters. It does not work either. I can share the macro, but cannot paste the non-ascii characters here.
    – Nre
    Commented Aug 15 at 19:12
  • I would rather recommend to make a proper function out of it. It's better for long-term maintanability Commented Aug 16 at 19:34
  • Thank you, I have done this yesterday. Took a bit of time, but is more readable and editable for sure. So no real way of converting reliably and consistently a macro to a function?
    – Nre
    Commented Aug 17 at 20:37
  • no, because Vim would need to parse the input string, keep track of modes, etc. Commented Aug 18 at 14:54
0

The right hand side of the nnoremap is "interpreted".

E.g.:

<Esc> stands for ^[

The | if not escaped will end the nnoremap command and chain it with the rest of the line as a command.

...

This could lead to different behavior between your macro and your mapping.

2
  • 1
    While it's true that nnoremap is interpreted and will recognize sequences such as <Esc>, there's actually nothing wrong with adding an actual ^[ escape character in your sequence, .vimrc will work just fine with it, nnoremap will recognize it and it should just work. There might be some specific sequences that might break, for example if the macro includes < itself, but in general it should work.
    – filbranden
    Commented Aug 14 at 19:44
  • 1
    This what I mean indeed if the macro contains <, | or \ the mapping will be interpreted differently. Commented Aug 15 at 0:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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