如何配置emacs让它像vim一样自动完成路径? [英] How to configure emacs to have it complete the path automatically like vim?
问题描述
我已经成为 vim 用户好几年了.现在由于某些原因我想尝试 Emacs.我在 Vim 中经常使用路径自动完成功能(C-c,C-f).所以我想知道 Emacs 中是否有类似的击键?我用谷歌搜索了这个问题,但只找到了这个答案 https://superuser.com/questions/67170/how-do-i-complete-file-paths-in-emacs .但是,与 vim 为我提供候选列表不同,Emacs 会自动完成第一个候选.所以,我的问题是如何配置 HippieExpand 以使其为我提供候选人列表,而不是自动完成第一个候选人?
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 ?
推荐答案
这是一个使用 hippie-expand 的实现,并使用 ido 作为选择菜单.
Here's an implementation using hippie-expand, and utilising ido for the selection menu.
这给了我们 my-ido-hippie-expand
(我绑定到 Cc e
)作为 ido
等价于 hippie-expand
,并且还可以使用特定的扩展函数(通常是 hippie-expand-try-functions-list
的一些子集)轻松生成其他目标扩展实用程序,促进仅文件名的版本.
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)
这篇关于如何配置emacs让它像vim一样自动完成路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!