Explore filesystem/directories in vim? - vim

What is the best way/plugin to explore filesystem and to open files and directories?

The best way to explore filesystem/directories in Vim is the one that best suits your needs. As it is phrased, this question can't get an answer because there's no "way" universally agreed upon.
On the other hand, if you want to have an overview of the many ways to explore the filesystem in Vim then, yes, that is a question that can be answered. In a non-exhaustive way, though.
NERDTree and netrw are already covered. These plugins show you a tree-like list of files and directories that you can act on. Before trying NERDTree, I'd suggest you try your hands on netrw as it comes with Vim by default and offers a much wider range of features than NERDTree. You should look around on http://www.vim.org because there are a bunch of similar plugins.
On the opposite side of the spectrum, you have Vim's own file handling capabilities. Here is a sample of commands you can use from Vim to open files:
:e filename edits filename
:sp filename edits filename in an horizontal split
:vs filename edits filename in a vertical split
:tabe filename edits filename in a new tab
You have tab-completion, just like in the shell:
:e <tab> goes through all the directories/files in the working directory
You can use wildcards, of course:
:e **/*.js<tab> shows all the js files in the working directory and its subdirectories
Assuming you have set wildmenu in your ~/.vimrc, you can make tab-completion even better with an horizontal menu which can be customized further…
You can also use "args"… but that will be for another time.
Somewhere between Vim's default commands and netrw/NERDTree you can find a bunch of "fuzzy" and less fuzzy file openers more or less modeled after a feature introduced in TextMate a while ago: FuzzyFinder, LustyExplorer, Command-T, CtrlP and many other variations on the same theme. The core concept is to provide you with a list of choice that you narrow down by typing more characters in a prompt until the file ou want to edit is selected.
If you decide you want to go down the plugin road, I'd suggest you visit http://www.vim.org, compare what's there, try a few plugins and decide for yourself.
Anyway, you should get used to the basics before looking for a plugin.

Try NERD Tree, besides the tree tab it also enhances the classical directory listing as suggested by #ATOzToa.

On Windows, I find :!start explorer %:p:h to be the way to go, or :!start explorer . if I'm in the directory I want opened.
On MacVim you could probably do something similar with :!open . to open a Finder window on the current directory but I don't have a Mac handy to try it out.
Not sure what you'd use in GNU/Linux; it probably depends on your desktop manager.

Related

Split window in vim to browse files, then open file in original window

I've tried out the NERDTree plugin for vim, and it's very powerful but is probably overkill for what I use it for. I'm not anti-plugin in general, but I don't like relying on plugins when I'm not using them to their full extent.
What I'm really looking for is the ability to open the file browser in a split, similar to how :Sex and :Vex work. I'd select the file I want, which would open in the original window, and the file browser goes away.
Are there any built-in commands that would work this way?
See :help netrw for all the mappings available in :Ex and friends.
The one you want is simply P.
By the way, netrw is also a plugin.

vim plugin for directory list/file open

I'm looking for a vim plugin similar to http://www.vim.org/scripts/script.php?script_id=1325 with a directory list in which you can select a file to open. Does this exist somewhere? Wasn't able to find it myself.
NERDTree is the second best ranked plugin on vim.org and it perfectly fits your description.
There are many other variations of the same principle:
FuzzyFinder
LustyExplorer (the one I used before)
Command-T (famous among TextMate switchers, I never liked it, though)
CtrlP (the one I use now)
…
And I second :Explore.
Have you tried the :Explore command that comes with the standard vim distribution since (roughly) version 6?
See http://vim.wikia.com/wiki/File_explorer for basic help on that command.
See http://vimdoc.sourceforge.net/htmldoc/pi_netrw.html#netrw-quickhelp for detailed help on what the Explorer can do.
If you want to open an explorer without replacing the current buffer, you can use the :Sexplore command. A puerile mnemonic for that is to execute :Sex.

How can I have two panels in terminal that interact with each other?

I'm trying to switch to Vim and I'm liking it a lot, but one big issue I'm having is switching files I have to constantly :wq, cd ..., ls ..., vi ..., etc. I'd like to have two panels (vertical?). One is for coding and the other is for cd-ing/ls-ing/vi-ing. Sort of like having a file browser as in most code editors. Any ideas?
You could use NERDTree for directories navigation and CommandT for fast file searching.
I second the NERDTree plugin from #lucapette.
And also recommend Bufferlist, to quickly switch between already opened files.
Another quick solution to explore a directory would be with :E
:E ~/workspace/
Would list the files in the workspace dir in your home folder for you to open.
You can :vsp into directory (as opposed to file).
I ended up using the CtrlP plugin and ditching the idea of having a file pane. I always knew what the file name was kinda, so this works better and takes up 0 screen real estate.
https://github.com/kien/ctrlp.vim

Opening files in Vim using Fuzzy Search

I'm looking for a way to make Vim have the ability to open a file by fuzzy-searching its name.
Basically, I want to be able to define a project once, and then have a shortcut which will give me a place to type a file name, and will match if any letters match up.
This kind of functionality exists in most editors I've seen, but for the life of me I can't understand how to get Vim to do this.
Note that I'm looking for something that won't require me to have any idea where in my directory tree a file is. I just want to be able to open it by the filename, regardless of what directory it's in.
Thanks
There are two great vim plugins for this.
ctrlp:
Written in pure VimL
Works pretty much everywhere
Supports custom finders for improved performance
Most popular fuzzy search plugin for Vim
Command-T:
Written in C, VimL and Ruby
Fast out of the box
Requires +ruby support in Vim
Recommends Vim version >= 7.3
EDIT:
I use CtrlP with ag as my custom finder and it's incredibly quick (even on massive projects) and very portable.
An example of using ag with CtrlP:
if executable('ag')
" Use Ag over Grep
set grepprg=ag\ --nogroup\ --nocolor
" Use ag in CtrlP for listing files. Lightning fast and respects .gitignore
let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""'
endif
CommandT for Vim is very much the comparable feature as in TextMate. My work flow is now
1) open up MacVim
2) :cd ~/my_project
3) (I have this mapped as described in the installation help)
4) C-v the file to open the file in a vertical split, or CR to open a new horizontal split.
5) to close the split, use :bd (buffer delete)
6) to switch to another buffer, I have BufferExplorer installed, so just \be and select
This workflow is comparable to TextMate, it takes a while to get used to, and I'm still learning.
Basic solution
Simply add this to your .vimrc
nnoremap <C-p> :find ./**/*
Pressing Ctrl+p will now allow you to fuzzyfind files in your current working directory and sub-directories thereof. Use the tab key to cycle through options.
Related solution
For those who want to keep it basic i.e. no plugins, this entertaining video shows another way to achieve fuzzy file find in vim.
They actually use
set path+=**
set wildmenu
in their .vimrc to find files in current sub-directories.
For example, with :find *Murph followd by tab, I would find the files KilianMurphy2012Why.R and KilianMurphy2014ROLE.R in subdir code which I can cycle through with the tab key. The first solution above has the advantage that the relative path is also shown.
Note that your current working directory will matter and that other files on your path (:set path?) will also be found with the this type of solution. The wildmenu option adds visual information and is not essential.
For a keyboard shortcut, add
nnoremap <C-p> :find *
to your .vimrc. Now you will be able to quickly search for files inside your project/current dir with Ctrl+p in normal mode.
What about http://www.vim.org/scripts/script.php?script_id=1984 Then there is http://github.com/jamis/fuzzy_file_finder .
Also see these blog posts: http://weblog.jamisbuck.org/2008/10/10/coming-home-to-vim and http://weblog.jamisbuck.org/2009/1/28/the-future-of-fuzzyfinder-textmate
HTH

