Reading efficiently PATHs in Vim - vim

How can you open the following PATH by ctrl-w-f to a new window?
Path
/usr/masi/codes/11
The PATH refers to a file 11.tex.
I need to use 11 instead of 11.tex
because my LaTeX or pdflatex does not understand PATHs with tex
when I input/include the file.
Possible solutions are
to add something to .vimrc
to use perhaps differently pdflatex

In Vim 7.2, you can set the suffixesadd to .txt as below:
:set suffixesadd=.tex,.latex,.java
see :help suffixesadd

It seems you'll need to override CTRL-W_f to add a extension.
For instance, you could add the following in a tex-ftplugin:
nnoremap <buffer> <c-w>f :exe ':sp '.globpath('.', expand('<cfile>').'.*')<cr>
NB: this mapping is far from perfection. It's still need to glob on , and .*, to keep only one file (or none) if several match. And to support the no-match case.

I don't know about latex to answer from its side, but I don't see a setting to make Vim look for the path with extensions, so you will have to create a custom mapping. Unfortunately, there are no command-line equivalents to the gf and f style of commands, so you have to mimic something equivalent (sorry, untested).
function! OpenFile()
try
exec "normal! \<C-W>f"
except
if filereadable(expand('<cfile>').'.tex')
split <cfile>.tex
endif
endtry
endfunction
nnoremap <silent> <C-W>f :call OpenFile()<CR>
You can put this in an ftplugin (with option for nnoremap) to restrict it to your latex files only.
Note: If you want to cover different cases such as gf, F, you will need a more sophisticated function, or just write different functions for each.

I use vim 7.2 and it really works with default settings. If i understand the problem well.
It works with soft link and hard link both.

Related

How do you turn off syntax highlighting for new vim windows without a filename or file type?

