I want to map Alt+F1 to :NERDTreeFocus while in normal vim mod and to :NERDTreeToggle while inside NERDTree view. I've tried this:
map <silent> <M-1> :NERDTreeToggle<CR>
nmap <silent> <M-1> :NERDTreeFocus<CR>
But it does not work, obviously, I don't understand how bindings work in vim. Can you please help me with it?
Your distinction between :map
and :nmap
only concerns the modes the mappings apply to; what you need here is a distinction between the scratch buffer that NERDTree uses for its user interface and all other buffers. Fortunately, Vim allows to define mappings that apply only to the current buffer: :help :map-local
In order to set this up, you need to be inside the active NERDTree buffer. Fortunately, the plugin sets a custom filetype for its scratch buffer, and so the FileType
event can be used to hook into the setup without having to directly modify the plugin or all commands that launch it:
:nnoremap <silent> <M-1> :NERDTreeFocus<CR>
:autocmd FileType nerdtree nnoremap <buffer> <silent> <M-1> :NERDTreeToggle<CR>
:noremap
; it makes the mapping immune to remapping and recursion.<M-1>
should be triggered by Alt + 1, not by F1 (unless you have a strange keyboard setup)