Rafael de F. Ferreira
Rafael de F. Ferreira

Reputation: 488

How to escape "<CR>" in a mapping in vimscript

I want to construct a command line within a mapping, using the :execute normal "commandstring" techinique. But I can't do it from within a mapping as vim immediately interprets the "" as a keypress. So this doesn't work:

nnoremap <leader>jj :execute "normal :tabnew foo.txt\<cr>"<cr>

I tried doubly escaping the backslash, to no effect:

nnoremap <leader>jj :execute "normal :tabnew foo.txt\\<cr>"<cr>

Now vim types in the backslash and then interprets the as a keypress.

Any way out of this?

Upvotes: 4

Views: 733

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172590

That's indeed tricky. You have to escape the < character as <lt>, so that the <cr> is not parsed as special key notation:

nnoremap <leader>jj :execute "normal :tabnew foo.txt\<lt>cr>"<cr>

Note that your example doesn't need any of this; :normal :...<CR> is the same as ...

nnoremap <leader>jj :tabnew foo.txt<cr>

Upvotes: 4

Related Questions