0

I would like to get Bash completions for code. How can that be done?

I tried using the search bar to see if this had already been asked (is:q [vscode] bash completion, is:q [vscode] [bash-completion], is:q [vscode] zsh completion, is:q [vscode] [zsh-completion]) and googling "how to get bash completions for vscode" and "how to get zsh completions for vscode". I didn't see any existing Q&A on this topic (or maybe I just missed it?). I did find Add command line completion in zsh for the "code" command #47101, but that didn't really answer my quesiton.

starball
  • 20,030
  • 7
  • 43
  • 238

1 Answers1

2

VS Code provides Bash and Zsh completion files. The source code templates can be found in resources/completions/bash/code and resources/completions/zsh/_code.

Looking at the source code, at the time of this writing, this file is only made part of the installation for Linux builds, where the completions files is placed in usr/share/code/resources/completions/{bash,zsh}/code (and also other directories).

I'm not sure if you actually have to do anything for Deb or RPM installations or if those are integrated automatically. But I definitely did have to take manual action for my Snap installation.

You can either source this file into one of your shell profile files (wherever they are, such as ~/.profile, ~/.bash_profile, etc.), or you can copy or symlink it to one of the locations that your shell looks for completion files. For example, for the Bash completion project, the /usr/share/bash-completion/bash_completion script looks in a directory defined by local -a dirs=(${BASH_COMPLETION_USER_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion}/completions). See also Standard place for user defined bash_completion.d scripts?

For my Snap installation, I did ln -sT /snap/code/current/usr/share/code/resources/completions/bash/code ~/.local/share/bash-completion/completions/code.

starball
  • 20,030
  • 7
  • 43
  • 238
  • In Ubuntu 23.04, adding `source /usr/share/code/resources/completions/zsh/_code` to the top of my `.zshrc` file does not fix auto-complete with `code` from the command line. From zsh, when I try to run `code ` I get the error: `_arguments:comparearguments:327: can only be called from completion function`. – Jayden.Cameron Aug 25 '23 at 07:31
  • @Jayden.Cameron why do you have an underscore in your `/_code`? The file's path is actually like that? – starball Aug 25 '23 at 08:21
  • Yes, the file path has the leading underscore for zsh. You have it also in your answer to the OP when you wrote "and `resources/completions/zsh/_code`. Renaming the file from `_code` to `code` (with or without sourcing from `.zshrc`) does not fix the problem. The command line still returns error `(eval):1: command not found: _code.dpkg-new`. It seems to be looking for `_code.dpkg-new`, but I'm not sure why or where. – Jayden.Cameron Aug 25 '23 at 08:45
  • Also, adding a symlink in `/usr/local/share/zsh/site-functions` to `/usr/share/code/resources/completions/zsh/_code` did not help. Same error: `(eval):1: command not found: _code.dpkg-new`. (Found location for zsh completions from [this post](https://stackoverflow.com/questions/67695861/how-should-i-install-a-zsh-completion-function-so-that-it-works-for-all-users-r#:~:text=6-,Is%20there%20a%20standard%20place%20or%20way%20in%20zsh%20to,default%20in%20every%20user's%20%24fpath%20.)) – Jayden.Cameron Aug 25 '23 at 09:19
  • I originally installed VS code using `apt` following [this tutorial](https://phoenixnap.com/kb/install-vscode-ubuntu). Uninstalling then reinstalling with `snap` using the same tutorial resolved the issue. Not sure what is wrong with the `apt` installation, but I'm glad the issue is resolved now. Thanks for your posts, @starball. – Jayden.Cameron Aug 25 '23 at 09:45