Reputation: 488
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
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