在.el文件中执行M-x describe-mode时,我注意到Emacs-Lisp模式实际上可以进行代码自动完成。然而,lisp-complete-symbol
绑定了M-TAB键。在Windows系统中,这个键位被用于切换活动窗口。大多数IDE使用C-SPC键,但在Emacs中也被使用了。有没有一个比较好且常见的键位可以用于代码自动完成呢?
如果你喜欢各种自动补全功能,我推荐使用 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))))
M-/
是 dabbrev
的默认绑定(hippe-expand 是其替代品)。此外,M-SPC
默认绑定到 'just-one-space
,我一直在使用。最后,使用标签 <kbd>KEY</kbd> 制作了酷炫的框。 - Trey Jackson(cons 'a (cons' b(delq'a(delq'b list))))
- jrockway正如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)
;;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))
C-M-i; no customization required.
我使用:
(define-key function-key-map [(control tab)] [?\M-\t])
我使用M-.
和M-/
来切换两种补全模式——hippie-expand和标准的emacs补全。
.emacs
文件中,以使Windows赋予Emacs使用M-TAB
的权限:(when (fboundp 'w32-register-hot-key) (w32-register-hot-key [M-tab]))