Emacs shell-mode and auto-complete integration

Looks nice, doesn’t it?

First you need to install bash-completion mode which will improve shell-mode completion by using bash’s completion support. Note that, at this moment bash-completion mode doesn’t work for me (bash –version = 4.3.11). This is probably the case for you as well. No worries, there is a fix that is not merged to master branch yet. You need to get complete_D branch and install that one.[Now it’s merged] Follow the setup instructions given in README file. And continue below.

Add below lines to your .emacs file:

(defun ac-bash-candidates ()
"This function is a modifed version of
bash-completion-dynamic-complete from bash-completion.el"
  (when bash-completion-enabled
    (let* ( (start (comint-line-beginning-position))
                    (pos (point))
                    (tokens (bash-completion-tokenize start pos))
                    (open-quote (bash-completion-tokenize-open-quote tokens))
                    (parsed (bash-completion-process-tokens tokens pos))
                    (line (cdr (assq 'line parsed)))
                    (point (cdr (assq 'point parsed)))
                    (cword (cdr (assq 'cword parsed)))
                    (words (cdr (assq 'words parsed)))
                    (stub (nth cword words))
                    (completions (bash-completion-comm line point words cword open-quote))
                    ;; Override configuration for comint-dynamic-simple-complete.
                    ;; Bash adds a space suffix automatically.
                    (comint-completion-addsuffix nil) )
      (if completions
                  completions))))

(setq ac-source-bash
	  '((candidates . ac-bash-candidates)))

(add-hook 'shell-mode-hook
		  (lambda()
			(setq ac-sources '(ac-source-bash))
			(auto-complete-mode)))

If you noticed I have actually used bash-completion-dynamic-complete from bash-completion.el.

Leave a Reply

Your email address will not be published.