Stop NERDTree opening when vim starts - vim

I have NERDTree plugin installed but it is always present when I start vim like this:
vim .
I only want it to open on demand.
How can I stop it opening whenever I start vim?

Try adding
let g:NERDTreeHijackNetrw=0
to your vimrc and make sure there is no other line that sets this variable.

$ vim . always opens a file explorer. If you don't have NERDTree or NERDTree is configured as per ZyX's answer you'll get netrw by default anyway.
I wonder what would happen if the netrw plugin was somehow removed.

In your vimrc file, do below steps
comment autocmd VimEnter * NERDTree, which stops opening nerdTree
by default
paste map <C-n> :NERDTreeToggle<CR> in vimrc, this will open
NerdTree only when 'Ctrl+n' is pressed from keyboard

Related

Open file at Vim startup

I like to have Vim open a cheatsheet at startup. Adding this to _vimrc works nicely with MacVim:
:e *path-to-cheatsheet*
but on Windows, vim displays a message box at startup:
"~\Dropbox\vimfiles\myvim\vimcheat.txt"
"~\Dropbox\vimfiles\myvim\vimcheat.txt" [unix] 52L, 1735C
When the message box is dismissed, Vim completes startup and opens the cheatsheet. How do I get this to work cleanly with Windows Vim?
The ~/.vimrc is processed at the very beginning of :help initialization. You can use the VimEnter event to open the file after all the startup stuff is done:
autocmd VimEnter * edit path/to/cheatsheet
If you don't want to see the messages, use silent edit. If you don't care about errors (if the cheatsheet isn't there), use silent!.

Disable or close NERDTree on opening "vim ."

I'm basically working in an environment where I sometimes use ctrlp and sometimes nerdtree. But when I start vim using vim . it always opens NERDTree. I've tried various .vimrc config commands:
let NERDTreeQuitOnOpen=1
let g:nerdtree_tabs_open_on_gui_startup=0
But they all don't work. Is there some whay I can disable the NERDTree directory listing when VIM opens? Note, I don't like just using the vim command by itself because I'm usually opening VIM when I'm in the application directory.
Thanks!
Netrw plugin opens when vim opens a directory. NerdTree takes over netrw and therefore opens when editing a directory.
vim . is equivalent to :e .
Just open vim without the . and you will be good

How to refresh in NERDTree plugin

When I open a file in vim with (Directory A in) NERDTree, it works well.
But if I open one more file in another directory (Directory B), it doesn't refresh to show the contents of directory B (While it still shows directory A).
Can NERDTree automatically refresh by itself?
From https://gist.github.com/geekontheway/2667442 : just hit the r or R key to refresh the current tree. Could be mapped to auto refresh in .vimrc
Keymap to Refresh NERDTree
Instead of switching to the NERDTree window, hitting R and switching back, I use a custom map that does it for me:
nmap <Leader>r :NERDTreeFocus<cr>R<c-w><c-p>
Once set, pressing Leader + r would refresh NERDTree.
Note: Since I also use CtrlP, my actual key map has a last step to refresh CtrlP after refreshing NERDTree
I detested the idea of having to manually refresh my NERDTree plugin. So, I've added this to my .vimrc:
map <C-n> :call NERDTreeToggleAndRefresh()<CR>
function NERDTreeToggleAndRefresh()
:NERDTreeToggle
if g:NERDTree.IsOpen()
:NERDTreeRefreshRoot
endif
endfunction
Now, NERDTree refreshes every time I open it.
After you have opened the new file just issue the :NERDTreeFind command. It will select the current editing file node in the NerdTree. If the node does not exists then the NerdTree will initialize a new tree with the root as the current file's directory.
You can use the autocommand to track the directory while opening vim.
au VimEnter * NERDTreeFind
For anyone seeing this on 2016, this worked for me:
autocmd CursorHold,CursorHoldI * call NERDTreeFocus() | call g:NERDTree.ForCurrentTab().getRoot().refresh() | call g:NERDTree.ForCurrentTab().render() | wincmd w
Enjoy!
NerdTree will keep pointing at the directory from which vim was originally opened no matter what new files are opened.
In order to change it, place the cursor on the desired directory node inside the NerdTree window and press cd.
NerdTree will confirm the directory change in the command line:
NERDTree: CWD is now: [new directory here]
Note that this also changes the working directory of vim in general which is important when running commands like :edit somefile.

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.

How can I make NERDTree to open on the same drive that the file that I'm editing?

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

Resources