How to save a Vim macro that contains “Escape” key presses?

前端 未结 7 1426
迷失自我
迷失自我 2020-12-14 07:43

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

I^[A

So I saved it into my .vimrc

相关标签:
7条回答
  • 2020-12-14 08:11

    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.

    0 讨论(0)
  • 2020-12-14 08:13

    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> 
    
    0 讨论(0)
  • 2020-12-14 08:17

    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.

    0 讨论(0)
  • 2020-12-14 08:19

    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.

    0 讨论(0)
  • 2020-12-14 08:22

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

    0 讨论(0)
  • 2020-12-14 08:30

    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.

    0 讨论(0)
提交回复
热议问题