123

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.

Palec
  • 10,298
  • 7
  • 52
  • 116
Techmaddy
  • 4,406
  • 5
  • 26
  • 32
  • 1
    Note that the suggested commands have different effect `:tabe` will open the file in a new tab, `:new` will horizontally split the window, and `:edit` will close the current file before opening the new one. – Skippy le Grand Gourou Oct 22 '13 at 15:18

19 Answers19

142

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.

Eliran Malka
  • 14,498
  • 5
  • 72
  • 96
dkretz
  • 36,502
  • 13
  • 76
  • 133
  • 23
    You don't need the space, saving an additional 25% typing. (`:e.`). – Konrad Rudolph Feb 22 '09 at 10:29
  • 68
    You don't need the dot if you use a capital e, e.g. :E – Andriy Drozdyuk Jul 19 '10 at 18:50
  • 2
    @drozzy This happens with vim :-) – DerMike Jul 04 '12 at 09:21
  • 7
    There is a difference in the behaviour between `:e.` and `:E` though : if the current file has been edited and not saved yet, the former will complain (like `:edit`) because of "no write since last change", while the latter will horizontally split the window (like `:new`) to allow the opening of a new file (while both will replace the current file with the new file if it hasn't been modified (like `:edit`)). – Skippy le Grand Gourou Oct 22 '13 at 15:14
  • How does one exit from this view if they want to continue editing the file they are already editing without closing vim entirely? –  May 03 '15 at 17:22
  • 1
    If you open a file, then replace it with a second file, you can get back to the first using "e #" – dkretz May 04 '15 at 22:58
61

I recently fell in love with fuzzyfinder.vim ... :-)

:FuzzyFinderFile will let you open files by typing partial names or patterns.

dkretz
  • 36,502
  • 13
  • 76
  • 133
f3lix
  • 27,786
  • 10
  • 63
  • 82
34

: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).

UncleZeiv
  • 17,080
  • 6
  • 46
  • 74
mattwarren
  • 691
  • 7
  • 8
  • I don't find useful `:find` since to get the path relative to the working directory. See the question and @romainl answer here: http://stackoverflow.com/questions/28224168/vim-using-find-how-to-get-the-path-relative-to-the-working-directory/28225117#28225117 . I find more interesting @strager answer to get the path relative to the working directory easier. – ziiweb Jan 29 '15 at 21:42
26

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.

strager
  • 84,025
  • 24
  • 129
  • 172
  • I tried using this but this is like so slow. I mean my application is big and the editor stopped working for sometime. Do you know any setting to increase the editor performance and speed. – Techmaddy Feb 23 '09 at 11:20
  • 1
    Nice answer. Use something like `:tabe **/*der.h` if you don't remember the full name of the file. – ziiweb Jan 29 '15 at 21:46
16

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.

Alexey
  • 8,424
  • 4
  • 55
  • 75
12

unless I'm missing something, :e filename is the fastest way I've found.

You can use tab to autocomplete the filename as well.

Sagar Jain
  • 6,261
  • 10
  • 40
  • 73
Ian P
  • 12,298
  • 6
  • 42
  • 68
  • @dirkgently, It is. It's still a shortcut, as the title of this question suggests, but I don't think it's what the questioner is looking for. – strager Feb 21 '09 at 13:59
  • For me tab autocomplete does not work for :e. Besides you don't always want to search for filename that starts with something, but rather for a middle word in a filename. – Andriy Drozdyuk Jul 19 '10 at 18:51
10

I like the :FuzzyFinderTextMate (or Ctrl + F) on my setup. See http://weblog.jamisbuck.org/2008/10/10/coming-home-to-vim

Jasper
  • 2,116
  • 3
  • 30
  • 48
alex2k8
  • 39,732
  • 56
  • 158
  • 214
9

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:


  1. You are editing a file called test.java in "/home/prog"
  2. ,cd -> Current working directory now becomes "/home/prog"
  3. ,e -> Expands to ":e /home/prog" so that you can just fill in the file name, say test.h.

  1. ,e -> Expands to ":e /home"
  2. tab -> Cycle through subdirectories of /home
  3. enter -> cd to the directory you want say /home/prog
  4. ,e -> Expands to ":e /home/prog"
Jasper
  • 2,116
  • 3
  • 30
  • 48
Himadri Choudhury
  • 9,389
  • 6
  • 36
  • 43
8

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/

bronson
  • 81
  • 1
  • 1
  • 1
    You might want to try ctrlp.vim (https://github.com/kien/ctrlp.vim) as well. It's basically the same as Command-T but written in pure Vimscript. (IMHO it is simpler to use as well) – datentyp Jan 11 '12 at 15:39
6

Use tabs, they work when inputting file paths in vim escape mode!

dirkgently
  • 101,474
  • 16
  • 123
  • 183
4

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

Nathan Fellman
  • 108,984
  • 95
  • 246
  • 308
4

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".

Paul Tomblin
  • 167,274
  • 56
  • 305
  • 392
4

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).

rampion
  • 82,104
  • 41
  • 185
  • 301
3

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):

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.

Luc Hermitte
  • 29,719
  • 7
  • 60
  • 74
1

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.

Wonil
  • 5,616
  • 2
  • 32
  • 52
1

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

Samantha
  • 571
  • 5
  • 11
1
  • 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.

Rook
  • 54,867
  • 44
  • 156
  • 233
1

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.

Naseer
  • 1,705
  • 2
  • 21
  • 22
0

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
Ethan
  • 52,651
  • 60
  • 180
  • 231
  • You sure? I have some config in my .vimrc, look for "g:fuf_abbrevMap". I must have found an example in the documentation. When I type the shortcut, I then type an abbrev like "web:" and it will lookup all files within a folder of my choice. My problem actually is that for some reason it doesn't open files anymore, I see [NEW DIRECTORY] in the status bar instead. But I have the same problem with :o, seems unrelated to fuf. –  Mar 11 '10 at 20:33