Emacs中的代码自动完成快捷键绑定

12

在.el文件中执行M-x describe-mode时,我注意到Emacs-Lisp模式实际上可以进行代码自动完成。然而,lisp-complete-symbol绑定了M-TAB键。在Windows系统中,这个键位被用于切换活动窗口。大多数IDE使用C-SPC键,但在Emacs中也被使用了。有没有一个比较好且常见的键位可以用于代码自动完成呢?


5
当然,你可以输入“ESC TAB”。 - Aidan Cully
1
+1 你说得对。为什么会这样?ESC=Meta? - User1
6个回答

9

如果你喜欢各种自动补全功能,我推荐使用 M-/ 并将其绑定到 hippie-expand

(global-set-key (kbd "M-/") 'hippie-expand)

它可以完成多种操作,这些操作由变量hippie-expand-try-functions-list控制。在.el文件中,你可以将其设置为首先执行'try-complete-lisp-symbol,以获得你上面要求的行为,同时还可以使用hippie-expand提供的所有其他扩展。

以下内容可以帮助您实现此操作:

(add-hook 'emacs-lisp-mode-hook 'move-lisp-completion-to-front)
(defun move-lisp-completion-to-front ()
  "Adjust hippie-expand-try-functions-list to have lisp completion at the front."
  (make-local-variable 'hippie-expand-try-functions-list)
  (setq hippie-expand-try-functions-list 
        (cons 'try-complete-lisp-symbol
              (delq 'try-complete-lisp-symbol hippie-expand-try-functions-list)))
  (setq hippie-expand-try-functions-list 
        (cons 'try-complete-lisp-symbol-partially
              (delq 'try-complete-lisp-symbol-partially hippie-expand-try-functions-list))))

Hippie-expand听起来很有趣。为什么你选择了M- /而不是M-SPC(如EmacsWiki页面所示)?另外,你是如何制作出那个酷炫的M- /周围的框的? - User1
1
@User1 M-/dabbrev 的默认绑定(hippe-expand 是其替代品)。此外,M-SPC 默认绑定到 'just-one-space,我一直在使用。最后,使用标签 <kbd>KEY</kbd> 制作了酷炫的框。 - Trey Jackson
顺便提一下,您可以将delq应用于delq的结果,并节省一些复制粘贴:(cons 'a (cons' b(delq'a(delq'b list)))) - jrockway
M-/也是GNU推荐的绑定方式:通常情况下,你可以使用dabbrev-expand将其绑定到M-/,标准的M-/绑定,则可提供其中一个扩展选项。 - user650654

7

正如Trey Jackson所提到的,hippie-expand是最好的选择,但是除了将其绑定到M-/上,我还喜欢使用TAB键来完成所有的补全工作。因此,我在我的.emacs文件中加入了来自Emacs-Wiki的以下内容:

;;function to implement a smarter TAB (EmacsWiki)
(defun smart-tab ()
  "This smart tab is minibuffer compliant: it acts as usual in
    the minibuffer. Else, if mark is active, indents region. Else if
    point is at the end of a symbol, expands it. Else indents the
    current line."
  (interactive)
  (if (minibufferp)
      (unless (minibuffer-complete)
        (hippie-expand nil))
    (if mark-active
        (indent-region (region-beginning)
                       (region-end))
      (if (looking-at "\\_>")
         (hippie-expand nil)
        (indent-for-tab-command)))))
(global-set-key (kbd "TAB") 'smart-tab)

您可以按照以下方式扩展hippie设置:
;;settings for hippie-expand
(setq hippie-expand-try-functions-list
       '(try-complete-lisp-symbol
         try-complete-lisp-symbol-partially
         try-expand-dabbrev
         try-expand-dabbrev-from-kill
         try-expand-dabbrev-all-buffers
         try-expand-line
         try-complete-file-name-partially
         try-complete-file-name))

6

C-M-i; no customization required.


2

我使用:

(define-key function-key-map [(control tab)] [?\M-\t])

1

我使用M-.M-/来切换两种补全模式——hippie-expand和标准的emacs补全。


0
将以下代码放入您的.emacs文件中,以使Windows赋予Emacs使用M-TAB的权限:
(when (fboundp 'w32-register-hot-key) (w32-register-hot-key [M-tab]))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接