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

vim

Solution

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>

Problem

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?

Original source