autochdir and NERDTree - vim

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.

Related

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.

Show the list of the files inside the folder of the current file

Imagine I'm editing file, and I want to show the list of the files inside the folder who belongs the file I'm editing, to edit one of them.
How can I do that? Is there any way using FuzzyFinder?
Did you even read FuzzyFinder's documentation (:help fuzzyfinder)? Quickly opening nearby files is one of that plugin's main features.
Without installing anything, you can do:
:Ex[plore]
to open the netrw file tree. See :help netrw.
You can also do:
:e <Tab>
Add these lines to your ~/.vimrc to make command line completion even better:
set wildmenu
set wildmode=list:full
and read :help wildmenu and :help commandline-completion.
set autochdir is a useful option to add to your ~/.vimrc, by the way.
change vim current directory to current file's:
:cd %:h
then
FuzzyFinder can do what you want (pick and edit). (:FufFile) I have mapping :
nmap <Leader>ff :FufFile<cr>
NERDTree can do that as well.
Depends on what you mean by showing the file.
To include the list of files in the currently edited files, you can do something like:
:read !ls /path/to/file
(it can be shortened to :cd %:h | read !ls if you don't mind if vim changes it's current directory...)
If you want to pick another file to edit, I'd suggest to take a look at NerdTree plugin (here is a little intro). Or you can simply issue:
:cd %:h | e .

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.

open vim in alternate directory in windows

using vim + NERDTree in windows is a bit of a pain for me because each time I start vim and toggle NERDTree, NERDTree takes a long time caching my whole /Windows/system32 directory.
In order to avoid that, how can I create a shortcut to open vim in say C:\users\me\vim ?
You can add somewhere in your .vimrc.
cd C:\users\me\vim
It will change the working directory of your Vim instance.
According to :help starting.txt.vimrc is loaded before plugins.

Vim :e starting directory?

I code in Vim, not an IDE.
My source code is often nested 2-3 directories deep.
~/foo$ find
xyz
bar/abc
bar/def
~/foo$ vim
// inside of vim
:e bar/abc
... some work ...
:e <-- is there a way I can have this :e start in ~/foo/bar instead of ~/foo ?
Basically, I want :e to start the directory in "pathname of last edited file"
Thanks!
There's a lot of reasons not to like autochdir as it messes up some plugins and if you end up doing :e ../../../foo.txt you are not gaining anything. Just as an idea try this cmap I knocked up
:cnoremap red edit <c-r>=expand("%:h")<cr>/
then you can type :red and get
:e /the/path/to/your/current/files/dir/
(edit: perhaps use z instead of red as there are commands that start with red)
To expand the topic, also check out the FuzzyFinder plugin and some custom mappings to rapidly jump to common files you are always editing. Eg
10 or so of your regular files should be no more than 2 keystrokes away. It helps if they are systematically named
Here's an idea I use for django.
http://code.djangoproject.com/wiki/UsingVimWithDjango#Mappings
Try the autochdir option. It will automatically change the current working directory to whatever file was most recently opened or selected. In .vimrc:
set autochdir
For more info, :help autochdir
To always change the working directory to the current file's directory I have this in my .vimrc:
if has("autocmd")
autocmd BufEnter * :lcd %:p:h
endif " has("autocmd")
Sorry, but vim's :edit command takes a path which is interpreted relative to the present working directory of the vim instance.
You do have a :cd command which you could use to :cd bar then work for a while, then :cd ...
Hope that help some.
Some time ago I asked questions related to this on the vim mailing list: http://www.mail-archive.com/vim_use#googlegroups.com/msg03266.html Maybe you will find useful tips in that thread.
I tested a lot of plugins, but since CLI based GUIs are not my taste, I simply ended up using standard vim with a few configuration settings.
As honk pointed out, this line sets the working directory to the same as the file your working on:
autocmd BufEnter * lcd %:p:h
My other tip is to use the wildmenu. It makes it easier to get an overview of the files in your current directory when you go :e and then TAB. I'm a python programmer so the last line shows how to hide auto generated files that the python interpreter spits out, but you could use it to hide java .class files or c .obj files or whatever.
set wildmode=list:longest
set wildignore=*.pyc,*pyo
:cd changes directory
:pwd prints the current one.
why not just :E? Explore directory of current file.
:help :E
This isn't exactly what you wanted, but check out NERDTree.
On vim/gVim I just have cd C:/blah/blah at the top of my vimrc. I imagine it works on all platforms.
I personally use vagrant for each project so one CD is enough, but I think you can get vim to use different config files too, -u flag I think.
Or map a key to each project you have so pressing Ctrl+F1 does cd path/to/project/1 and Ctrl+F2 does cd path/to/project/2 perhaps?
Note: I don't use any plugins

Resources