How to configure emacs to have it complete the path automatically like vim?

前端 未结 3 971
长情又很酷
长情又很酷 2020-12-21 10:54

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

相关标签:
3条回答
  • 2020-12-21 11:37

    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)
    
    0 讨论(0)
  • 2020-12-21 11:41

    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.

    0 讨论(0)
  • 2020-12-21 11:51

    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.

    0 讨论(0)
提交回复
热议问题