In Vim, how to choose the nth suggested file while doing :edit? - vim

This is a total newbie vim question. Apologies for the basic-ness involved.
I need to open a lot of files. The :edit <file-name> command seems to open a file and I also see a filename auto-complete feature, which searches for all similar file names in the path. But I don't know how to choose one of those suggestions quickly without writing the whole file name.

set wildmenu
enables the "wildmenu" where you navigate with <Tab> and <S-Tab>, enter a subdirectory with <Down> and select a file with <CR>.
See :help 'wildmenu' and :help 'wildmode'.
Note that :edit can only open one file. If you want to open multiple files at once, use :args models/*.php.
Another, more familiar, way to open files is to use the built-in netrw:
:Explore
See :help netrw for more info.

Sounds like you might enjoy the conveniences of the rather smashing CTRL-P plugin. https://github.com/kien/ctrlp.vim
It will allow you to type any substring of the filename to narrow the available files down. You can mark multiple files for opening.

Related

Explore filesystem/directories in 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.

How do you have vim find or search for a file in the path when opening a split?

In vim, I can find a file and open it in a new tab using :tabfind, and I can open a file I know the location of in a new split using :split or :vsplit. However, I would like vim to find files for me in my path when running the :split or :vsplit commands. For example, say in my current directory, I have these files (all of which are in my path):
./ReallyLongFileName1.txt
./fooDir/ReallyLongFileName2.txt
./barDir/bazDir/ReallyLongFileName3.txt
I would like to type the following command in vim:
:vsplit R<Tab>
And have vim search in my path for all files that match that expression, and fill the rest of the line with
:vsplit ReallyLongFileName
This works perfectly with :find or :tabfind, but it doesn't work when splitting. Is there a command that does this that I just don't know about, or is there some way to add this functionality to the :split and :vsplit commands?
As romainl said, :sf is the command that performs this action. There is no vertical split find, although you can do the same thing with the slightly longer command :vert sf.
Typing :split | :find<space>R (meaning "open a split then find..." seems to do the trick. Same thing with :vsplit.
Making it a custom function and calling it with a nnoremap would make it even better.
Edit:
I didn't know about :find and :tabfind so I looked them up in Vim's help and I found :sfind which does exactly one of the things you want. Unfortunately I don't see a :vfind or :vsfind.
Look it up: :help sfind.
what I do for vertical split find is: add an abbreviation in .vimrc.
" Abbreviation for vertical split find
cabbrev vsf vert sfind

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.

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

Shortcut to open file in Vim

I want to open a file in Vim like in Eclipse using Ctrl + Shift + R, or via the Ctrl + N option of autofill. Invoke a keyboard shortcut, type the file name/pattern, and choose from all the matching files names.
I know opening it normally like:
:tabe <filepath>
:new <filepath>
:edit <filepath>
The problem here is that I have to specify the whole file path in these cases.
What I normally do is e . (e-space-dot) which gives me a browsable current directory - then I can / - search for name fragments, just like finding a word in a text file. I find that generally good enough, simple and quick.
I recently fell in love with fuzzyfinder.vim
... :-)
:FuzzyFinderFile will let you open files by typing partial names or patterns.
:find is another option.
I open vim from the root of my project and have the path set to there.
Then, I can open files located anywhere in the tree using:
:find **/filena< tab >
Tab will autocomplete through various matches. (** tells it to search recursively through the path).
You can search for a file in the current path by using **:
:tabe **/header.h
Hit tab to see various completions if there is more than one match.
Consider using CtrlP plug-in.
It is included in Janus Distributive.
Allows you to find files in the current directory, open buffers or most recently used files using "fuzzy matching" or regular expression.
unless I'm missing something, :e filename is the fastest way I've found.
You can use tab to autocomplete the filename as well.
I like the :FuzzyFinderTextMate (or Ctrl + F) on my setup.
See http://weblog.jamisbuck.org/2008/10/10/coming-home-to-vim
I use a couple of shortcuts in my .vimrc file (exact syntax below).
They are based on the fact that in 90% of the cases, I want to open another file in the same directory as the file that I am currently editing, or in a directory that is very close in the hierarchy to that edited file.
Here's what the commands do do:
,cd : Change the current working directory to the directory that the current file you are editing is in.
,e : Opens a file with the current working directory already filled in so you have to specify only the filename.
Put these into your .vimrc:
map ,e :e <C-R>=expand("%:p:h") . "/" <CR>
map ,cd :cd %:p:h <CR>
Here's a sequence of events:
You are editing a file called test.java in "/home/prog"
,cd -> Current working directory now
becomes "/home/prog"
,e -> Expands to ":e /home/prog" so
that you can just fill in the file
name, say test.h.
,e -> Expands to ":e /home"
tab -> Cycle through subdirectories of /home
enter -> cd to the directory you
want say /home/prog
,e -> Expands to ":e /home/prog"
There's also command-t which I find to be the best of the bunch (and I've tried them all). It's a minor hassle to install it but, once it's installed, it's a dream to use.
https://wincent.com/products/command-t/
Use tabs, they work when inputting file paths in vim escape mode!
If you've got tags (and you should), you can open a file from the command line just by the name of the class or method or c function, with "vim -t DBPlaylist", and within vim with ":tag ShowList".
If you're editing files in a common directory, you can :cd to that directory, then use :e on just the filename.
For example, rather than:
:e /big/long/path/that/takes/a/while/to/type/or/tab/complete/thingy.rb
:sp /big/long/path/that/takes/a/while/to/type/or/tab/complete/other_thingy.c
:vs /big/long/path/that/takes/a/while/to/type/or/tab/complete/one_more_thingy.java
You can do:
:cd /big/long/path/that/takes/a/while/to/type/or/tab/complete/
:e thingy.rb
:sp other_thingy.c
:vs one_more_thingy.java
Or, if you already have a file in the desired directory open, you can use the % shorthand for the current filename, and trim it to the current directory with the :h modifier (:help :_%:) :
:e /big/long/path/that/takes/a/while/to/type/or/tab/complete/thingy.rb
:cd %:h
:sp other_thingy.c
:vs one_more_thingy.java
And, like others have said, you can tab-complete file names on the ex-line (see :help cmdline-completion for more).
This isn't exactly what you're looking for, but it's good in many cases (though not all).
If you VIM open and there's a name of a file in the buffer, you can put the cursor on that filename and type gf. This opens the file whose name is under the cursor in the same buffer. It's the same as
:e CTRL+r CTRL+w
I know three plugins that permit to open files, support auto-completion, and don't require to enter the full path name of the file(s) to open (as long as the files are under one of the directories from &path vim option):
searchInRuntime that I'm maintaining (the completion is not on :e/:find, but on split actions)
fuzzy finder as it has been already pointed out,
lookupfile.
Lately, I've seen another plugin with a similar feature, but I don't remember the name.
Soon, :find is likely support auto-completion -- patches on this topic are circulating on vim_dev mailing-list these days.
you can use (set wildmenu)
you can use tab to autocomplete filenames
you can also use matching, for example :e p*.dat or something like that (like in old' dos)
you could also :browse confirm e (for a graphical window)
but you should also probably specify what vim version you're using, and how that thing in emacs works. Maybe we could find you an exact vim alternative.
FuzzyFinder has been mentioned, however I love the textmate like behaviour of the FuzzyFinderTextmate plugin which extends the behaviour to include all subdirs.
Make sure you are using version 2.16 of fuzzyfinder.vim - The higher versions break the plugin.
With Exuberant ctags, you can create tag files with file information:
ctags --extra=+f -R *
Then, open file from VIM with
:tag filename
You can also use <tab> to autocomplete file name.
In GVIM, The file can be browsed using open / read / write dialog;
:browse {command}
{command} - open / read / write
open - Opens the file
read - Appends the file
write - SaveAs dialog
I installed FuzzyFinder. However, the limitation is that it only finds files in the current dir. One workaround to that is to add FuzzyFinderTextmate. However, based on the docs and commentary, that doesn't work reliably. You need the right version of FuzzyFinder and you need your copy of Vim to be compiled with Ruby support.
A different workaround I'm trying out now is to open all the files I'm likely to need at the beginning of the editing session. E.g., open all the files in key directories...
:args app/**
:args config/**
:args test/**
etc...
(This means I would have possibly scores of files open, however so far it still seems to work OK.)
After that, I can use FuzzyFinder in buffer mode and it will act somewhat like TextMate's command-o shortcut...
:FuzzyFinderBuffer

Resources