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
Related
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().
I would like NERDTree to open on startup when I start vim without any arguments, but I don't want it to open when I use foo | vim -
I have autocmd VimEnter * if !argc() | NERDTree | endif in my vimrc already, but of course this still opens it with vim -
You can use the StdinReadPre event to set a flag to detect this:
autocmd StdinReadPre * let g:isReadingFromStdin = 1
autocmd VimEnter * if !argc() && !exists('g:isReadingFromStdin') | NERDTree | endif
Let me explain my question, what I want to do is:
From the command line calling gvim without arguments, want NERDTree open by default in my /home/user/Documents folder.
From the command line calling gvim . want to open NERDTree with the directory set to the actual directory where the command was executed from. BUT I still want NERDTree on the left and a empty buffer in the right (not NERDTree being the only window just like normally happens).
From the command line calling gvim /some/path/to/folder want to open NERDTree with the directory set to the given directory. BUT I still want NERDTree on the left and a empty buffer in the right (not NERDTree being the only window just like normally happens).
When calling gvim with an argument:
If it is a file, don't open NERDTree just the file.
If it is a directory NERDTree should work as #3
To address #1 I have:
function! StartUp()
if 0 == argc()
NERDTree ~/Documents
endif
endfunction
autocmd VimEnter * call StartUp()
autocmd VimEnter * wincmd p
What I was thinking to address #2 and #3 was:
function! StartUp()
if 0 == argc()
NERDTree ~/Documents
else
if argv(0) == '.'
NERDTree expand(getcwd())
else
NERDTree expand(argv(0))
endif
endif
endfunction
autocmd VimEnter * call StartUp()
autocmd VimEnter * wincmd p
But it doesn't work, it gives me errors and vim freezes some times. What I can do to achieve the desired effect?
Thanks for your help.
Complete solution
Does not work exactly as I expected but it's very very close. So far so god.
function! StartUp()
if 0 == argc()
NERDTree ~/Documents
else
if argv(0) == '.'
execute 'NERDTree' getcwd()
else
execute 'NERDTree' getcwd() . '/' . argv(0)
endif
endif
endfunction
autocmd VimEnter * call StartUp()
autocmd VimEnter * wincmd p
I can't give you a complete solution, but here's a hint that should resolve the errors:
The :NERDTree command takes an (optional) directory; this doesn't resolve expressions. Vim's evaluation rules are different than most programming languages. You need to use :execute in order to evaluate a variable (or expression); otherwise, it's taken literally; i.e. Vim uses the variable name itself as the argument. So change this:
NERDTree expand(getcwd())
into:
execute 'NERDTree' getcwd()
I've also left out the expand(), as getcwd() already returns a full path.
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
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.