I have been a vim user for several years. Now I want to try Emacs for some reasons. I use the path auto-completition functionality(C-c, C-f) a lot in Vim. So I am wondering if there are any similar keystrokes in Emacs ? I have googled this question but only find this answer https://superuser.com/questions/67170/how-do-i-complete-file-paths-in-emacs .However, unlike vim which provides me a list candidate, Emacs completes the first candidate automatically. So, my question is How to configure HippieExpand to make it provide me a list of candidates instead of completing the first candidate automatically ?
-
4I suggest looking at ido – Bwmat Jun 29 '11 at 02:04
-
I concur with Bwmat. It may not provide the same exact functionality, but [ido](http://www.emacswiki.org/emacs/InteractivelyDoThings) is great. If you are using Emacs 22 or later, you already have it. In addition, other modes (e.g. org-mode), allow you to use ido for completion. – Ryan Kaskel Jun 29 '11 at 07:07
3 Answers
I'm not sure how to customize HippieExpand to do this, but you might want to take a look at the auto-complete package. It's fairly easy to set up and offers completion as a drop-down list. It's also very customizable so you can tweak it to behave exactly like you'd prefer. Hope you find it useful!
Edit: I just realized you were looking just for path completion not auto-completion in general. In that case auto-complete.el might be overkill. Still I do suggest that you take a look at it as it offers a lot of added utility. I second taking a look at ido for path-completion.

- 78
- 5
-
On a similar note, there's also http://www.emacswiki.org/emacs/CompletionUI (which I haven't used) – phils Jun 29 '11 at 05:07
-
@phils: I hadn't heard of this before, it looks like its still in early development. Always good to come across a new emacs package though! :) – bridgeburner Jun 29 '11 at 05:32
-
auto-complete will complete filenemes in contexts where you add [ac-source-filename](http://cx4a.org/software/auto-complete/manual.html#ac-source-filename) to its completion sources (ac-sources). So this is a valid answer, I think. – Francois G Jun 29 '11 at 11:22
-
@huitseeker: True, I was expressing the opinion that if all OP wanted was to complete filenames then auto-complete might be the proverbial sledgehammer for cracking the path completion nut :) – bridgeburner Jun 29 '11 at 18:33
Here's an implementation using hippie-expand, and utilising ido for the selection menu.
This gives us my-ido-hippie-expand
(which I'm binding to C-c e
) as the ido
equivalent of hippie-expand
, and also makes it easy to generate other targeted expansion utilities using particular expansion functions (typically some sub-set of hippie-expand-try-functions-list
) which facilitates a filename-only version.
(defun my-hippie-expand-completions (&optional hippie-expand-function)
"Return the full list of possible completions generated by `hippie-expand'.
The optional argument can be generated with `make-hippie-expand-function'."
(require 'cl)
(let ((this-command 'my-hippie-expand-completions)
(last-command last-command)
(buffer-modified (buffer-modified-p))
(hippie-expand-function (or hippie-expand-function 'hippie-expand)))
(flet ((ding)) ; avoid the (ding) when hippie-expand exhausts its options.
(while (progn
(funcall hippie-expand-function nil)
(setq last-command 'my-hippie-expand-completions)
(not (equal he-num -1)))))
;; Evaluating the completions modifies the buffer, however we will finish
;; up in the same state that we began.
(set-buffer-modified-p buffer-modified)
;; Provide the options in the order in which they are normally generated.
(delete he-search-string (reverse he-tried-table))))
(defmacro my-ido-hippie-expand-with (hippie-expand-function)
"Generate an interactively-callable function that offers ido-based completion
using the specified hippie-expand function."
`(call-interactively
(lambda (&optional selection)
(interactive
(let ((options (my-hippie-expand-completions ,hippie-expand-function)))
(if options
(list (ido-completing-read "Completions: " options)))))
(if selection
(he-substitute-string selection t)
(message "No expansion found")))))
(defun my-ido-hippie-expand ()
"Offer ido-based completion for the word at point."
(interactive)
(my-ido-hippie-expand-with 'hippie-expand))
(global-set-key (kbd "C-c e") 'my-ido-hippie-expand)
And the extension of this for just completing filenames:
(defun my-ido-hippie-expand-filename ()
"Offer ido-based completion for the filename at point."
(interactive)
(my-ido-hippie-expand-with
(make-hippie-expand-function '(try-complete-file-name))))
(global-set-key (kbd "C-c f") 'my-ido-hippie-expand-filename)

- 71,335
- 11
- 153
- 198
Notes of interest:
;; For the real hippie-expand enthusiast: A macro that makes it
;; possible to use many functions like hippie-expand, but with
;; different try-functions-lists.
;; Usage is for example:
;; (fset 'my-complete-file (make-hippie-expand-function
;; '(try-complete-file-name-partially
;; try-complete-file-name)))
;; (fset 'my-complete-line (make-hippie-expand-function
;; '(try-expand-line
;; try-expand-line-all-buffers)))
;;
;;;###autoload
(defmacro make-hippie-expand-function (try-list &optional verbose)
"Construct a function similar to `hippie-expand'.
Make it use the expansion functions in TRY-LIST. An optional second
argument VERBOSE non-nil makes the function verbose."
You could presumably utilise this to generate a list of possible filename completions.
edit: Actually, that doesn't get you any further than the other question. I'll keep it here, as it seems a slightly nicer approach.

- 71,335
- 11
- 153
- 198
-
hi, phils, could you be more specifically ? I am new to emacs so I don't know much about emcas. I have put the following code in my .emacs file but it seems that it didn't work. (fset 'my-complete-file (make-hippie-expand-function '(try-complete-file-name-partially try-complete-file-name))) (fset 'my-complete-line (make-hippie-expand-function '(try-expand-line try-expand-line-all-buffers))) (defmacro make-hippie-expand-function (try-list &optional verbose) – Jeff Li Jun 29 '11 at 09:46
-
The code I posted is an excerpt from the `hippie-exp.el` library in Emacs. You can see it with `M-x find-function RET make-hippie-expand-function RET`. In your case, I think the first of those `(fset)` calls would be relevant. Evaluating that function call (only) gives you a function you can then call with `(my-complete-file ARG)` which is identical to `(hippie-expand ARG)`, except that it only uses the two `try-complete-file-name*` functions when determining completions. On its own, that doesn't solve your problem, but it could be used as part of a solution. – phils Jun 29 '11 at 10:10