How to navigate in large project in VIM

How do you manage big projects (hundreds of files) using only VIM?
I personally start having problems in any larger than small project.
is there any way to quickly 'go to file', preferably with name completition?
same for 'go to class definition', when it is in another file
I kinda know all the VIM basics, so I don't have problem using it for writing scripts or quick editing some source code. But it gets really messy for me when I have to navigate between files.
VIM has excellent support for tags. Once you have created a tags file for your project, you can jump to a definition or declaration of a method, class, etc., including jumping across files, all inside the same editing session.
Try
:help tags
To generate a tags file for C/C++, go to your shell prompt (I'm assuming your system is *nix/Cygwin) and type
info ctags
or
ctags --help
I like simple solutions, my favorite way to navigate at the moment is:
Add to ~/.vimrc.local
set path=$PWD/**
Then type this in the editor to find the file anywhere in the current working directory (pwd)
:find user_spec.rb
You can use tab-completion on the filenames to find multiple choices as well, making this TextMate convert very happy.
I use a combination of NERDTree (directory sidebar), FuzzyFinder Textmate (go-to-file like TextMate's CMD+T), and Sessions (:h sessions) to help me deal with large projects.
I would suggest using some sessions helper plugin. I would mention what I use, but I'm not satisfied with it yet. Just Google "vim sessions".
One thing to note with getting FuzzyFinder Textmate to work is that it depends on an old version the FuzzyFinder plugin, specifically v2.16. Anything higher and you'll get errors. But it's definitely worth the trouble. While it doesn't have name completion, its search is smart so if I search for fro/time/actionsphp it will pull up the file apps/(fro)ntend/modules/(time)_tracking/actions/(actions).class.(php) (parenthesis denote what it's matching). It makes it very easy to pick out files that are only unique by their folder name.
As well as the invaluable ctags and the various associated commands. I also couldn't live without the project plugin, which allows you to have the files of interest associated with a project in a separate pane. I can't remember how much of my setup is customised, but if I want to open a source file called Debug.c, I hit:
<F12> " Opens the project pane
/De " Searches for "De", which is likely to be enough to find Debug.c or possibly Debug.h
<ENTER> " Opens the selected file and closes the project pane
I often then do:
:vsp " Vertically split the window
<F12> " Reopen project pane
# " Search back to find previous entry with the same name (to find
Debug.h if I was on Debug.c: my headers are in Headers/ and
my source is in Source/, so the headers come earlier in the list
than the source). I use * to go the other way, obviously.
<ENTER> " Open the header file in the other window and close the project window.
With this relatively short sequence, I can open any file and it's header in a vertical split. Since the project plugin window is just a text file, completion is achieved by using Vim's searching capability.
Starting in Vim 7.3, the :find command has tab-completion of filenames.
So if you set your 'path' option to contain your entire project (probably using the ** wildcard to allow recursively searching subdirectories), then you can use the :find, :sfind, :tabfind, etc. commands with completion to get to any file in your project. This also allows jumping to files directly with gf and friends if the file name is in your text, for example in an include directive.
With this method, no external tools or plugins are needed for navigating to specific files. Although, it may admittedly not be as fast or easy to use, and doesn't address the need for jumping to definitions. For definitions, I use ctags as other answers suggest.
If you are using ctags as other posters have recommended, then make sure you look at the taglist plugin.
Make sure you take the time to read the docs and learn the key bindings. Here are a few to get you started (from the TList window):
o - open file in new window
t - open file in new tab
[[ or backspace - previous file in list
]] or tab - next file in list
Exuberant ctags.
Use Ctrl-] to jump to the tag under the cursor.
Opening vim from root of your source file and extending path option to include all sub-directories therein.
For example set path+=/usr/include/c++/** for C++ headers and set path+=** for your source directory.
This ,then, opens a plethora of following possibilities.
1) Opening file by name or parts of it
:find file_name
You can use auto-completion and wildcard expansion with :find reliably. You type the name, it will locate the name. This works language agnostic.I am sure you will like it.
2) Navigating to files under cusror:
if you want to go a file path like #include "project/path/classA.h.
gf or gF - go to file under cursor.
Ctrl-6 - to come back to last cursor position after gf or gF
3) API lookup and navigating to the API location
[i or [I can be used to look up your function signature for word under cursor without leaving your workspace. [<Tab> to actually go to declaration. Use Ctrl-6 to come back to last location.
Without extending path, you can start navigating files by :Ex command and navigate and open your file. I prefer NerdTree over this though.
I use FindFile. If you open vim at the root of your project and run :FC . the plugin will cache all the filenames beneath your cwd. You can then do :FF to open a completion menu and type the name of the file you want (or rather, the first few letters).
Although I'm kinda hoping someone will point out a better solution so I can learn something, NERDTree has been good to me for getting to specific files with name completion as long as I have the tree expanded. The command when I need to get to a file is something like:
,d/foo.pyo (where foo.py is a file name)
,d to open the tree, / to enter search mode, the name (or partial name, or regex, or whatever) of the file, and then o to open.
Of course you may have to hit 'n' a few times if you didn't type enough of the filename or there are duplicates.
I admit it feels like a bit of a hack using NERDTree like this although it has gotten so far into my muscle memory by now that I don't even think about it.
Of course I use ctags too but those are only useful when you have a function near the cursor and need to get to its definition in another file or something. A lot of times I say "OK, I need to work on feature x now" and need to navigate to another file without any references nearby that ctags would really help with.
I'm using two plugins of mine:
searchInRuntime that completes filenames on command line. It is somehow similar to fuzzyfinder and lookupfile,
lh-tags which is completely experimental and undocumented. It offers two features: automatic and quick update of the tagfile on file save(ing?), and a tag selector plugged to <c-w><m-down> by default. You may want to check the renowned taglist plugin instead.
Both require my viml library lh-vim-lib.
Try SourceCodeObedinece. This one I developed to handle C++ 1Gb source files project.
I use it in pair with 0scan.
These two plugins are wrappers around the most popular Vim browsing tools: ctags and cscope.

Resources