Answers:
这是在中定义的company-active-map
。您可以在该映射中取消绑定返回键:
(define-key company-active-map (kbd "<return>") nil)
但是请注意,当有多个候选者时,return和tab会做不同的事情。Tab绑定到company-complete-common
,而return绑定到company-complete-selection
。如果取消绑定返回键,则将无法使用M-n
并M-p
从列表中选择补全。
您可能需要选择其他一些可用于的密钥company-complete-selection
,例如:
(with-eval-after-load 'company
(define-key company-active-map (kbd "<return>") nil)
(define-key company-active-map (kbd "RET") nil)
(define-key company-active-map (kbd "C-SPC") #'company-complete-selection))
解决此烦人的默认行为的完整设置是:
;;; Prevent suggestions from being triggered automatically. In particular,
;;; this makes it so that:
;;; - TAB will always complete the current selection.
;;; - RET will only complete the current selection if the user has explicitly
;;; interacted with Company.
;;; - SPC will never complete the current selection.
;;;
;;; Based on:
;;; - https://github.com/company-mode/company-mode/issues/530#issuecomment-226566961
;;; - /emacs//a/13290/12534
;;; - http://stackoverflow.com/a/22863701/3538165
;;;
;;; See also:
;;; - /emacs//a/24800/12534
;;; - /emacs//q/27459/12534
;; <return> is for windowed Emacs; RET is for terminal Emacs
(dolist (key '("<return>" "RET"))
;; Here we are using an advanced feature of define-key that lets
;; us pass an "extended menu item" instead of an interactive
;; function. Doing this allows RET to regain its usual
;; functionality when the user has not explicitly interacted with
;; Company.
(define-key company-active-map (kbd key)
`(menu-item nil company-complete
:filter ,(lambda (cmd)
(when (company-explicit-action-p)
cmd)))))
(define-key company-active-map (kbd "TAB") #'company-complete-selection)
(define-key company-active-map (kbd "SPC") nil)
;; Company appears to override the above keymap based on company-auto-complete-chars.
;; Turning it off ensures we have full control.
(setq company-auto-complete-chars nil)
(链接到当前配置,该配置已得到进一步改进,但在这一点上非常复杂)
(已根据此问题更新)
(define-key company-active-map (kbd "C-RET") #'company-complete-selection)
但是没有用。您知道这是否可行以及如何实现吗?
TAB
选择列表中的第一项,而无需“交互”。但是要回答您的问题,我敢打赌您的终端无法发送C-RET
(我的不能发送)。尝试C-h k C-RET
看看Emacs是否实际上得到了信号,C-RET
而不是仅仅得到了RET
。
(define-key company-active-map (kbd "TAB") #'company-complete-common-or-cycle)
但是看起来像您的权利,当我更改为C-RET以外的其他选项 时,便获得了所需的性能。谢谢。
M-p
和M-n
,或ISEARCH他们C-s
,或者直接选择与候选M-1
通过M-0
(如果设置显示号码company-show-numbers
)。
在寻找相同问题的解决方案时刚刚看到了这一点(尽管对我来说烦人的钥匙是SPC
和.
)。
我发现将有问题的键绑定到nil
@glucas和@Radon Rosborough提出的建议不是一个很好的UX,因为您需要手动中止公司的下拉菜单,这对于快速键入具有破坏性。
也就是说,如果公司开始活动,而您键入一个键,则绑定不会发生nil
任何事情。这不是您键入字符时所期望的。您可能希望插入字符。
相反,让emacs自动调用company-abort
然后为您插入键入的字符要方便得多。这是我的解决方案:
(defun company--my-insert-spc() (interactive) (company-abort)(insert-char #10r32))
(defun company--my-insert-dot() (interactive) (company-abort)(insert-char #10r46))
(define-key company-active-map (kbd "SPC") 'company--my-insert-spc)
(define-key company-active-map (kbd ".") 'company--my-insert-dot)
我认为您可以RET
通过调用(newline)
而不是轻松地将其扩展到(insert-char)
(尽管我没有尝试过,因为我确实使用RET
过select)。
实际上我正在使用,use-package
所以这就是我的init内容:
(use-package company
:config
(global-company-mode)
(setq company-minimum-prefix-length 3)
(setq company-auto-complete t)
(setq company-show-numbers t)
:bind
(("C-<tab>" . company-complete)
:map company-active-map
("ESC" . company-abort)
;; prevent company from completing on its own when we type regular characters
("SPC" . company--my-insert-spc)
("." . company--my-insert-dot)
)
)