How do I add NERDTree to my .vimrc?
Okay, the previous version was a bit terse, but the answer you're looking for is to add the line below into your ~/.vimrc file. It tells Vim that you want to setup a command to run when Vim starts, but since it depends on various plugins to be loaded, you don't want to run it until all initialization is finished:
autocmd VimEnter * NERDTree
If, however, you're annoyed by the fact that the cursor always starts in the NERDTree window, you can add a second autocommand that will move the cursor into the main window:
autocmd VimEnter * NERDTree
autocmd VimEnter * wincmd p
I like to see NERDTree only when I start vim without file arguments, so I added this to my .vimrc:
autocmd VimEnter * if !argc() | NERDTree | endif
Are you on a Windows or unix-y system?
If you're on a unix-y system you put plugins in ~/.vim/plugin. Here's what my plugin directory looks like:
$ ls ~/.vim/plugin
NERD_tree.vim scratch.vim scratchfind.vim
After that it starts working right away. Try running vim like this:
$ vim .
It should open the current directory in the NERD tree view.
If you're on Windows you put plugins here: C:\Program Files\Vim\vim70\plugin
To get NERDTree to load automatically when you start up vim, run it like this from the command line:
$ vim -c "NERDTree" some_file.txt
You can set an alias for this in your .bashrc:
alias vimt='vim -c "NERDTree" $1'
Now whenever you run vimt (instead of vim) you'll also open up NERDTree on the left side of the window.
You could also add a shortcut key to start NERDTree in your .vimrc this way:
function OpenNERDTree()
execute ":NERDTree"
endfunction
command -nargs=0 OpenNERDTree :call OpenNERDTree()
nmap <ESC>t :OpenNERDTree<CR>
Now when you hit Esc then t it will pop open NERDTree.
Per the NERDTree instructions you can just use pathogen.vim. Install it with:
mkdir -p ~/.vim/autoload ~/.vim/bundle; \
curl -Sso ~/.vim/autoload/pathogen.vim \
https://raw.github.com/tpope/vim-pathogen/master/autoload/pathogen.vim
Add this to your .vimrc:
execute pathogen#infect()
then install NERDTree:
cd ~/.vim/bundle
git clone https://github.com/scrooloose/nerdtree.git
And if you want to open a NERDTree automatically when Vim starts up, add the following to your .vimrc:
autocmd vimenter * NERDTree
The answers here have a minor problem.
If you call vim --noplugin or use a script that uses --noplugin mode such as vimpager, it will cause this error:
Error detected while processing VimEnter Auto commands for "*":
E492: Not an editor command: NERDTree
To avoid this, put the command in ~/.vim/after/plugin/NERD_tree.vim instead:
autocmd VimEnter * NERDTree
And it might also be a good idea to test that NERDtree is available as well, i.e.:
if exists("loaded_nerd_tree")
autocmd VimEnter * NERDTree
endif
" NERD Tree
nmap <silent> <special> <F2> :NERDTreeToggle<RETURN>
I like to open NERDTree on startup, but with two requirements:
Open NERDTree only when there are no arguments (file arguments)
Display only the NERDTree window (I don't want to display the main window)
I use this command:
autocmd VimEnter * if !argc() | NERDTree | wincmd p | q | endif
Update (9 Jan 2022)
I've found a more performant way to meet the two requirements I've specified above.
Remove the autocmd, which I've mentioned above, from the .vimrc file. Instead, create an alias like this:
vim() {
if [ $# -eq 0 ]; then
/usr/bin/vim ./
else
/usr/bin/vim "$#"
fi
}
Related
I want netrw to autoload when I launch vim using the terminal. Completely new to linux/ubuntu. Is there any way of doing that?
Adding the following to your .vimrc (Vim's configuration file, located in the root of your home directory) will cause Vim to automatically load Netrw after starting up.
" Open Netrw after Vim starts up
augroup InitNetrw
autocmd!
autocmd VimEnter * :silent! Explore
augroup END
A problem with the preceding approach, as implemented, is that Netrw will also load when you use Vim with an argument to open a specific file. A workaround is to use the following modification, based on the suggested approach in Netrw's documentation (:help netrw-activate).
" Checks if there is a file open after Vim starts up,
" and if not, open the current working directory in Netrw.
augroup InitNetrw
autocmd!
autocmd VimEnter * if expand("%") == "" | edit . | endif
augroup END
The following pages have more details on autocommands and the .vimrc configuration file.
https://learnvimscriptthehardway.stevelosh.com/chapters/12.html
https://learnvimscriptthehardway.stevelosh.com/chapters/14.html
https://learnvimscriptthehardway.stevelosh.com/chapters/07.html
And the following code block in your vimrc:
set autochdir
let g:netrw_browse_split=4
augroup InitNetrw
autocmd!
autocmd VimEnter * if argc() == 0 | Lexplore! | endif
augroupend
Kind of does what #dannyadam suggested. But opens the netrw pane as a side bar on the right. If you want to be on the right use Lexplore without the bang(!).
I'd like to open CtrlP if I am opening a directory with vim, but not a file. I like to have it automatically open in I just open a directory for convenience. However, it is slightly inconvenient if I know exactly which file I want to open because of the added loading time.
Currently I just have this in my .vimrc:
autocmd vimenter * CtrlP
Thanks in advance for any responses!
You can write a function to test the args to see if a single directory was passed in and if it was, execute CtrlP. Here is a very rudimentary solution:
function! MaybeCtrlP()
if argc() == 1 && isdirectory(argv()[0])
" Uncomment this to remove the Netrw buffer (optional)
" execute "bdelete"
execute "CtrlP"
endif
endfunction
autocmd VimEnter * :call MaybeCtrlP()
In .bash_profile, create an alias:
alias vimCtrlP="vim +CtrlP"
Then every time you need this, use vimCtrlP as you would issue vim in shell, followed by the directory.
I currently have the following in my .vimrc:
au VimEnter * NERDTree
au BufWinEnter * NERDTreeMirror
This I believe launches NERDTree when I open Vim, as well as when I open a new tab using a plugin. Currently, when I launch Vim using
vim
at the command line, the cursor is sitting in NERDTree ready for me to navigate my files, which is great. However, when I use
vim my-file.txt
the cursor remains in the NERDTree window. I'm aware that I can add this to my .vimrc:
au BufNew * wincmd l
But that means the cursor will always be placed in the window to the right of NERDTree, even if I don't specify a file.
Does anyone have any ideas?
You can make the command conditional on whether arguments were passed to Vim:
:au VimEnter * if argc() > 0 | wincmd l | endif
(I'd use the VimEnter event for that; just make sure this autocmd comes after the one that opens NERDTree.)
PS: There are a couple of related questions here:
Auto-open NERDTree in "EVERY" tab
How do you add NERDTree to your vimrc?
NERDTree shows in viewport disk c: regardless from which disk do I open the file.
When I use gvim in windows I open files using:
gvim.exe --remote-tab-silent [FILE]
I'm loading NERDTree with this line in _vimrc:
au VimEnter * NERDTree
Can NERDTree automaticaly change drive to correct drive somehow?
Actually, my last answer does not work because once the NERDTree have been opened, it does not open again in the new buffer dir. It must work similarly to NERDTreeFind but it does not have a Toggle feature.
I made a function and mapped it to my key and now it works perfectly even opening the Ruby project if you have the vim-rails plugin.
Add this to your vimrc:
function! NTFinderP()
"" Check if NERDTree is open
if exists("t:NERDTreeBufName")
let s:ntree = bufwinnr(t:NERDTreeBufName)
else
let s:ntree = -1
endif
if (s:ntree != -1)
"" If NERDTree is open, close it.
:NERDTreeClose
else
"" Try to open a :Rtree for the rails project
if exists(":Rtree")
"" Open Rtree (using rails plugin, it opens in project dir)
:Rtree
else
"" Open NERDTree in the file path
:NERDTreeFind
endif
endif
endfunction
"" Toggles NERDTree
map <silent> <F1> :call NTFinderP()<CR>
It should work now.
Previous answer below:
You could map the key you use to open
NERDTree like this(in .vimrc):
map <silent> <F1> :NERDTreeToggle %:p:h<CR>
This maps my F1 key to
toggle(open/close) NERDTree using the
path of the currently active buffer.
If no buffer is open, it opens in the
currently launched Macvim directory.
NERDTree provides several Ex commands to manipulate its buffer (see
:help NERDTreeGlobalCommands). Among them there is the :NERDTreeFind
command which behaves the same way as the :NERDTree command except it opens
the NERDTree buffer in the directory containing currently opened file.
So, in order to achieve the desired effect described in the question, you can
simply change the auto-command to read
:autocmd VimEnter * NERDTreeFind
I use mapping for NERDTree and in this way when I open it always opens in current dir
" NERDTree mappings
nnoremap <silent> <F9> :NERDTreeToggle <cr>
inoremap <silent> <F9> <Esc>:NERDTreeToggle <cr>
But if you open a file like gvim ~/other/dir/file NERDTree will open current dir from where gvim was called. So this is not a real solution to your problem.
Perhaps if you cd in working dir before calling gvim will solve your problem. In this case even your au VimEnter * NERDTree in _vimrc must work as you espect .
About changing directory and setting working dir set autochdir read here
Add
au VimEnter,BufWinEnter * NERDTreeFind
to your .vimrc.
VimEnter part makes it work on load.
BufWinEnter makes it happen you open a new file.
* tells it to do this with all files
NERDTreeFind is the command to run
srcs:
http://vimdoc.sourceforge.net/htmldoc/autocmd.html
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.