I want to have a macro in my .vimrc
which when activated will enter a command and execute it.
I want something like
let @r = ':e!'
but instead of it just being entered in the command line I want it executed.
Your goal is to insert a literal ^M
character. You have two options: You can use digraphs (:h digraphs
), or the <C-v>
shortcut (:h i_CTRL-v
).
Digraphs are a way to enter hard-to-reach characters, like accented letters or special keys. If you look at the listing in :digraphs
, you will see that CR is the shortcut you want.
To enter a digraph, press Ctrlk followed by the digraph letters. In this case, it would be Ctrlk - CR (note the uppercase, it's significant).
Typing Ctrlv will "insert next non-digit literally". You can either press Ctrlv - Ctrlm, or Ctrlv - Enter – both will result in ^M
.
Before ending '
sign, press combination CtrlV followed by Enter. This will insert Enter code represented by ^M
sign, which will as you would have pressed Enter yourself.
You should get something like this:
let @r = ':e!^M'
let @w = 'macro commands'
How to find macro commands?
qw <do macro> q
Ctrl-r Ctrl-r w
<cr>
(Carriage Return) inside a mapping or a command will expand to an Enter keystroke.
So your mapping then would be
let @r = ':e!<cr>'
<
with a backslash \
and enclose whole expression in double quotes instead of single ones '
=> "