2
nnoremap <leader><cr> :execute "normal! i\\\<cr>\<esc>"

When I run that, it says:

E114: Missing quote: "normal! i\\\
E15: Invalid expression: "normal! i\\\

What's wrong with this? I'm trying to press i to go into insert mode, add a \ character, enter a newline, then escape.

4

You used the following command:

nnoremap <leader><cr> :execute "normal! i\\\<cr>\<esc>"

Here is the command which does what you want:

:nnoremap <leader><cr> :execute "normal! i\\\n"<cr>

First, <cr> has special meaning to :map (and :abbrev and a couple others). Most of the rhs of a mapping is processed literally, as if you were typing those characters. With <cr>, :map acts as if you hit Enter. That leaves you submitting :execute "normal! i\\\ ... an incomplete command and thus you get the "Missing quote" error. \n has no such special meaning, it's just another character and is passed to :execute in the string parameter.

Second, you don't need to explicitly escape just like you wouldn't if you directly entered :execute "normal! i\\\n". Once you hit Enter in that case (which we're doing in the mapping with the <cr> at the end) the insert occurs and you are returned back to command line.

If you really want to use <esc> ;) here's another way to do it...and I'm sure some folks would prefer this version...

:nnoremap <leader><cr> :star<cr>\<cr><esc>

(:star is short for :startinsert)

  • I'm sorry, but I don't understand what I was doing wrong above. I see the difference between \n and \<cr>, but why does one work and the other not? – Daniel Kaplan Nov 19 '17 at 6:39
  • I updated my answer with an explanation. – B Layer Nov 19 '17 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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