szermatt / emacs-bash-completion

Add programmable bash completion to Emacs shell-mode
GNU General Public License v2.0
279 stars 33 forks source link

Understanding how to setup bash-completion #54

Open EmilyGraceSeville7cf opened 2 years ago

EmilyGraceSeville7cf commented 2 years ago

I have the following ~/.emacs init file config now:

(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
(package-initialize)

(when (version<= "26.0.50" emacs-version )
  (global-display-line-numbers-mode))
(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(package-selected-packages (quote (bash-completion))))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )

(require 'bash-completion)
(bash-completion-setup)

My goal to have Bash completion automatically enabled in all .sh files. But now when I try press tab in .sh files nothing is suggested. May be I am doing smth wrong. 🤔

If I find the decision I will place it in the comments.

EmilyGraceSeville7cf commented 2 years ago

esc+tab produces the following results:

image

But completion for command options doesn't work.

szermatt commented 2 years ago

Completion in .sh files with bash-completion isn't supported by (bash-completion-setup). With (bash-completion-setup) you get completion in bash shells, started with M-x shell.

Getting it to work is likely possible - with some limitations - but definitely nontrivial.

EmilyGraceSeville7cf commented 2 years ago

Do you know some plugins that do what I want? If there are no such ones can you give me some advice what to pay attention for while creating them?

szermatt commented 2 years ago

I'm not aware of any plugins that does what you want.

To do it yourself, you'd need to write an adapter: add a new completion function intocompletion-at-point-functions as described on https://www.gnu.org/software/emacs/manual/html_node/elisp/Completion-in-Buffers.html that eventually calls (bash-completion-dynamic-complete-nocomint start (point) 'dynamic-table) Make sure you set bash-completion-use-separate-processes to t either globally or just before calling that function.

The tricky part is, I think, figuring out what start should be in the general case. In the simple case, it could be the beginning of the line, but Bash allows commands to be spread over multiple lines and many scripts use that so that'll likely require parsing. There are some parsing helper functions in bash-completion.el, but nothing ready-made; you'd probably want to write your own.

With that setup, bash completion will not be able to complete functions or variables that are not generally available (defined in the .bashrc, for example) and will not be aware of the script's current directory; getting that to work would need more work. If all you want is completion of the command-line options, you might not care about that.