How do I complete file paths in emacs?

13,829

Solution 1

Try Hippie Expand, which as one of it's possibilities has 'try-complete-file-name. You can change the order and list of expansion functions hippie expand will use to favor expanding the file name.

Or, you could even write a custom wrapper that would only do the file name expansion. Something like:

(global-set-key (kbd "C-M-/") 'my-expand-file-name-at-point)
(defun my-expand-file-name-at-point ()
  "Use hippie-expand to expand the filename"
  (interactive)
  (let ((hippie-expand-try-functions-list '(try-complete-file-name-partially try-complete-file-name)))
    (call-interactively 'hippie-expand)))

Solution 2

I usually type Ctrl-X Ctrl-F like I would open a file, but instead of pressing RET I press Ctrl-A Ctrl-K Ctrl-G to copy the path and then paste it into the buffer I was editing with Ctrl-Y.

I don't need this often enough, but if I really wanted a better solution, I would definitely use Trey Jackson's solution using hippie-expand. I thought about how hippie-expand might be a better way to do this when first answering, but I didn't know and was too lazy to look it up, so I just wrote what I do.

Solution 3

Up to version 25, the vanilla, out-of-the-box

M-x comint-dynamic-complete-filename

and

M-x comint-replace-by-expanded-filename

both worked outside of comint mode. Of course, you can use the minibuffer's dynamic expansion to use less keystrokes to get to them (e.g., M-x comint-dynamic-complete-filename or M-x comint-replace-by-expanded-filename). Or, if you will be doing this frequently, you can bind them to key sequences of your choice using global-set-key .

For version 26+, it seems to me that you need to make sure that comint mode is explicitly loaded --- adding a line

(require 'comint)

in your .emacs file should make this work again.

Caveat: Not all applicable comint functions can be used this way. comint-dynamic-list-filename-completions doesn't seem to work outside of its native mode.

Solution 4

It is also possible to do this using the company autocompletion if the company-files are in the list of company-backends (default in my installation) or explicitly by calling company-files.

Solution 5

I recently discovered the works of Tomohiro Matsuyama (@m2ym) and I have been very impressed by the quality of its emacs packages.

I have struggled with most auto-completion extensions and found out that they all have major shortcomings.

Check out Tomo's auto-complete (http://auto-complete.org/doc/manual.html) it has been a breeze to install (I'm on Linux) and covers many auto-completion needs in a modern fashion.

Of course, your request for in-buffer file names completion is perfectly covered.

Finally, if you happen to do Ruby too, make sure not to miss his RSense extension; again, the best of its kind.

Share:
13,829

Related videos on Youtube

Jason Baker
Author by

Jason Baker

I'm a developer on Google's Cloud Console.

Updated on September 17, 2022

Comments

  • Jason Baker
    Jason Baker over 1 year

    Say for instance I'm editing a config file and I want to type in a path. Is there a plugin for emacs that lets you complete a file path in that buffer? I've searched for this and there's like a bazillion completion plugins out there.

    • dmckee --- ex-moderator kitten
      dmckee --- ex-moderator kitten over 14 years
      I'm not clear on what you area asking here. Modern emacen have path completion in the mini-buffer (when using find-file for instance). Do you want a similar behavior in a content buffer?
    • Jason Baker
      Jason Baker over 14 years
      @dmckee - Yes, that's actually exactly what I'm looking for.
    • Zeynel
      Zeynel over 10 years
      @dmckee Is this OS specific? I had file name completion in Find File in ubuntu but there is no completion in OSX. For instance I have a file called "reference" in root and I am in mini buffer: "Find file: ~/" and I enter "ref" and I expect completion but nothing happens. Do I need to install something?
    • dmckee --- ex-moderator kitten
      dmckee --- ex-moderator kitten over 10 years
      @Zeynel Works fine for me in Mac OS, too. Either with the Apple installed version or the homebrew version. Also worked with the fink version on my older machine.
    • Zeynel
      Zeynel over 10 years
      @dmckee Strange. Maybe I ask a new question. Thanks.
  • dmckee --- ex-moderator kitten
    dmckee --- ex-moderator kitten over 14 years
    Clunky and inelegant, but working. I have to admit to doing this from time to time myself. I'm still interested in a neater solution, though...
  • Jason Baker
    Jason Baker over 14 years
    Indeed. It doesn't really seem like a lot less work than typing it out manually though.
  • Teddy
    Teddy over 14 years
    @Jason Baker: It depends on whether you know the exact file name or if you need to explore a bit for it.
  • xuhdev
    xuhdev about 9 years
    The link is broken
  • Krab
    Krab over 5 years
  • Yu Shen
    Yu Shen almost 4 years
    With Doom emacs, C-x C-f after typing the partial path, it works the charm!