casey / just

🤖 Just a command runner
https://just.systems
Creative Commons Zero v1.0 Universal
21.84k stars 486 forks source link

Add instructions to readme for setting up completions for supported shells #618

Open casey opened 4 years ago

casey commented 4 years ago
kenden commented 4 years ago

For zsh, using .oh-my-zsh, this works, but this is just a hack. If someone has a cleaner way, please share!

(edited March 28, 2023)

mkdir ~/.oh-my-zsh/plugins/just
just --completions zsh > ~/.oh-my-zsh/plugins/just/_just

Edit ~/.zshrc to add just in the plugin list, e.g.

plugins=(
        git
        docker
        kubectl
        cargo
        rust
        just
)

And also add this to ~/.zshrc get the plugin to work (not needed in test from March 28 2023)

autoload -Uz compinit && compinit -C  # hack to load just autocomplete

Note: the hack breaks some oh-my-zsh plugins, like 'go'.

lucasholder commented 4 years ago

+1 , can't figure out how to use it with zplug :( https://github.com/zplug/zplug/issues/547

schodet commented 4 years ago

For zsh, put the completion script somewhere in your fpath, and be sure to have your fpath defined before calling compinit.

For example:

mkdir -p .zsh/completion
just --completions=zsh > .zsh/completion/_just

And in your .zshrc:

fpath=(~/.zsh/completion $fpath)
…
autoload -U compinit
compinit
kenden commented 4 years ago

The code in my last comment had an issue; I found it actually works directly with ohmyzsh.

I created a pull request to make it easier to add to ohmyzsh: https://github.com/ohmyzsh/ohmyzsh/pull/9287 -> edit march28 2023. This seems broken

casey commented 4 years ago

@kenden Nice! Please let me know when that PR lands, so it can be added to the readme.

vmiheer commented 4 years ago

@kenden I tried below with your commit (ohmyzsh/ohmyzsh#9287), looking at omz wiki:

mkdir $ZSH_CUSTOM/plugins/just
 wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/f90284951b33dc4dc9b5ee734e82bab8f615d344/plugins/just/_just -O $ZSH_CUSTOM/plugins/just/just.plugin.zsh
# in .zshrc
# plugins=(... just)

When I start zsh I get error:

_arguments:comparguments:325: can only be called from completion function

Am I missing something?

kenden commented 4 years ago

@vmiheer My ~/.zshrc is based on the ohmyzsh template in ~/.oh-my-zsh/templates/zshrc.zsh-template Does it work if you use this template? These seem to be the important parts of it

export ZSH=$HOME/.oh-my-zsh
plugins=(just)
source $ZSH/oh-my-zsh.sh
anacrolix commented 2 years ago

Is something like eval "$(just --completions bash)" in my bashrc okay for bash?

casey commented 2 years ago

Is something like eval "$(just --completions bash)" in my bashrc okay for bash?

Good question! I think so, but I'm not a bash user, so I'm not 100% sure.

oeb25 commented 2 years ago

For fish this worked for me

just --completions fish > $__fish_config_dir/completions/just.fish

or if your fish config is in the default directory

just --completions fish > $HOME/.config/fish/completions/just.fish
nk9 commented 2 years ago

I got this working using @schodet's code, but be sure you put the .zsh folder in $HOME. :-)

nk9 commented 2 years ago

I've learned a bit more about why this wasn't working for me and I wanted to share. My setup:

I think many Mac users will be in the same boat, especially now that zsh is the default macOS shell.

In this case, the solution is really simple. I just needed to add this line before OMZ was started in my ~/.zshrc:

fpath=(/opt/homebrew/share/zsh/site-functions $fpath)

This is because the just formula adds the completions file (named _just) to the Homebrew zsh completions directory as part of the postinstall script.

$ brew info just
just: stable 1.2.0 (bottled), HEAD
[...]
==> Caveats
zsh completions have been installed to:
  /opt/homebrew/share/zsh/site-functions

OMZ turns on completions as part of its setup, but it looks in the fpath to do it. And since I was using the built-in version of zsh, instead of the Homebrew version, this directory wasn't included by default in fpath.

If you set up the Homebrew shell environment before OMZ, you can also use a variable:

eval "$(/opt/homebrew/bin/brew shellenv)"

plugins=(git docker zsh-z) #etc
fpath=($HOMEBREW_PREFIX/share/zsh/site-functions $fpath)

source $ZSH/oh-my-zsh.sh

Now that I'm using the completion script auto-installed in site-functions, it will get updated along with just by Homebrew. As a bonus, many other Homebrew formulae put completions in this directory (asdf, bat, rg, brew itself, etc). I wasn't getting any of them. Basically, my ZSH was just misconfigured.

If I had installed zsh using Homebrew, I expect everything would have Just Workedâ„¢.

casey commented 2 years ago

It think this would be a great addition to the completion scripts section of the readme, if you feel like opening a PR.

k-bx commented 1 year ago

For zsh, using .oh-my-zsh, this works, but this is just a hack. If someone has a cleaner way, please share!

mkdir ~/.oh-my-zsh/plugins/just
invoke --print-completion-script=zsh > ~/.oh-my-zsh/plugins/just/_just

Edit ~/.zshrc to add just in the plugin list, e.g.

plugins=(
        git
        docker
        kubectl
        cargo
        rust
        just
)

And also add this to ~/.zshrc get the plugin to work

autoload -Uz compinit && compinit -C  # hack to load just autocomplete

Note: the hack breaks some oh-my-zsh plugins, like 'go'.

I get "zsh: command not found: invoke" on this one, but just --completions=zsh > ~/.oh-my-zsh/plugins/just/_just did the job.

kenden commented 1 year ago

I get "zsh: command not found: invoke" on this one, but just --completions=zsh > ~/.oh-my-zsh/plugins/just/_just did the job.

Thanks @k-bx , that was a mistake, I updated https://github.com/casey/just/issues/618#issuecomment-601824467

alexpovel commented 1 year ago

Using zsh and oh-my-zsh, my $fpath turned out to already contain (never explicitly touched it, so this should be a vanilla output) ~/.oh-my-zsh/completions. Check yours with:

echo $fpath | grep "$HOME/.oh-my-zsh/completions"

Note that it outputs absolute paths. That directory didn't exist yet, however. I was able to simply do:

local ZSH_COMPLETIONS_DIR="$HOME/.oh-my-zsh/completions"
mkdir -p $ZSH_COMPLETIONS_DIR
just --completions zsh > "${ZSH_COMPLETIONS_DIR}/_just"

and it worked on shell startup. The _just filename is mandatory; just.zsh or similar won't work.