How-to customize NERDTree behavior
-
19-06-2021 - |
Question
I recently added NERDTree and NERDTreeTabs to my vim configuration and I'm quite happy about it.
However, I wanted to configure it a bit further and have the following behavior:
Ctrl+Left:
If inside the NERDTree, do nothing.
If inside the edited file, go to the opened NERDTree, opening one first if none is present. I guess
NERDTreeTabsToggle
should be used, but to use it, I must be able to detect if a NERDTree is already open, not to close it by "toggling" it.
Ctrl+Right:
If inside the NERDTree, go back to the edited file without closing NERDTree. (Just like what Ctrl-w, Right would do.)
If inside the edited file, hide/close the NERDTree.
However, my Vim script skills are way to low for me to get any satisfying result. For instance, I can't figure out how to check what the current active window is or how to write the appropriate conditional statements.
Can someone help me with that ?
Thank you.
Solution
If finally found a way.
I edited the nerdtree_plugin/vim-nerdtree-tabs.vim
file, adding these functions:
" === Stepped Open/Close functions ===
" focus the NERDTree view, creating one first if none is present
fun! s:NERDTreeSteppedOpen()
if !s:IsCurrentWindowNERDTree()
if s:IsNERDTreeOpenInCurrentTab()
call s:NERDTreeFocus()
else
call s:NERDTreeMirrorOrCreate()
endif
endif
endfun
" unfocus the NERDTree view or closes it if it hadn't had focus at the time of
" the call
fun! s:NERDTreeSteppedClose()
if s:IsCurrentWindowNERDTree()
call s:NERDTreeUnfocus()
else
let l:nerdtree_open = s:IsNERDTreeOpenInCurrentTab()
if l:nerdtree_open
silent NERDTreeClose
endif
endif
endfun
I also made a pull-request to the author to ask him if he wants to add theses functions upstream.
The author added my pull-request to the main repository, so with the next release, you can just call the functions directly.
Hope it helps people.