Vim nerdtree not able to open files - vim

I am trying to setup NerdTree on Unix. When I open gvim editor it displays the file explorer bar on the side. However, I am not able to click and open files nor use the vim keys. Not sure what's going on here.
I have the following lines in my vimrc for NerdTree setup:
autocmd VimEnter * NERDTree
autocmd VimEnter * wincmd p
nmap <silent> <c-n> :NERDTreeToggle<CR>
let g:NERDTreeWinSize = 30
Any ideas? THanks.

In my system, nerdtree is easy to use.
just delete
autocmd VimEnter * NERDTree
autocmd VimEnter * wincmd p
nmap <silent> <c-n> :NERDTreeToggle<CR>
let g:NERDTreeWinSize = 30
and try in cmdline :NERDTreeToggle
and then map the cmd.

Related

Vim treatment vimrc if folder

I'm currently working on my .vimrc, and theres is something i don't know about.
I made this line :
"=====[ On vim load, toggle NERDTree and switch to file ]=====
autocmd VimEnter * :NERDTreeToggle | wincmd l
Which open nerdtree on load when i open vim, then switch to the main buffer after.
This work nice, but i want this cmd only when I open a folder :
vim .
But when I'm working on file only I don't need it.
vim mytext.md
I'm looking for a condition on my rc file, but I don't find it. Something like :
"=====[ On vim load, toggle NERDTree and switch to file ]=====
if typefile != 'file'
autocmd VimEnter * :NERDTreeToggle | wincmd l
endif
So if you have an idea on this...
thanks for all
This should work:
augroup vimrc
autocmd!
autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) | NERDTreeToggle | wincmd l | endif
augroup END
But I'm sure there's already a definitive solution somewhere on the internet.
See :help argc(), :help argv() and :help isdirectory().

vim: open NERDTree and move the cursor to the file editing area

I tried following the instruction in FAQ section on NERDTree github site:
"Q. How can I open a NERDTree automatically when vim starts up?"
"A. Stick this in your vimrc: autocmd vimenter * NERDTree"
It works but when I open a file the cursor stay in the NEARDTree explorer area but not in the edit area, I have to press Ctrl+w+l to move it back, what should I write in my .vimrc file to automate setting the cursor in the edit area?
Just add this second command right after:
autocmd VimEnter * NERDTree
autocmd VimEnter * wincmd p
Or if you want a one-liner
autocmd VimEnter * NERDTree | wincmd p
If you want to keep the default behavior if no file is specified (i.e. leave the NERDTree explorer only if there is a file to edit) you can use this setting:
autocmd VimEnter * if argc() == 1 | NERDTree | wincmd p | endif

How to set the cursor focus after toggling the NERDTree in vim?

By default I have turned off the NERDTree and I use F2 to toggle it.
" autocmd VimEnter * NERDTree
" autocmd VimEnter * wincmd p
nmap <silent> <special> <F2> :NERDTreeToggle<RETURN>
After turning on the NERDTree by pressing F2, the cursor is then focused on the NERDTree window. My question is how to redesign the F2 shortcut so the focus window NOT to the NERDTree window?
This should do it:
nnoremap <silent> <special> <F2> :NERDTreeToggle <Bar> if &filetype ==# 'nerdtree' <Bar> wincmd p <Bar> endif<CR>
After toggling, it checks whether it now is in the NERDTree buffer. If it is, it jumps back to the previous window.
PS: You should use :noremap; it makes the mapping immune to remapping and recursion.

Auto-open NERDTree in vim

Does someone know how to force .vimrc to auto-open NERDTree each time vim is invoked? The operation system is *nix.
au VimEnter * NERDTree
in your vimrc should do it
:he autocmd.txt for background
You can also only open Nerd Tree when there was no file on the command line:
function! StartUp()
if 0 == argc()
NERDTree
end
endfunction
autocmd VimEnter * call StartUp()
Taken from a blog post by Ovid.
One liner to open NERDTree when no file argument provided would be
autocmd vimenter * if !argc() | NERDTree | endif
OR
au vimenter * if !argc() | NERDTree | endif
The above code just checks if no argument is provided then open NERDTree.
Building on #zoul's answer, I in my case I wanted NERDTree to be open by default if I specify a directory or if I specify nothing, and not be open if I specify a single file, so I ended up with:
function! StartUp()
if !argc() && !exists("s:std_in")
NERDTree
end
if argc() && isdirectory(argv()[0]) && !exists("s:std_in")
exe 'NERDTree' argv()[0]
wincmd p
ene
end
endfunction
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * call StartUp()
If you are looking for a way to have a persistent NERDTree, that remains even when you open new tabs, you'd better use jistr/vim-nerdtree-tabs and add in your .vimrc :
let g:nerdtree_tabs_open_on_console_startup=1
The package is not maintained anymore, but it works and I don't know any equivalent.
In your vim config file (I use nvim, so for me its in ~/.config/nvim/init.vim), Add the line anywhere in the file:
au VimEnter * NERDTree

Auto-open NERDTree in "EVERY" tab

Is it possible to open NERDTree in every tab with pressing t or T in NERDTree, if yes, How?
autocmd VimEnter * NERDTree
autocmd BufEnter * NERDTreeMirror
edit: The above command seems to open the new tab in NERDTree's buffer. Instead use this as mentioned by wejrowski in the comment below :
autocmd BufWinEnter * NERDTreeMirror
I wrote a vim plugin that does this and also adds some goodies on top (i.e. keeps all trees in sync, ensures meaningful tab captions - not captions like 'NERD_tree_1' etc.).
It's here on Github: https://github.com/jistr/vim-nerdtree-tabs
autocmd VimEnter * NERDTree
autocmd BufEnter * NERDTreeMirror
autocmd VimEnter * wincmd w
This one is a little better than Dustin's one because it places the cursor directly on the file you are intending to edit for quick edits. Thanks dustin for the original example ^^
A better solution is to open NERDTree only if there are no command line arguments set.
" Open NERDTree in new tabs and windows if no command line args set
autocmd VimEnter * if !argc() | NERDTree | endif
autocmd BufEnter * if !argc() | NERDTreeMirror | endif
NERDTree is e.g. not helpful if you do a git commit or something similiar.
This is probably not the best way, but if you edit plugin/NERDTree.vim and change this:
exec "nnoremap <silent> <buffer> ". g:NERDTreeMapOpenInTab ." :call <SID>openInNewTab(0)<cr>"
to this:
exec "nnoremap <silent> <buffer> ". g:NERDTreeMapOpenInTab ." :call <SID>openInNewTab(0)<cr>:NERDTree<cr>"
it will alter the binding of 't' in the NERDTree view to first open the file and then open NERDTree. Note, that the NERDTree views will not keep in sync.
How about toggling it.
" in .vimrc
" NERDTree, Use F3 for toggle NERDTree
nmap <silent> <F3> :NERDTreeToggle<CR>
In OSX, you just need to fn-F3 to toggle NERDTree.
This problem was actually mentioned in the official Repository's Readme file including three situations related to opening NERDTree automatically:
How can I open a NERDTree automatically when vim starts up?
Stick this in your vimrc: autocmd vimenter * NERDTree
How can I open a NERDTree automatically when vim starts up if no files were specified?
Stick this in your vimrc:
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif
Note: Now start vim with plain vim, not vim .
How can I open NERDTree automatically when vim starts up on opening a directory?
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) && !exists("s:std_in") | exe 'NERDTree' argv()[0] | wincmd p | ene | endif
This window is tab-specific, meaning it's used by all windows in the tab. This trick also prevents NERDTree from hiding when first selecting a file.

Resources