Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Understanding how to setup bash-completion #54

Open
EmilyGraceSeville7cf opened this issue May 13, 2022 · 4 comments
Open

Understanding how to setup bash-completion #54

EmilyGraceSeville7cf opened this issue May 13, 2022 · 4 comments

Comments

@EmilyGraceSeville7cf
Copy link

EmilyGraceSeville7cf commented May 13, 2022

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
Copy link
Author

esc+tab produces the following results:

image

But completion for command options doesn't work.

@szermatt
Copy link
Owner

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
Copy link
Author

EmilyGraceSeville7cf commented May 14, 2022

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
Copy link
Owner

szermatt commented May 16, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants