0

I am currently using NERDTree in Vim. I often have multiple tabs open and I switch between them using the gt key command.

Current view with tabs: vim tab 1 vim tab 2

I currently experience two issues with NERDTree

  1. When having multiple tabs, the NerdTree view is not 'static'. I mean, if in tab 1 the buffer of NerdTree is on the first line, on tab 2 it could be on the bottom. This makes it annoying and I have to search my file again when switching tabs.
  2. IntelliJ has a handy function to find the currently opened file in the project tree, can this be done in Vim with NerdTree too?

I would like something, that when I scroll in NerdTree in a specific tab, the scroll is applied in all the NerdTree's in the other tabs too. And I would like to implement a shortcut to find the currently opened file in NerdTree.

I found a script here, but it is not solving much:

" Check if NERDTree is open or active
function! IsNERDTreeOpen()
  return exists("t:NERDTreeBufName") && (bufwinnr(t:NERDTreeBufName) != -1)
endfunction

" Call NERDTreeFind iff NERDTree is active, current window contains a modifiable
" file, and we're not in vimdiff
function! SyncTree()
  if &modifiable && IsNERDTreeOpen() && strlen(expand('%')) > 0 && !&diff
    NERDTreeFind
    wincmd p
  endif
endfunction

" Highlight currently open buffer in NERDTree
autocmd BufRead * call SyncTree()

This is my full .vimrc:

set nocompatible
" Initialisation de pathogen
call pathogen#infect()
call pathogen#helptags()

set number
set tabstop=4
set shiftwidth=4
set expandtab
set smartindent
set autoindent

set backspace=indent,eol,start

filetype plugin indent on
syntax on
runtime! config/**/*.vim

autocmd VimEnter * NERDTree
autocmd BufWinEnter * NERDTreeMirror
"autocmd BufWinEnter * NERDTreeMinor

set guifont=Source\ Code\ Pro\ SemiBold:h16

let NERDTreeMinimalUI = 1
let NERDTreeDirArrows = 1
set laststatus=2

let g:airline_powerline_fonts = 1
let g:airline_theme='minimalist'

colorscheme afterglow


set guioptions-=r  "remove right-hand scroll bar
set guioptions-=L

set scrolloff=5


"nnoremap <ScrollWheelUp> k
"nnoremap <ScrollWheelDown> j

autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif


"OMNISHARP C# CONFIG
"" Use the vim-plug plugin manager: https://github.com/junegunn/vim-plug
" Remember to run :PlugInstall when loading this vimrc for the first time, so
" vim-plug downloads the plugins listed.
silent! if plug#begin('~/.vim/plugged')
Plug 'OmniSharp/omnisharp-vim'
Plug 'w0rp/ale'
Plug 'beyondwords/vim-twig'
Plug 'iamcco/coc-tailwindcss'
Plug 'rodrigore/coc-tailwind-intellisense', {'do': 'npm install'}
call plug#end()
endif

" Note: this is required for the plugin to work
filetype plugin indent on

" Use the stdio OmniSharp-roslyn server
let g:OmniSharp_server_stdio = 1

" Set the type lookup function to use the preview window instead of echoing it
"let g:OmniSharp_typeLookupInPreview = 1

" Timeout in seconds to wait for a response from the server
let g:OmniSharp_timeout = 5

" Don't autoselect first omnicomplete option, show options even if there is only
" one (so the preview documentation is accessible). Remove 'preview' if you
" don't want to see any documentation whatsoever.
set completeopt=longest,menuone,preview

" Fetch full documentation during omnicomplete requests.
" By default, only Type/Method signatures are fetched. Full documentation can
" still be fetched when you need it with the :OmniSharpDocumentation command.
"let g:omnicomplete_fetch_full_documentation = 1

" Set desired preview window height for viewing documentation.
" You might also want to look at the echodoc plugin.
set previewheight=5

" Tell ALE to use OmniSharp for linting C# files, and no other linters.
let g:ale_linters = { 'cs': ['OmniSharp'] }

