4

This thread explained saving a vim macro to a file but I cannot get it to work. I have the following macro stored in register b that I am trying to save to my rst.vim file:

let @b = '<Esc>bea**<Esc>`<i**<Esc>gvoo<Esc>e'

Everything inside of '...' came from pasting the macro from the buffer using "bp. But when I close and reopen a rst file containing only the sentence The quick brown fox jumped over the moon and type fbv2e@b, this is the resulting sentence:

Thc>bea**<Esc>`<i**<Esc>gvoo<Esc>e quick brown fox jumped over the moon.

So, there must be some escape keystrokes I am missing, but I can't find what they are. I tried searching google and :help for similar macro examples with no avail. What am I missing and what terms should I be looking for when searching? Thanks!

Community
  • 1
  • 1
wdkrnls
  • 4,548
  • 7
  • 36
  • 64
  • 1
    Are you sure that you have the escape character and not the characters `<`, `E`, `s`, `c` and `>` ? It worked for me when I tested. – some Jan 23 '11 at 17:03
  • I do have `<,E,s,c,>` because that is what got pasted in from `"bp`. I know that when looking at `:reg b` it is using `` instead. How can I insert `` into a text file? *UPDATE:* I found [this question](http://stackoverflow.com/questions/2943555/how-to-save-a-vim-macro-that-contains-escape-key-presses) which answers my question. – wdkrnls Jan 23 '11 at 17:31
  • 2
    I don't like having literal control characters somewhere, so I would have used `"\e"` for escape and `"\"` for other control characters (`"\"` also works). Note the double quotes. – ZyX Jan 23 '11 at 17:40
  • @ZyX thanks! I like that much better as well. – wdkrnls Jan 23 '11 at 17:50
  • Similar to earlier question: http://stackoverflow.com/questions/2943555 – Nicolas Raoul Dec 06 '11 at 10:25

1 Answers1

4

As hinted at by some, the problem was that <Esc> actually was <,E,s,c,>. Vim generates escapes in a text file using C-v <ESC>. So I ran :s/<Esc>/^[/g on the :let line in the question. Note that ^,[ is not the same as the ^[ generated by C-v <Esc>!

To save myself a headache a few months from now, I switched ^[ to \e. I had to make sure I surrounded the macro in double quotes because single quotes didn't work.

wdkrnls
  • 4,548
  • 7
  • 36
  • 64
  • `\e` and double quotes appears to be the only solution working for me in a `nnoremap` setting. `` or `^[` would result in `E114: Missing quote`. – dev Dec 13 '16 at 01:01