Open NERDTree when vim is opened without args, but not with - - vim

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

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 autocmd - Run command on WinEnter for every buffer name EXCEPT {name}

I have an autocmd which runs every time I exit and enter a window. It turns off the number for every window except the current one.
au WinEnter * setlocal number
au WinLeave * setlocal nonumber
However, I do not want line numbers in a NERDTree window. Setting g:NERDTreeShowLineNumbers to 0 doesn't help either. Is there any way of having the autocommand run on WinEnter only when the window is not a NERDTree window?
My best guess:
au WinEnter * if !exists("b:NERDTreeType") | setlocal number | endif
au WinLeave * if !exists("b:NERDTreeType") | setlocal nonumber | endif
Note: I do not use NerdTree so I have not tested any of this.
The following works when vim is opened with a file, as well as when you switch in and out of NERDTree windows:
au FileType * if !(&ft ==? "nerdtree") | setlocal number | endif
au WinEnter * if !(&ft ==? "nerdtree") | setlocal number | endif
au WinLeave * if !(&ft ==? "nerdtree") | setlocal nonumber | endif
Tested on CentOS Linux 7.6.1810 with
vim 7.4
NERDTree 5.0.0

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

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