Search code examples
emacselisp

Code completion key bindings in Emacs


When doing a M-x describe-mode in a .el file, I noticed that the Emacs-Lisp mode actually does code completion. However, lisp-complete-symbol is bound to M-TAB. In Windows, this key binding is taken by Windows for switching the active window. Most IDE's use C-SPC, but that's taken in Emacs as well. What is a good, fairly common key binding for code completion?


Solution

  • If you like completion of all kinds, I recommend M-/ and binding that to hippie-expand.

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

    It does a variety of completions, which are controlled by the variable hippie-expand-try-functions-list. In the .el files, you can set that to do the 'try-complete-lisp-symbol first to get the behavior you're asking for above, along with all the other expansions hippie-expand provides.

    This would do that for you:

    (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))))