2

In my environment, I have bash autocomplete set up to do lots of amazing things. However, when I'm editing a shell script in emacs, all of that autocomplete magic goes away.

Is there any way to get emacs to do bash command line autocompletion when I'm editing shell scripts?

I would accept an answer that requires me to use a different editor, if that editor were available on Linux systems. I would also accept an answer that tells me that I need to use a different shell's autocomplete.

3
  • 1
    yes, you can use bash-completion.el Commented Nov 10, 2019 at 19:13
  • As far as I can tell, that package is only for shell-mode (where I'm actually executing commands) and not for sh-mode (where I'm editing a shell script) which is what I need. I certainly can't get it to work in sh-mode, although it works in shell-mode. Commented Nov 11, 2019 at 21:00
  • it can be used in sh-mode as well with a little bit of elisp, I'll add an example config Commented Nov 11, 2019 at 21:03

1 Answer 1

2

bash-completion.el provides an interface to normal bash completion, which can also be extended to work in normal sh-modes with an addition to your completion-at-point-functions. Here is how that could work, just using the command sh-beginning-of-command from sh-script to determine the current completion candidate (this could be extended to handle more complicated shell commands if necessary).

(require 'sh-script)                    ;sh-beginning-of-command
(require 'bash-completion)

(defun my-sh-completion-at-point ()
  (let ((end (point))
        (beg (save-excursion (sh-beginning-of-command))))
    (when (and beg (> end beg))
      (bash-completion-dynamic-complete-nocomint beg end t))))

(defun my-sh-hook ()
  (add-hook 'completion-at-point-functions #'my-sh-completion-at-point nil t))

(add-hook 'sh-mode-hook #'my-sh-hook)
Sign up to request clarification or add additional context in comments.

3 Comments

If I may pester you just a little longer -- There seem to be two modes. Sometimes TAB does indentation while M-TAB does completion, and other times both TAB and M-TAB do completion and nothing does indentation. For example, the first time I edit a particular line, I need to use M-TAB for completion the first time, and then just TAB will do completion after that. Is there a way to control this, or if not, can you help me understand it?
If you have (setq tab-always-indent 'complete) (which I would do), then TAB will try to indent first, and if the line is already indented it will do completion. With this setting I peronally never use M-TAB, but see options for tab-always-indent
For me your solution does not work. If I try to complete something, like a filesystem path, i got "No match" in the echo area.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.