" Update semantic highlighting after all text changes
let g:OmniSharp_highlight_types = 3
" Update semantic highlighting on BufEnter and InsertLeave
" let g:OmniSharp_highlight_types = 2

augroup omnisharp_commands
    autocmd!

    " Show type information automatically when the cursor stops moving
    autocmd CursorHold *.cs call OmniSharp#TypeLookupWithoutDocumentation()

    " The following commands are contextual, based on the cursor position.
    autocmd FileType cs nnoremap <buffer> gd :OmniSharpGotoDefinition<CR>
    autocmd FileType cs nnoremap <buffer> <Leader>fi :OmniSharpFindImplementations<CR>
    autocmd FileType cs nnoremap <buffer> <Leader>fs :OmniSharpFindSymbol<CR>
    autocmd FileType cs nnoremap <buffer> <Leader>fu :OmniSharpFindUsages<CR>

    " Finds members in the current buffer
    autocmd FileType cs nnoremap <buffer> <Leader>fm :OmniSharpFindMembers<CR>

    autocmd FileType cs nnoremap <buffer> <Leader>fx :OmniSharpFixUsings<CR>
    autocmd FileType cs nnoremap <buffer> <Leader>tt :OmniSharpTypeLookup<CR>
    autocmd FileType cs nnoremap <buffer> <Leader>dc :OmniSharpDocumentation<CR>
    autocmd FileType cs nnoremap <buffer> <C-\> :OmniSharpSignatureHelp<CR>
    autocmd FileType cs inoremap <buffer> <C-\> <C-o>:OmniSharpSignatureHelp<CR>

    " Navigate up and down by method/property/field
    autocmd FileType cs nnoremap <buffer> <C-k> :OmniSharpNavigateUp<CR>
    autocmd FileType cs nnoremap <buffer> <C-j> :OmniSharpNavigateDown<CR>

    " Find all code errors/warnings for the current solution and populate the quickfix window
    autocmd FileType cs nnoremap <buffer> <Leader>cc :OmniSharpGlobalCodeCheck<CR>
augroup END

" Contextual code actions (uses fzf, CtrlP or unite.vim when available)
nnoremap <Leader><Space> :OmniSharpGetCodeActions<CR>
" Run code actions with text selected in visual mode to extract method
xnoremap <Leader><Space> :call OmniSharp#GetCodeActions('visual')<CR>

" Rename with dialog
nnoremap <Leader>nm :OmniSharpRename<CR>
nnoremap <F2> :OmniSharpRename<CR>
" Rename without dialog - with cursor on the symbol to rename: `:Rename newname`
command! -nargs=1 Rename :call OmniSharp#RenameTo("<args>")

nnoremap <Leader>cf :OmniSharpCodeFormat<CR>

" Start the omnisharp server for the current solution
nnoremap <Leader>ss :OmniSharpStartServer<CR>
nnoremap <Leader>sp :OmniSharpStopServer<CR>

" Enable snippet completion
let g:OmniSharp_want_snippet=1


" Use <C-k> to trigger completion.
inoremap <silent><expr> <S-Tab> coc#refresh()
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm() : "\<CR>"
" For Rust
call plug#end()
Plug 'phpactor/phpactor', {'for': 'php', 'tag': '*', 'do': 'composer install --no-dev -o'}
Plug 'neoclide/coc.nvim', {'branch': 'release'}
call plug#end()

