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.
Related
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.)
autochdir automatically sets the current working directory to the present file that the cursor is located on. I'd like to exclude autochdir from changing the directory if the cursor is in the NERDTree window.
For example, if the present file I'm working on is in ~/foo, and the NERDTree window is in ~/lots/of/stuff/here, I'd like to go to the NERDTree window and still have the current working directory set to ~/foo.
That way, one can apply the CD keybind, which will set NERDTree's root tree node to ~/foo. Otherwise, it's terribly inconvenient to manually change the root tree node in the NERDTree window by navigating to ~/foo, and then applying the cd keybind.
From :help autochdir:
This option is provided for backward compatibility with the Vim
released with Sun ONE Studio 4 Enterprise Edition.
Note: When this option is on some plugins may not work.
I.e. it is obsolete, inflexible and breaks plugins. I suggest you don't use it, and instead use one of the alternate, more configurable mechanisms here. For instance,
autocmd BufEnter * if &ft !~ '^nerdtree$' | silent! lcd %:p:h | endif
However, while this does not change the current directory when you pull up NERDTree, it will not preserve it, either. I'm not 100% sure what you mean in your last paragraph.
If I open a folder in vim like this:
$ mvim . # or vim .
NERDTree opens by default in full width:
How can I prevent this from happening and show default VIM welcome screen instead?
You are explicitly telling vim to start with a listing of the current directory, if you don't want that, just do $ vim.
Run vim to get the Welcome Screen.
If you open vim . you'll get a directory listing of the current working directory (Netrw or NERDTree Directory Listing).
NERDTree overrides the default file browser (netrw).
To disable directory listing by NERDTree at startup, add let g:NERDTreeHijackNetrw=0 to your ".vimrc".
This Option tells NERD tree whether to replace the netrw autocommands for exploring local directories.
Run vim --noplugin . and you see an empty buffer.
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.
When using the vim editor with the NERDTree plugin to navigate through the tree of your project, is there an easy way to create a new source code file under the currently highlighted directory?
Currently I go into my shell, add the file and then refresh the tree. There must be a better way.
Activate the NERDTree and navigate to the directory in which the new file should live. Then press m to bring up the NERDTree Filesystem Menu and choose a for "add child node". Then simply enter the file's (or directory's name) and you're done.
From vim you can run shell commands. So in this case I use:
:!touch somefile.txt
and then hit r to reload the nerdtree window.
The other thing to do is to just start the new file from within vim.
:e somefile.txt
One handy thing for this is that in my .vimrc I auto change the cwd to the directory my current file is in:
" Auto change the directory to the current file I'm working on
autocmd BufEnter * lcd %:p:h
This way if I'm editing a file and want another one in the same place the path is changed right there. Opening any file from NERDTree sets the directory to the one that file is in.