Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

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?

like image 758
Rafael de F. Ferreira Avatar asked Oct 26 '25 23:10

Rafael de F. Ferreira


1 Answers

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>
like image 64
Ingo Karkat Avatar answered Oct 28 '25 21:10

Ingo Karkat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!