One mapping to toggle NERDTree and open to current file when toggling on
Asked Answered
K

5

8

I'm trying to find a way to have a normal mode mapping that can toggle NERDTree, but when toggling on, tell NERDTree to find the current file.

I know about NERDTreeToggle and NERDTreeFind, and what I'm looking to do is essentially a combination of those two commands.

Here's my use case:

  1. When NERDTree is not open, I can hit <C-\> and NERDTree will open to the current file.
  2. I can then hit <C-\> again and NERDTree will close.
Karisakarissa answered 1/11, 2015 at 18:2 Comment(0)
K
12

The first answer didn't work for me so I came up with this:

function MyNerdToggle()
    if &filetype == 'nerdtree'
        :NERDTreeToggle
    else
        :NERDTreeFind
    endif
endfunction

nnoremap <C-\> :call MyNerdToggle()<CR>
Klaipeda answered 12/9, 2017 at 8:29 Comment(3)
It's not perfect. The mapping has to be used twice when not on NERDTree buffer and want to close it.Appolonia
I agree. However this works ideally for me when I combine with g:NERDTreeQuitOnOpen=1.Klaipeda
It's a good point. In that case it solves situations where you want to open one file at a time. I think we need patch or plugin for NERDTree itself to provide an option that would auto-find whenever NERDTree buffer gets reopened.Appolonia
Z
7

This will do exactly what you want:

nnoremap <silent> <expr> <C-\> g:NERDTree.IsOpen() ? "\:NERDTreeClose<CR>" : bufexists(expand('%')) ? "\:NERDTreeFind<CR>" : "\:NERDTree<CR>"
Zildjian answered 9/1, 2019 at 12:52 Comment(1)
In neovim v0.6 I had to quote the first expression as well ("g:NERDTree.IsOpen()")Senior
G
2
function! NerdTreeToggleFind()
    if exists("g:NERDTree") && g:NERDTree.IsOpen()
        NERDTreeClose
    elseif filereadable(expand('%'))
        NERDTreeFind
    else
        NERDTree
    endif
endfunction

nnoremap <C-\> :call NerdTreeToggleFind()<CR>
Gertrudegertrudis answered 10/5, 2019 at 8:56 Comment(0)
H
1

It is the :NERDTreeToggle command, which you actually need, and just add %

nnoremap <C-\> :NERDTreeToggle %<CR>

In the help provided to the NERD tree you can find that :NERDTree command depends on the argument given, and that in the case of :NERDTreeToggle if no NERD tree exists for this tab then this command acts the same as the :NERDTree command.

Hanshansard answered 1/11, 2015 at 18:16 Comment(1)
Not really... it seems to open a directory, not a file... besides, it doesn't work at all if the NERDTree buffor is already initiated but closed (invisible).Appolonia
S
1

After Lewis R's answer and realizing it did not work for me, I've tweaked it a little bit and came up with this

" map nerdtree to the ctrl+n
function MyNerdToggle()
    if &filetype == 'nerdtree' || exists("g:NERDTree") && g:NERDTree.IsOpen()
        :NERDTreeToggle
    else
        :NERDTreeFind
    endif
endfunction

nnoremap <C-\> :call MyNerdToggle()<CR>
Streetcar answered 30/10, 2019 at 14:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.