0

I want to toggle vim in a whole new window and as I searched, I can open vim in new window using:

:NERDTree | only

So I set this in my vimrc:

nnoremap <C-n> :NERDTreeToggle <bar> only<CR> 

and it works well, however, I cant close it pressing control n again. Is there any way to open and close (toggle) nerdtree in a new window only by pressing one keystroke.

1 Answer 1

0

I would do:

nnoremap <C-n> :if getbufvar('%', '&filetype')=='nerdtree' <bar> bd <bar> else <bar> NERDTreeToggle <bar> only <bar> endif<CR>

Or the same but in function to make it more readable

function! ToggleNERDTree()
  if getbufvar('%', '&filetype')=='nerdtree'
    bd
  else
    NERDTreeToggle
    only
  endif
endfunction

nnoremap <C-n> :call ToggleNERDTree()<CR>
2
  • Thank you so much, it works just as I want
    – Reyomi
    Commented Mar 24, 2023 at 9:33
  • Welcome to Vim! Thanks for the feedback :-) Commented Mar 24, 2023 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.