Change root in NERDTree - vim

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.

Related

Stop NERDTree opening when vim starts

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

vim shortcut to open a file with current directory populated

I'm trying to add a key mapping so that it opens up command and populate it with :e /path/to/current/file
I can get the current directory using :pwd but I'm having a trouble to use it in the mapping
I think it will be along the lines of setting pwd to a variable and use that variable as such:
noremap <C-q> <C-o>:e *pwdvariable*<Space>
Should I create a function to perform this?
I guess you need
nnoremap <C-q> <C-\><C-n>:e <C-r>=fnameescape(expand('%:p:h'))<CR>
%:p:h will get the full path of the current file (without trailing slash). Read more in :help filename-modifiers.
Not exactly, what you have asked for, but maybe more helpful: Vim tip 64: Set working directory to the current file: In short, add the following line to .vimrc:
autocmd BufEnter * silent! lcd %:p:h
Interesting for you would be also Easy edit of files in the same directory.

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.

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 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