" Rust Pmenu color
 """ Customize colors
func! s:my_colors_setup() abort
    " this is an example
    hi Pmenu guibg=#000000 gui=NONE
    hi PmenuSel guibg=#000000 gui=NONE
    hi PmenuSbar guibg=#000000
    hi PmenuThumb guibg=#000000
    hi Pmenu ctermbg=black
endfunc

augroup colorscheme_coc_setup | au!
    au ColorScheme * call s:my_colors_setup()
augroup END


call s:my_colors_setup()

nnoremap <silent> ll :call phpactor#UseAdd()<CR>



" HTML
" filenames like *.xml, *.html, *.xhtml, ...
" These are the file extensions where this plugin is enabled.
"
let g:closetag_filenames = '*.html,*.xhtml,*.phtml,*.twig'

" filenames like *.xml, *.xhtml, ...
" This will make the list of non-closing tags self-closing in the specified files.
"
let g:closetag_xhtml_filenames = '*.xhtml,*.jsx'

" filetypes like xml, html, xhtml, ...
" These are the file types where this plugin is enabled.
"
let g:closetag_filetypes = 'html,xhtml,phtml.twig'

" filetypes like xml, xhtml, ...
" This will make the list of non-closing tags self-closing in the specified files.
"
let g:closetag_xhtml_filetypes = 'xhtml,jsx'

" integer value [0|1]
" This will make the list of non-closing tags case-sensitive (e.g. `<Link>` will be closed while `<link>` won't.)
"
let g:closetag_emptyTags_caseSensitive = 1

" dict
" Disables auto-close if not in a "valid" region (based on filetype)
"
let g:closetag_regions = {
    \ 'typescript.tsx': 'jsxRegion,tsxRegion',
    \ 'javascript.jsx': 'jsxRegion',
    \ }

" Shortcut for closing tags, default is '>'
"
let g:closetag_shortcut = '>'

" Add > at current position without closing the current tag, default is ''
"
let g:closetag_close_shortcut = '<leader>>'


let g:ale_linters = {
            \   'twig': ['twig'],
            \   'html': [],
            \ }


inoremap " ""<left>
inoremap ' ''<left>
inoremap ( ()<left>
inoremap [ []<left>
inoremap { {}<left>
inoremap {<CR> {<CR>}<ESC>O
inoremap {;<CR> {<CR>};<ESC>O

au FocusGained * redraw
set tags=tags

function! s:is_tailwind()
  return !empty(findfile('tailwind.config.js', '.;')) ||
       \ !empty(findfile('config/tailwind.config.js', '.;'))
endfunction

autocmd BufEnter *.html,*.slim,*.twig if s:is_tailwind() |
      \   setlocal omnifunc=tailwind#Complete |
      \ endif


" Check if NERDTree is open or active
function! IsNERDTreeOpen()
  return exists("t:NERDTreeBufName") && (bufwinnr(t:NERDTreeBufName) != -1)
endfunction

" Call NERDTreeFind iff NERDTree is active, current window contains a modifiable
" file, and we're not in vimdiff
function! SyncTree()
  if &modifiable && IsNERDTreeOpen() && strlen(expand('%')) > 0 && !&diff
    NERDTreeFind
    wincmd p
  endif
endfunction

" Highlight currently open buffer in NERDTree
autocmd BufRead * call SyncTree()

3
  • Obligatory: vimcasts.org/blog/2013/01/…
    – D. Ben Knoble
    Commented Dec 9 at 17:48
  • 1
    I used to use NERDTree until a few months ago, and I left it for many reasons, one is the same you address. I then started to use Netrw only with vinegar. And I feel quiet happy with that. Distraction free, simple and efficient.
    – simzz
    Commented Dec 9 at 22:19
  • Do you still have something open in your question? How can we help you further? Commented Dec 11 at 15:21

1 Answer 1

0

For the NERDTree state across different tabs you could follow the NERDTree recipes:

Can I have the same NerdTree on every tab?

" Open the existing NERDTree on each new tab.
autocmd BufWinEnter * if &buftype != 'quickfix' && getcmdwintype() == '' | silent NERDTreeMirror | endif

But what some are doing is using a bufferline that simulate graphically the tab look & feel like:

For the synchronization between the buffer and the tree you could use the :NERDTreeFind command that highlight in the NERDTree tree the file corresponding to the buffer.

You could map it to Ctrl F width:

nnoremap <C-f> <cmd>NERDTreeFind<CR>

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.