Nerdtree: map Ctrl-m to :NERDTreeToggle<CR> will also map ENTER key to :NERDTreeToggle<CR>

Created on 12 Nov 2017  路  5Comments  路  Source: preservim/nerdtree

NERDTree is an excellent plugin, But I am suffering a lot by a problem:

My config in ~/.vimrc
map <C-m> :NERDTreeToggle<CR>

when I press ENTER key the:NERDTreeTogglecommand is excuted.

if I use :map command to see key maps, there's a entry:
<CR> :NERDTreeToggle<CR>
But no <C-m> map.

Can I only map<C-m>but not ENTER key?
Thanks for your help.

Most helpful comment

I searched a result.

Special keys such as Tab, Backspace, Enter and Esc are encoded as control characters.
Control characters are not printable, they display in the terminal as ^ and are intended to have an effect on applications.

Ctrl+I = Tab
Ctrl+J = Newline
Ctrl+M = Enter
Ctrl+[ = Escape

All 5 comments

I don't really know why, but <C-M> appears to be interpreted as the same key as <CR>. Try a simpler mapping like:

:nnoremap <C-M> :echo 'test'<CR>

... and you'll notice that <CR> is also mapped. Again, I don't know why, but this is also indicated in the documentation accessed by :help Ctrl-m.

This isn't really a NERDTree issue, but I'll leave it open for a while in case anyone wants to chime in with an explanation.

I searched a result.

Special keys such as Tab, Backspace, Enter and Esc are encoded as control characters.
Control characters are not printable, they display in the terminal as ^ and are intended to have an effect on applications.

Ctrl+I = Tab
Ctrl+J = Newline
Ctrl+M = Enter
Ctrl+[ = Escape

Thanks, @yuanjianpeng. Looks like you've found an answer. I'll close this issue now.

This redundancy is built into the ASCII code, and there's no real way to get around it. <C-m> is the same as <CR>, <C-i> is the same as <Tab>, and <C-h> is the same as <BS>. There are other control key combinations for other unprintable characters, but they aren't relevant to Vim. This post - https://superuser.com/questions/763879/why-are-special-characters-such-as-carriage-return-represented-as-m - describes the situation in more detail.

I discovered this phenomenon myself when I wanted to learn and start using jumplist navigation. My vimrc mapping - nnoremap <Tab> <C-W>w was interfering with the <C-i> command to jump to the newer cursor position in the jump list. So I had to remove the mapping I'd set up and retrain my fingers.

Just want to point out that this is a great find, @PhilRunninger. You've helped me learn about something that always puzzled me!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

milushov picture milushov  路  4Comments

BigfootN picture BigfootN  路  5Comments

NikosEfthias picture NikosEfthias  路  3Comments

Coleej picture Coleej  路  3Comments

kristijanhusak picture kristijanhusak  路  3Comments