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

前端 未结 3 1099
长情又很酷
长情又很酷 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条回答
  •  梦毁少年i
    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)
    

提交回复
热议问题