Vim Editor open always users home directory - linux

I installed Nerd_tree plugin for vim on ubuntu 10.04. It is great plugin. But I met the following problems:
after open nerd_tree to browse the files in /etc/apache2/sites-available/, now I close it, and go on with my work. Later I want open nerd_tree again to edit another file in /etc/apache, but nerd_tree shows me always the user home directory /home/toledot, So I have to up dir->up-dir->up-dir again. Is there a way let nerd_tree to remember the path, which I have used at the last minute?
I opened nerd_tree and select one file to edit. After my update on this file, and want to select another file to make some change. But I can not go back to the nerd_tree. I tried to make double click and tab and so on. It doesn't work. What is the hotkey to switch nerd_tree and the opened files in vim?
I am absolute new with vim and plugins, so please help me and explain the solution in details. I am very thankful for any suggestion or infos.

A couple of points that may be helpful.
If you set autochdir the current working directory will be automatically be set to that of the current file. You can do this either interactively or in your .vimrc file.
The official way to open the NERDTree window seems to be with the command :NERDTree. You can map this to any convenient key sequence.
You should be able to view the helpfile with the command :help NERDTree. If you have trouble getting the help, change to the directory containing file NERD_tree.txt (in vim) and execute :helptags .
You can also add bookmarks for file/directories that you often use. See
:help NERDTreeBookmarkCommands.

What you need is a handy map to :NERDTreeToggle. Put this in your .vimrc
nmap <silent> <F4> :NERDTreeToggle<CR>
And just hit <F4> to open/close NERDTree, just like that, just as you last toggle-closed it.

You can also open a specific directory using :NERDTree /my/dir
I advise you to set autochdir so that whenever you open a file, working directory (you can find out this with :pwd) will be changed accordingly

Related

Changing NERDTree ~/.vimrc options on the fly without restarting VIM

I would like to change my sort order dynamically. At times, I want my sources to be listed alphabetical, at times I want only one extension to be shown.
I can currently get it to work by editing NERDTreeSortOrder in my ~/.vimrc.
But how do I do this without re-launching VIM?
I tried just executing ":let NERDTreeSortOrder = ['new sort order']" but that doesn't really change anything. i add it to my vimrc, quit VIM and restart and it works.
By the way, if someone can tell me how to make NerdTree show only a specific extension, that would work for me too. I know I can make NERDTree not show a particular file type by adding it to ignore, but I want the other way around.
Any ideas?
You'll need to refresh the directory (after entering let NERDTreeSortOrder=[...]).
While in the NERDTree buffer, either :
r to limit the refresh to the selected directory
R to refresh from the start of the root node.
As for whitelisting specific file extensions, have at look at this question.
Have your tried source-ing your .vimrc from within vim?
:source $MYVIMRC or :source ~/.vimrc
:source can be shortened to :so, e.g. :so ~/.vimrc

vim / file explorer: make browsing directory the current directory

I'm starting to use vim's native explorer (:E). My doubt: when I press c to "make browsing directory the current directory" nothing happens.. When I used nerdTree and did the same operation (:NERDTreeCWD), that directory was placed at the top of the exporer and the directories upper were "hidden".
It just seems pressing c is not doing anything..
NOTE: Im using the tree view on explorer, instead of the default one. I pressed i to change the view.
Im using 7.4.52
The netrw command works different than NERDTree's. It doesn't apply to the directory under the cursor, but to the one currently being browsed. So if you want to make a subdir the current one, first go into it (via <Enter>), then press c.
To expand on Ingo's answer. If you always want netrw to make the current browsing directory the current vim directory, you can set the option let g:netrw_keepdir=0 in your .vimrc.
This allows you to use netrw to browse directories and use vim to operate in that directory. For example, you can use vim's command :e <filename> to create a new file instead of netrw's command % <filename>.
To learn more about netrw I recommend reading the help pages :help netrw-quickhelp

How do I refresh files available to CommandT in vim

I'm having this occasional problem:
In macvim I create a new file in the current directory using
:e foo.coffee
I save that file and continue working in macvim. I close and restart macvim, and use <Leader>T to try to open that file but it's not being listed.
I see foo.bar in the directory and can open it manually, but why is not available for CommandT? Is there a way to sort of punch CommandT in the head and refresh the files available to it?
Thanks
You can also use <C-f> (that's control-f), when CommandT is open.
Ref: https://github.com/wincent/Command-T under the section titled:
The following mappings are active when either the prompt or the file listing has focus
(I would like to link to the heading, but the README is plain text, so there are no heading anchors).
Yes, I also have a mapping for it, but you can type the command manually if you so like:
noremap <F5> :CommandTFlush<CR>
Typing this in mvim worked out for me in the end:
:ClearCtrlPCache

Is there a way to make NERDtree load only directories that I specify?

Is there a way to make NERDtree load only directories that I specify? I don't see how to do this.
One idea could be to use bookmarks for that purpose. For doing that, use the following sequence of commands:
Start VIM and NERDtree inside it.
Navigate to the directory you want to visit often, lets name that directory example my_proj.
Enter the command: Bookmark my_proj.
You may now open the list of Bookmarks by pressing B.
Navigate to the bookmark you want to open.
Press RETURN there.
The bookmark seems to be persistent, so you are now able to open a new VIM editor, and revisit the directories you have bookmarked. And you could even start the NERDtree with an additional argument, which could be a bookmark or directory (thank's david for the hint). So by entering NERDtree my_proj, everything is started fine.
Another idea could be to insert in your .vimrc file the following line:
:cd c:/path/To/my_proj
so when you start then NERDtree, your project directory should be in focus.

GVim - How to handle multiple files

Sorry to ask such a novice question but I am looking for a way to handle multiple files. I dont want to type huge file paths to open every file using :tabnew and :e commands
Fuzzy Finder is a handy plugin to quickly find and open files.
Basically you have to only type a few letters like test and you'll get a pop-up menu to open in your current path :
footest.c
bartest.h
footest.h
...
It is a bit slow when used on NFS but it is useful if you don't want to type long path and file names.
Alternatively if you don't want to use any plugin, by default gvim/vim includes a file browser called netrw.
To start it, just type :e . you'll get the content of your current directory, you can then navigate through the directory structure quite easily. (There is even commands to delete, rename, etc like a standard file explorer)
:help netrwfor more information.
A couple of tips that you might be interested in:
You can configure Vim so that the
current directory "follows" the
directory of the file you are
currently editing. That way you can
edit another file from the same
directory without having to type the
full path. This can be achieved by
putting either set autochdir or
autocmd BufEnter * lcd %:p:h in
your .vimrc
You can use wildcards with tab
completion. e.g. to edit
a_file_with_a_long_name.txt you could
do :e a*long and then press
Tab followed by
Return.
Usually, vim supports buffers for that. Use :badd to add buffer, :bdelete to remove it and :ls (or :buffers) to list all opened buffers. I believe, GVim supports these features too.
For example, if you wanna edit all .rb files in your app/controllers/pages dir (in the case of Rails project), you type vim app/controllers/pages/*.rb in your terminal and then edit the first file (buffer) in the vim window. When you've done with all changes, save changes as usual with :w (note: do not use q! option - this will close all your buffers you've opened) and then use :bn<tab> (or fully, :bnext) or :bprevious to switch to the next file (buffer). When you run :bnext on the last buffer, you'll be dropped to the first one.
You can open a directory in Vim, search for the file o directory you are looking for with '/' and type [enter] to open it.

Resources