34

I use the following Vim macro a lot (it puts the current line inside XML tags):

I<e>^[A</e>

So I saved it into my .vimrc

let @e='I<e>^[A</e>'

But it does not work.
The ^[ part means "Escape" but it is not understood as such in .vimrc

How can I save this macro, or any macro that contains "Escape" ?

Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373

7 Answers7

38

Try entering the escape with <Ctrl-v><Esc> in insert mode. See :help i_CTRL-V in vim.

catchmeifyoutry
  • 7,179
  • 1
  • 29
  • 26
  • 2
    No it shouldn't. With the ^[ is a single charater (escaped code for ESC), not the '^' and '[' characters. Just tested it again in vim and gvim 7.2, it correctly wraps a line in tags. – catchmeifyoutry May 31 '10 at 13:22
  • 2
    simple test, when you move the cursor along the ^[ part, you shouldn't be able to place it between the ^ and [, as it represents a single keycode. – catchmeifyoutry May 31 '10 at 13:25
  • The trick was indeed this single keycode character, I didn't see difference at first. Thanks a lot! – Nicolas Raoul May 31 '10 at 13:35
  • Not sure if my google-fu was just bad, but I couldn't find any other hints of the right way to do this on all of the internets. – cori Jan 09 '18 at 15:27
25

For a macro:

:let @e='^[I<e>^[A</e>'

Where ^[ is just one char formed by hitting CTRL+VESC (CTRL+QESC on Windows). Note the escape right at the beginning of the macro.

See :help c_CTRL-V in Vim for more information.


For those who've landed on this post looking for how to add ESC to a mapping, the answer is different.

Literally type <ESC>, thus:

:nnoremap <Leader>E I<e><ESC>A</e><ESC>

See :help key-notation in Vim for more info.

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • 1
    Now I think I understand what you mean by "does not work". Answer updated. – johnsyweb May 31 '10 at 13:19
  • 2
    It works, thanks a lot :-D The trick was to use this single character instead of ^[ Now I realize catchmeifyoutry had the solution! – Nicolas Raoul May 31 '10 at 13:22
  • 1
    Glad you got it working. You may find that `map`-pings are more useful if you have a lot of such shortcuts as they do not trample on your `registers`. – johnsyweb May 31 '10 at 13:25
  • Indeed, mappings seems to be more appropriate for my usage, thanks again :-) – Nicolas Raoul May 31 '10 at 13:31
  • 1
    @Johnsyweb Yes. Great explanation! Couldn't find this anywhere else. This finally solved my problem for how to embed an escape. Thank you! – GH05T Apr 02 '17 at 09:54
  • You should remove the wrong suggestion. It's an unnecessary noise. I was trying to get it work for past half an hour. Please make the updated version the default one. – ashrasmun Jun 21 '19 at 15:48
  • I'm sorry my 9 year-old answer cost you so much time @ashrasmun. I've used the edit functionality to make the answer clearer. – johnsyweb Jun 23 '19 at 05:17
  • @Johnsyweb Thank you :) – ashrasmun Jun 24 '19 at 13:21
6

For readability purpose, it's possible to use the proper key-notation tags such as <Esc> or <CR> instead of ^[ or ^M

You would need to escape the tag <Esc> with a single \ and use double quotes instead of single quotes which would result in "\<Esc>". The following examples are equivalent

:let @e='^[I<e>^[A</e>'
:let @e="\<Esc><e>\<Esc>A</e>"

A list of all key notations can be found by typing :help key-notation or here.

tfachmann
  • 136
  • 2
  • 4
4

If you're using Windows behavior for vim where Ctrl+V is the Paste command, the equivalent sequence is Ctrl+Q

Nick Canzoneri
  • 3,774
  • 1
  • 23
  • 17
1

Today I discovered a vim plug-in called MARVIM (http://www.vim.org/scripts/script.php?script_id=2154).

It is capable of storing macros and executing them later using shortcuts.

Oz.
  • 97
  • 1
  • 4
  • 8
1

Saving macros in plain file without plugins.

Having for example macro m . In insert mode, at e.g beginning of new line, then type 3 keys

ctrl-r ctrl-r m

the macro with escapes will be put in that line. (editable as needed) In another line name/comment it. Add other macros if you wish - then save this file as e.g. :w my_vim_macros

When you want to reuse such saved macro(s): place cursor on the beginning of macro string, then 4keys

"xy$

and your macro (this line) will be yanked to register x.

1

I'm answering what I do to add Escape in macro.

Using in vim editor

:let @a='iabcjj'

here I map <ESC> to jj by

.vimrc file

imap jj <esc> 
vusan
  • 5,221
  • 4
  • 46
  • 81