Search code examples
vimnerdtree

How to open NERDTree without focusing it


I want to execute (via a keymap), while in the current buffer: NERDTreeCWD and NERDTreeFind. So I would do:

map <F8> :ProjectRootExe NERDTreeCWD<CR>:NERDTreeFind<CR>

The problem is, that NERDTreeCWD focues the NERDTree, and makes the "NERDTreeFind" not work.

So I need to open the NERDTree without focusing it. My Idea would be to call :b# to jump to the last buffer:

map <F8> :ProjectRootExe NERDTreeCWD<CR>:b#<CR>:NERDTreeFind<CR>

Unfortunately it seems that :b# does not work, whole in the NERDTree.


Solution

  • You can go back to the previous window with <C-w>p; in a command-line, the equivalent :wincmd p comes handy. With this, you can write the entire mapping as a single command-line:

    nnoremap <F8> :ProjectRootExe NERDTreeCWD<Bar>wincmd p<Bar>NERDTreeFind<CR>
    

    You should use :noremap; it makes the mapping immune to remapping and recursion.