I'd like to use a super small value of company-minimum-prefix-length, for example, 2. This is really handy and I don't feel any performance issue. The only thing that annoys me is, when in inferior shell mode(inferior-python-mode, inferior-ess-mode, shell-mode ...), some commands are short, i.e. ls, what I actually want is to run the command after I press RET even there was company tooltip displaying the candidates before I press RET. But what I got is company did the completion and I need to press RET to run the command.

Because this will happen even when company-minimum-prefix-length is set to a large value, for example, 5,

enter image description here

Rather than set a larger value of company-minimum-prefix-length, I'd like to tell company to do completion and and run the command of RET of current major mode if current candidate is already exactly the same as the prefix I typed ? Thanks in advance.

  • Here's a small solution for this. Bind C-m to nil in company-active-map and whenver you encounter a situation like you described, press RET to complete the company candidate or press C-m to just execute return without company completing the candidate. Dec 25, 2016 at 8:33
  • @ChakravarthyRaghunandan Thanks for your suggestions. But for me I rarely use keystrokes like C-j and C-m. Dec 25, 2016 at 8:40

1 Answer 1


Here is a dirty solution

(defun company-immediate-completion-and-run (orig-func &rest args)
  (let ((prefix company-prefix)
        (candidate (nth company-selection company-candidates)))
    (prog1 (apply orig-func args)
      (if (string= prefix candidate)
          (call-interactively (key-binding (kbd "RET")))))))
(advice-add 'company-complete-selection :around #'company-immediate-completion-and-run)

There is a hook company-completion-finished-hook but it seems that we must record the prefix and candidate before finished because once the completion is finished, company will set both back to nil. More elegant solutions are welcome.

  • Why symbol-function?
    – Stefan
    Dec 28, 2016 at 3:04
  • @Stefan I tested and it seems that symbol-function is indeed not necessary. Thanks for pointing out this. When I wrote the snippet, I though it would be necessary to use it. Dec 28, 2016 at 4:51
  • Not only it's not necessary, but it would introduce a bug when the RET key is bound to an autoloaded command (that hasn't yet been loaded).
    – Stefan
    Dec 28, 2016 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.