I want to save a certain macro permanently so that I can replay it in future sessions.
For instance, I record the macro which surrounds selected words with **
(like **this text is strong**
):
c
, **
, Ctrl-r
, "
, **
, Ctrl-;
(to switch to the normal mode); and From the next time, I can just select words and press @a
to replay these actions.
Now I enter :reg a
and see this output:
--- Registers ---
"a c**^R"**^[
How can I define this using the :let
function so that I can add this to.vimrc and make this macro permanent?
:let @a='c**^R"**^['
didn't work.
The ^R
and ^[
in the output of :reg a
are literal control codes, not ^
followed by R
or by [
. Usually, Vim uses a special color to hint at that difference.
You can do either of two things…
In your vimrc
, type out the macro directly, using :help i_ctrl-v
to insert the literal control codes. That is, press <Cv>
then <Cr>
to insert a literal ^R
:
let @a = 'c** " type this out <Cv><Cr> " press <Cv><Cr> to insert ^R "** " type this out <Cv><Esc> " press <Cv><Esc> to insert ^[ ' " type this out <Esc> " leave insert mode
In your vimrc
, insert the register directly between the quotes with :help i_ctrl-r
:
let @a = ' <Cr>a ' <Esc>
Note that it doesn't protect the register in any way so @a
can be overwritten accidentally. A mapping, the right hand side of which is a macro, seems safer to me:
" again, use <C-v><C-r> to insert `^R`
xnoremap <key> c**^R"**^[
" or use the angled brackets notation
xnoremap <key> c**<C-r>"**<Esc>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.