vim: How to show directory tree with vi command - vim

Every time I want to show directory tree, I do it like this: vi [filename], then :NERDTree . How to directly show direcotry tree just with vi [filename]?

If you wanna see directory tree on opening vi, just put
autocmd vimenter * NERDTree
in your vimrc

Related

Open nerdtree automatically when start vim on a folder and don't show two tree viewers

I would like to open NERDtree automatically but only when passing a folder argument in command line, and that it would not open two tree viewers, only NERDtree. For example, if I am on folder 'rootFolder', which has a 'childFolder' and I run:
nvim childFolder
I would like it to open vim like if I had done:
cd childFolder
nvim
:NERDtree
And if I open a file or empty I would like it to not show nerdtree. That way if I just work on a single file like:
nvim
or
nvim test.js
or
nvim reminder.txt
I don't need to see NERDtree, because I don't need it, it's just a test script or a notes file I am leaving myself.
I found a workaround, add these two lines in .vimrc:
let g:NERDTreeHijackNetrw = 1
au VimEnter NERD_tree_1 enew | execute 'NERDTree '.argv()[0]
That's exactly how NERDTree behaves if 'NERDTreeHijackNetrw' is set to 1 (which it is by default.)

Using NERD tree as a Vim sidebar

I'm a new user of NERD tree in Vim and am obviously not that much familiar with its features.
When I'm using :NERDTreeToggle, the sidebar window always opens my home directory, ~. How can I change the default directory (like open a project in Sublime Text)?
Also, how can I keep this sidebar window open in all of the new tab windows (something like the Sublime Text sidebar)? Or at least, is there another alternative to this task?
In your .vimrc file, add the following code, which will by default open Vim with a NERD tree sidebar of the current directory. So if you are in the projects directory and you type "vim" it will open Vim with a sidebar on the left showing all the files and directories in the projects folder
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif
If you want toggle on and off the sidebar, just add this into your .vimrc file so that if you want to toggle the sidebar, just type Ctrl + N:
map <C-n> :NERDTreeToggle<CR>
Vim has no concept of "project".
The closest you can get without installing clunky plugins is the "current directory": when you start Vim, the "current directory" is set to the directory where you started Vim.
In your shell, this is easy to manage:
$ cd /path/to/project
$ vim
:pwd --> /path/to/project
If you use gVim or MacVim, the "current directory" is usually set automatically to $HOME if you start Vim without a file so, either you find a way to start Vim in an arbitrary directory or you use :cd /path/to/dir as soon as possible.
Without argument, the :NERDTree* commands open the NERD tree window in the "current directory".
You can use :NERDTreeToggle /path/to/dir to make it display the content of a specific directory.
Or you can make sure you start Vim from your project's directory and NERD tree will do what you want it to do.

Change root in NERDTree

I tried to change the NERDTree root with
:BookmarkToRoot <bookmark>
command and with C letter but when I close vim and open vim the root is not changed.
Do you know how to change the root forever?
You could try something like:
nnoremap <leader>n :NERDTree /path/to/folder<CR>
I have this in my .vimrc:
set autochdir
let NERDTreeChDirMode=2
nnoremap <leader>n :NERDTree .<CR>
so that NERDTree always opens in the current folder.
With the 1st line, the working directory is always the one where the active buffer is located.
With the 2nd line, I make sure the working directory is set correctly.
With the 3rd line, I hit <leader>n to open NERDTree.
As i don't want vim to change directories automatically, i just use:
com! -nargs=1 -complete=dir Ncd NERDTree | cd <args> |NERDTreeCWD
so you can just do a ":Ncd path/you/like/" with path completion.
Quickest what worked form me was just :cd new-root, close :q and reopen :NERDTree.
I simply changed the “Working Directory:” of my GVim launcher to where I wanted the root to be.

NERDTree Load Particular Directory Automatically

I'm using gvim in windows.By default nerdtree loads C:\ drive as default.I like to change it to E:\ drive.How to achieve this?
Whenever I try to start NERDTree using :NERDTree command, I get this error E492: Not an editor command: NERDTree
I have the following code in my vimrc file
cd ~/documents
map <F2> :NERDTreeToggle<CR>
" open Nerd Tree in folder of file in active buffer
map <Leader>nt :NERDTree %:p:h<CR>
The cd command is not NerdTree specific. It just changes the working directory when Vim starts to something which for me is sensible.
From the NERDTree help-file:
:NERDTree [<start-directory> | <bookmark>] *:NERDTree*
Opens a fresh NERD tree. The root of the tree depends on the argument
given. There are 3 cases: If no argument is given, the current directory
will be used. If a directory is given, that will be used. If a bookmark
name is given, the corresponding directory will be used. For example: >
:NERDTree /home/marty/vim7/src
:NERDTree foo (foo is the name of a bookmark)
In my vimrc file,I use this autocmd vimenter * NERDTree G:\.The G:\ should be set your default drive,but, it's only the NERDTree's work dir. add cd G:\change the vim start work dir.Just like Jeromy Anglim said that.

How to add NERDTree to your .vimrc

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
}

Resources