After I installed the 'artesanal' theme for vim and turned syntax highlighting on, every vim window has syntax highlighting including brand new empty windows [No Name], without a name or file type. I'm wondering if any of you know how to keep syntax highlighting on for every file with an extension but have it disabled for any file without a name or file extension.
This should not happen. I don't know artesanal (and "theme" is an undefined term inside Vim; it has colorschemes, filetype plugins, and syntax scripts; I hope it's not a full Vim "distribution" like spf-13 and Janus, which lure you with a quick install and out-of-the-box settings, but you pay the price with increased complexity (you need to understand both Vim's runtime loading scheme and the arbitrary conventions of the distribution) and inflexibility (the distribution may make some things easier, but other things very difficult)).
It looks like a syntax is active even for plain files. Usually, the syntax is determined by the filetype, so check :verbose setlocal filetype? first. If this returns a value, you need to look into the detection of :help filetypes.
If this is empty, it could also be that something sets 'syntax' directly. You can check in the same way: :verbose setlocal syntax?.
Now, if that also is empty, and :syntax list doesn't show something, the highlighting could also come from :match or :call matchadd() commands; :call clearmatches() would remove this then. (And you still would need to find the source that defines those matches.)
You can check to see if a filetype has been set
if &filetype != ""
syntax enable
endif

vim, switching between files rapidly using vanilla Vim (no plugins)

I understand that limiting myself to vanilla Vim (not using plugins) limits the power of the editor, but as I switch between different machines frequently, it is often too much trouble to move my environment around everywhere. I want to just stay in vanilla Vim.
Something that holds me back is the ability to quickly switch between files.
I (believe at least) have a good understanding of buffers, windows, tabs, as well as netrw (Vex, Ex, etc).
But in an editor such as Sublime Text, I can just type ctrl-p and instantly I am at the file.
I know that I can drop down to the shell, but I wonder if there are any other "hidden" secrets to rapidly switching between files in Vim based off more than just the filename.
The closest equivalent ("closest", not "exact") to ST2's Ctrl+P is a plugin called, get ready… CtrlP. There are other similar plugins like Command-T or FuzzyFinder.
I use CtrlP and I love it but I wholeheartedly support your decision to go "plugin-free". It's not the easiest way to go but it will pay off in the long run.
Opening files
The most basic way to open a file is :e /path/to/filename. Thankfully, you get tab-completion and wildcards: the classic * and a special one, **, which stands for "any subdirectory".
Combining all of that, you can do:
:e **/*foo<Tab>
to choose from all the files containing foo in their name under the working directory or:
:e **/*foo/*bar<Tab>
to choose from all the files containing bar in their name under any subdirectory containing foo in its name, anywhere under the working directory.
Of course, that works for :tabe[dit], :sp[lit] and :vs[plit], too.
Those commands are limited to one file, though. Use :next to open multiple files:
:next **/*.js
and take a look at :help arglist.
Jumping between buffers
:b[uffer] is the basic buffer-switching command:
:b4 " switch to buffer number 4
:bn " switch to next buffer in the buffer list
:bp " switch to previous buffer in the buffer list
:bf " switch to first buffer in the buffer list
:bl " switch to last buffer in the buffer list
:b foo<Tab> " switch by buffer name with tab-completion
:b# " switch to the alternate file
Note that many of these commands and their relatives accept a count.
The :ls command shows you a list of loaded buffers. It is a bit "special", though: buffers are assigned a number when they are created so you can have a list that looks like 1 2 5 if you delete buffers. This is a bit awkward, yes, and that makes switching to a buffer by its number a bit too troublesome. Prefer switching by partial name, :b foo<Tab> or cycling, :bn :bp.
Anyway, here is a cool mapping that lists all loaded buffers and populates the prompt for you, waiting for you to type the number of a buffer and press <enter>:
nnoremap gb :ls<CR>:b<Space>
With this mapping, switching to another buffer is as simple as:
gb
(quickly scanning the list)
3<CR>
or:
gb
(quickly scanning the list)
foo<tab><CR>
The idea comes from this image taken from Bairui's collection of Vim infographics:
Vim also has <C-^> (or <C-6> on some keyboards)—the normal mode equivalent of :b#—to jump between the current buffer and the previous one. Use it if you often alternate between two buffers.
Read all about buffers in :help buffers.
Go to declaration
Within a file, you can use gd or gD.
Within a project, Vim's "tags" feature is your friend but you'll need an external code indexer like ctags or cscope. The most basic commands are :tag foo and <C-]> with the cursor on a method name. Both tools are well integrated into Vim: see :help tags, :help ctags and :help cscope.
For what it's worth, I use tag navigation extensively to move within a project (using CtrlP's :CtrlPTag and :CtrlPBufTag commands, mostly, but the buit-in ones too) and my favorite "generic" buffer switching method is by name.
Deploying your config
A lot of Vim users put their config under version control which makes it very quick and easy to install your own config on a new machine. Think about it.
EDIT
A few months ago, I had to work on a remote machine with an outdated Vim. I could have installed a proper Vim and cloned my own beloved config but I decided to travel light, this time, in order to "sharpen the saw". I quickly built a minimalist .vimrc and revisited a couple of half forgotten native features. After that gig, I decided CtrlP wasn't that necessary and got rid of it: native features and custom mappings are not as sexy but they get the job done without much dependencies.
Juggling with files
set path=.,**
nnoremap <leader>f :find *
nnoremap <leader>s :sfind *
nnoremap <leader>v :vert sfind *
nnoremap <leader>t :tabfind *
:find is a truly great command as soon as you set path correctly. With my settings, ,ffoo<Tab> will find all the files containing foo under the current directory, recursively. It's quick, intuitive and lightweight. Of course, I benefit from the same completion and wildcards as with :edit and friends.
To make the process even quicker, the following mappings allow me to skip entire parts of the project and find files recursively under the directory of the current file:
nnoremap <leader>F :find <C-R>=expand('%:h').'/*'<CR>
nnoremap <leader>S :sfind <C-R>=expand('%:h').'/*'<CR>
nnoremap <leader>V :vert sfind <C-R>=expand('%:h').'/*'<CR>
nnoremap <leader>T :tabfind <C-R>=expand('%:h').'/*'<CR>
WARNING! The path option is extremely powerful. The value above—.,**—works for me, as a default fallback value. In the real world, the exact value of the option will differ from project/language/framework/workflow to project/language/framework/workflow, so the proper value depends entirely on your needs. Don't blindly copy that line and expect it to solve all your problems.
Juggling with buffers
set wildcharm=<C-z>
nnoremap <leader>b :buffer <C-z><S-Tab>
nnoremap <leader>B :sbuffer <C-z><S-Tab>
The mappings above list the available buffers in the "wildmenu" with an empty prompt, allowing me to either navigate the menu with <Tab> or type a few letters and <Tab> again to narrow down the list. Like with the file mappings above, the process is quick and almost friction-less.
nnoremap <PageUp> :bprevious<CR>
nnoremap <PageDown> :bnext<CR>
Those mappings speak for themselves.
Juggling with tags
nnoremap <leader>j :tjump /
This mapping uses regex search instead of whole word search so I can do ,jba<Tab> to find tag foobarbaz().
Yes, fuzzy matching is addictive but you can be just as productive without it. And for a fraction of the cost.
MORE EDIT
A couple of additional tips/tricks…
Wildmenu options
The "wildmenu", enabled with set wildmenu, makes file/buffer navigation easier. Its behavior is governed by a bunch of options that are worth investigating:
wildmode tells Vim how you want the "wildmenu" to behave:
set wildmode=list:full
wildignore filters out all the cruft:
set wildignore=*.swp,*.bak
set wildignore+=*.pyc,*.class,*.sln,*.Master,*.csproj,*.csproj.user,*.cache,*.dll,*.pdb,*.min.*
set wildignore+=*/.git/**/*,*/.hg/**/*,*/.svn/**/*
set wildignore+=tags
set wildignore+=*.tar.*
wildignorecase allows you to search for foo and find Foo:
set wildignorecase
File marks
augroup VIMRC
autocmd!
autocmd BufLeave *.css normal! mC
autocmd BufLeave *.html normal! mH
autocmd BufLeave *.js normal! mJ
autocmd BufLeave *.php normal! mP
augroup END
I recently found this gem in someone else's ~/.vimrc. It creates a file mark at the exact position of the cursor whenever you leave a buffer so that, wherever you are, 'J jumps to the latest JavaScript buffer you edited. Awesome.
The answer depends a lot on your preferences and circumstances. Some examples:
If it's mostly two files (e.g. a C header and implementation file), <C-^> is very handy. In general, the alternate file is an important concept.
If you use a large editor window, window :splits turn the problem of locating a buffer from locating the window (once you've got all buffers opened). You can use [N]<C-w><C-w> to quickly switch to it.
If you can memorize (a few) buffer numbers, the :[N]b[uffer] and :[N]sb[uffer] commands are quite handy; :ls tells you the numbers.
Plugins (or at least custom mappings) can improve things a lot, and there's a whole variety on this topic on vim.org. There are various mechanisms to distribute your config (Pathogen + GitHub, Dropbox, ...), or you could remotely edit server files through the netrw plugin that ships with Vim.
Sometimes it is also handy to go sequentially through a list of files (e.g., if you did something like vim *.php to open several files at once). Then you can use :n[ext] (as well as :prev[ious], :fir[st], and :la[st]) for navigation (in addition to what was suggested in the other answers).
You can do wildcard tab completion on the command line without any plugins. e.g.
:e src/**/foo*<tab>
will let you cycle through all the files starting with 'foo' in the directory tree under ./src and select the one you want to edit.
If you have already edited the file and it is still in a buffer then you can switch to it with:
:b foo<tab>
which will cycle through all the buffers with 'foo' in the path.
You may need to set the wildmode and wildmenu options to get the behaviour you want. I have
wildmode=longest:full
wildmenu
in my .vimrc.
If you are on a filename and want to jump to that file, gf will do it for you. I also like using ctags, which isn't a plugin; you just build the tags and can easily jump around your codebase.
If you want switch between files in vim editor, please see below answer
First press Esc key to exit from edit mode.
Then type :e to check current file path.
if you want to go another file then type :e /path-of-file.txt/ using this you are able to switch.
If you want to go previous file simply type :e# which switch to previous file path.
I had the same issue with Vim.
The last thing I want is to depend on plugins for a task as mundane as file switching.
I added the following lines to .vimrc
set path+=**
set wildmenu
And BAM! I can now :find any/filename/in/any/folder/ as long as vim is in the root directory of the project. Tab completion works. Wildcards work!
Once files are opened already, and there are a ton of buffers in the background (you could use :ls to see all buffers), running :b any/file <TAB> will fuzzy search for all buffers and jumps to the required file. In case it is not unique there will be a wildmenu of tabs (hence the 2nd line in .vimrc) which can be selected using tab.
My answer is coming from this awesome video
https://www.youtube.com/watch?v=XA2WjJbmmoM&feature=youtu.be&t=489
There are more tricks in and I recommend watching it.

Can you have different localleaders for different Vim plugins?

I started using a plugin that conflicts with my existing maps, but instead of remapping all of it's maps, I just want to add a prefix. I thought I'd be able to do this with LocalLeader.
Vimdoc says:
<LocalLeader> is just like <Leader>, except that it uses
"maplocalleader" instead of "mapleader". <LocalLeader> is to be used
for mappings which are local to a buffer.
It seems that the only way to set localleader is to set a global variable (the docs don't mention this, but b:maplocalleader didn't work):
let maplocalleader = '\\'
And I don't see how I'd cleanly unset that variable (an autocmd that clears it after plugins are setup!?)
Is there a way to do this? Or is LocalLeader only to give one global prefix and one filetype-specific prefix?
Your last hunch is correct. If the plugin uses <Leader> (and it should unless it's a filetype plugin), there's no use in messing with maplocalleader.
Remapping is canonically done via <Plug> mappings, which the plugin hopefully offers. Some plugins do define a lot of similar mappings, some of those define a g:pluginname_mappingprefix (or so) variable to save you from having to remap all mappings individually. If your plugin doesn't, maybe write a suggestion / patch to the plugin author.
While #IngoKarkat solution is a prefered one, there is a hack which lets you do what you want: the SourcePre event:
autocmd SourcePre * :let maplocalleader='\\'
autocmd SourcePre plugin-name.vim :let maplocalleader='_'
. This works for <Leader> as well. There are lots of cases when this won’t work though. You can as well use SourceCmd for this job, using something like
function s:Source(newmll)
try
let oldmll=g:maplocalleader
let g:maplocalleader=a:newmll
source <amatch>
finally
let g:maplocalleader=oldmll
endtry
endfunction
let maplocalleader='\\'
autocmd SourceCmd plugin-name.vim :call s:Source('_')
in SourceCmd is the only way I see to restore maplocalleader after plugin was sourced, but SourceCmd event here won’t be launched for any file sourced inside plugin-name.vim. For some poorly written plugins (I mean, those that emit errors while sources) putting :source inside a :try block will break execution at the point where error occurs. Should not happen most of time though. You may also want to use */ftplugin/plugin-name.vim as a pattern instead of plugin-name.vim.

How can I disable code folding in vim with vim-latex?

I have tried the usual approaches, and have read :help tex.vim
(see : http://vimdoc.sourceforge.net/htmldoc/syntax.html )
I've taken a brief look at syntax/tex.vim, but can't see how to disable it without rebuilding vim without folding. I'm sick of hitting 'zE'.
Lines I've tried in my .vimrc:
set foldlevel=manual
set foldlevelstart=99
let g:tex_fold_enabled=0
Just noticed that there are variables to control folding in vim-latex-suite, at least as of v1.6 of the plugin. The functionality is documented here:
http://vim-latex.sourceforge.net/documentation/latex-suite.html#latex-folding
In short you should be able to change three global variables to get rid of all folding:
:let Tex_FoldedSections=""
:let Tex_FoldedEnvironments=""
:let Tex_FoldedMisc=""
That should get rid of all folding. If you want to disable some folding but not all then you can control things by setting the appropriate values for each variable, as described in the documentation link above. Hope that helps.
What about
autocmd Filetype tex setlocal nofoldenable
The folding functionality all seems to located in folding.vim file of latex-suite distribution. This file is referenced in line 825 of my main.vim file in the latex-suite folder of the ftplugin folder. That line reads:
exe 'source '.fnameescape(s:path.'/folding.vim')
Comment out that line and, as far as I can tell, it strips out all the folding in latex-suite plugin. I don't think it affects anything else, but I haven't checked.

Vim file navigation

I'm trying really hard to learn vim after using TextMate for the last few years.
I've started to commit some of the in-file navigation to memory but I'm struggling with navigating between multiple files.
In my workflow it is pretty common that I'm flipping between a handful of files pretty regularly (enough files such that split-pane windows become too small).
I'm currently using NERDTree but find drilling down into directories cumbersome as well as constantly using CTRL+W h/CTRL+W l to hop back and forth.
I think I would do better with tabs I can easily toggle between but maybe I need to use a different workflow.
I'd also like a "Go to File..." shortcut like CMD+T in TextMate. I've found fuzzy_file_finder but it requires vim to be built with Ruby bindings which isn't the case the native installs I've worked on.
While I could rebuild the main reason I want to switch to vim is so I can have one editor environment that I know will easily work across any platform.
An easy way to browse the file system is the command:
:Sex
I'm not making this up :)
I don't find drilling down into subdirectories via plain old :e to be that cumbersome given a decent configuration for tab-completion.
Look into the 'wildmenu' option to have Vim show a list of completions (filenames) in the modeline above the commandline. You can change the 'wildmode' option to further configure the kind of tab-completion Vim will do.
Personally I use :set wildmode=full.
My workflow is like this:
:cd into the toplevel directory of my project.
To open file foo/bar/baz:
Simplest scenario: type :e f<tab>b<tab>b<tab><enter>.
If there are more than one file starting with b in one of those directories you might have to do a <left> or <right> or another <tab> on the keyboard to jump between them (or type a few more letters to disambiguate).
Worst-case scenario there are files and directories that share a name and you need to drill down into the directory. In this case tab-complete the directory name and then type *<tab> to drill down.
Open 2 or 3 windows and open files in all of them as needed.
Once a file is open in a buffer, don't kill the buffer. Leave it open in the background when you open new files. Just :e a new file in the same window.
Then, use :b <tab> to cycle through buffers that are already open in the background. If you type :b foo<tab> it will match only against currently-open files that match foo.
I also use these mappings to make it easier to open new windows and to jump between them because it's something I do so often.
" Window movements; I do this often enough to warrant using up M-arrows on this"
nnoremap <M-Right> <C-W><Right>
nnoremap <M-Left> <C-W><Left>
nnoremap <M-Up> <C-W><Up>
nnoremap <M-Down> <C-W><Down>
" Open window below instead of above"
nnoremap <C-W>N :let sb=&sb<BAR>set sb<BAR>new<BAR>let &sb=sb<CR>
" Vertical equivalent of C-w-n and C-w-N"
nnoremap <C-w>v :vnew<CR>
nnoremap <C-w>V :let spr=&spr<BAR>set nospr<BAR>vnew<BAR>let &spr=spr<CR>
" I open new windows to warrant using up C-M-arrows on this"
nmap <C-M-Up> <C-w>n
nmap <C-M-Down> <C-w>N
nmap <C-M-Right> <C-w>v
nmap <C-M-Left> <C-w>V
It takes me a matter of seconds to open Vim, set up some windows and open a few files in them. Personally I have never found any of the third-party file-browsing scripts to be very useful.
:Sex,:Vex, :Tex and :Ex are all useful commands for ex(ploring) the files on your system if you want to use something different from :e
(where S/V/T are short for Split/Vertical/Tab)
By far the best and fastest plugin I found for file navigation is fzf.vim. You can very quickly fuzzy search all your files, the open buffers and even the files contents.
Since we have a very large codebase at work I specified a couple of directories I use most as the directories fzf searches. Fzf even has a rich git integration. So you can search only tracked files.
This is how the file search looks:
and his is how the content search looks:
If you haven't found them already, you might want to check out:
the original fuzzy finder plugin -- which IIRC doesn't have the Ruby binding issues
the Project plugin -- similar to NERDTree
the buffer explorer plugin -- shows a list of open buffers
Also bear in mind that you can remap key shortcuts in your .vimrc to make them less cumbersome. I do use split windows a lot; I've found the following make dealing with them much easier:
" set your own personal modifier key to something handy
let mapleader = ","
" use ,v to make a new vertical split, ,s for horiz, ,x to close a split
noremap <leader>v <c-w>v<c-w>l
noremap <leader>s <c-w>s<c-w>j
noremap <leader>x <c-w>c
" use ctrl-h/j/k/l to switch between splits
map <c-j> <c-w>j
map <c-k> <c-w>k
map <c-l> <c-w>l
map <c-h> <c-w>h
It is surprising to see that :find command has not been mentioned in any of the answers. I usually work with rails projects where I need to drill down deep into the directory hierarchy. So for a file application.css, located in app/assets/stylesheets/ all I have to type is :find applica and then press tab to autocomplete and enter to open.
I think I would do better with tabs I
can easily toggle between, but maybe I
need to use a different workflow.
Are you aware of Vim's tabs? Not sure if you were referring to Vim's own tabs there, or pining for TextMate's. It's unclear what Vim version you're using but it's had tabs since at least 7.0 (which seems to be installed everywhere I look lately), and they're awesome.
:tabe opens a new one (short for "tab edit", so e.g. :tabe for empty or :tabe path/to/file for opening a file in a new tab,) you can move between adjacent tabs with gt/gT, and there's a bunch of other tab-related nav commands too, as you might expect from Vim (:help tabs)
My workflow for large projects tends to involve a bunch of tabs, each with between 1 and 3 windows depending on what sort of context I need. This works even better if I have a doublewide terminal window since I can vertically split with :vs, so then a single tab can easily show me 4 files at once with plenty of room for each.
Final tip: I sometimes use the "switch buffer" command (:sb <partial_buf_name>) which is sorta-kinda like TM's fuzzy finding, in that it works pretty well but only for already-open buffers. Still great for when I have a dozen or more files open at once. (N.B. I needed to :set switchbuf=usetab in my vimrc for this to work across tabs, but once that's set it's great.)
I find LustyExplorer the best so far.
You can open a file by typing only a part of its name.
You can open a file from the directory of the current file, or the current pwd, or from the buffer list.
If you open a file from the buffer list, you don't have to navigate through the directory tree, though unfortunately for the other two modes, you still have to navigate through the tree. The good thing is you don't have to start from the first character.
If you have:
dir_a/
...
dir_b/
file.js
file.html
and you want to open dir_b/file.html you can generally find it by typing b<Tab>h
Check out the plugin ctrlp as an alternative to the plugin Command-T. It's better than this in that it is a 'native' Vim script and doesnt need Vim to be compiled with ruby (or other support) while it runs very fast and unobtrusive.
If the file that you want is already loaded into a buffer (which is likely if you are flipping between a handful of files regularly) then you can quickly switch to the buffer with the :b[uffer] command.
:b can either take a buffer number as a parameter, or (more usefully) a string that matches part of the path/filename. If there are multiple matches you can use tab to cycle through them.
If you want to split the window and open the buffer in the new window then use :sb name
If you want to open the buffer in a new tab then use :tab b name
You can also use the :ls command to see a list of currently loaded buffers.
I use this function for finding files in subdirectories, I didn't write it though. It's from the vim wiki:
function! Find(name)
let l:list=system("find . -name '".a:name."' | grep -v \".svn/\" | perl -ne 'print \"$.\\t$_\"'")
let l:num=strlen(substitute(l:list, "[^\n]", "", "g"))
if l:num 0
echo "Not a number"
return
endif
if l:inputl:num
echo "Out of range"
return
endif
let l:line=matchstr("\n".l:list, "\n".l:input."\t[^\n]*")
else
let l:line=l:list
endif
let l:line=substitute(l:line, "^[^\t]*\t./", "", "")
execute ":e ".l:line
endfunction
command! -nargs=1 Find :call Find("")
One thing I like is it has support for wildcards (*). It's also well behaved for multiple matches.
In addition to the wildmenu answers, I use BufExplorer plugin, and the following mappings to quickly jump buffers:
nmap <A-1> :b 1<CR>
nmap <A-2> :b 2<CR>
nmap <A-3> :b 3<CR>
...
nmap <A-0> :b 10<CR>
nmap <A-=> \bs
Or you can just replace the buffer explorer with the :ls command, which basically display the same thing.
The last one maps the Alt-= to show the buffer explorer.
I don't use Windows that much, as I generally have one or two windows only.
I also modified the statusline to include the buffer number %2n, so that the I always know which buffer is being edited. See :h statusline
do you know MacVim application? It is VIM polished for OS X, and one of its feature is support for tabs (CMD+T to open new tab), it have drawer, etc... (read this: http://zerokspot.com/weblog/2008/08/03/macvim-as-textmate-replacement/)
Try it!
Regards
Michal
NerdTree is best for file navigation in vim.
The link below is a good introduction to nerdtree.
http://www.catonmat.net/blog/vim-plugins-nerdtree-vim/
I often do:
:e ./
in NORMAL MODE to see the content of the current folder and then navigate to any other folder or file.
Here's something fairly trivial that I stick in my .vimrc. It will load a file from any subdirectory.
" load from an unspecified subdirectory
function! LoadFromSubdirectory(filespec)
let filename = glob("`find . -name '" . a:filespec . "' -print`")
exe ':e '.filename
endfunction
:command -nargs=1 Er :call LoadFromSubdirectory(<f-args>)
So I can just say:
:Er Main.java
and it will search subdirectories, find Main.java and load it. It's not well behaved for multiple files of the same name, but it's quick and simple.
The other tool I use is VTreeExplorer, which provides a tree-based window of files/directories, and doesn't require any special build options from VIM (i.e. it's a vanilla plugin).
My workflow for finding files is the wildmenu, autocwd and :e.
Relevant parts in the .vimrc:
set wildmenu
set wildignore=*.dll,*.o,*.pyc,*.bak,*.exe,*.jpg,*.jpeg,*.png,*.gif
set wildmode=list:full
set autochdir
let g:netrw_list_hide='^\.,.\(pyc\|pyo\|o\)$'
And how to use it:
:e myf^D
That gives all the files in the current directory that start with myf. You can also <Tab> through them. Use return to open :)
^D will always give you all the matches. Because autocmd always goes to the folder of the current file it's easy to work with. If you are in foo/bar.py and you want to go to foo/baz.py you only do :e baz.py and you're there. That also works with buffers (:b foo^D lists all buffers starting with foo)

Resources