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.

4 Answers 4


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' 
  • if you are using gvim for Windows, you probably need to use c-w rather than c-v
    – Jim U
    Apr 27, 2017 at 14:14

Q: How to set a macro in vimrc or vimscript?

A: let @w = 'macro commands'

How to find macro commands?

  1. Record your macro using qw <do macro> q
  2. Expand your macro in insert mode using 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>'
  • 2
    I would say that this won't work. It will, if you prefix < with a backslash \ and enclose whole expression in double quotes instead of single ones ' => "
    – grodzik
    Apr 28, 2017 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.