zellij: Cannot use `zr` or `zrf` without autocompleting `zellij` command once

Basic information

zellij --version: 0.32.0 stty size: 34 72 uname -av: 6.0.6-1-MANJARO #1 SMP PREEMPT_DYNAMIC Sat Oct 29 14:21:50 UTC 2022 x86_64 GNU/Linux alacritty --version: alacritty 0.11.0 omz version: master (fb66b67) Minimal .zshrc:

export ZSH="$HOME/.oh-my-zsh"
source $ZSH/oh-my-zsh.sh

Further information

  • Setup: Placing completions file generated from zellij setup --generate-completion zsh inside $fpath (I’m using oh-my-zsh in this case)

  • Expected behavior: zellij commands such as zr, zrf should work out of the box

  • Actual behavior: zsh: command not found: zr etc. error

  • Working attempt: I have to autocomplete zellij once by typing zellij<space><tab> or zell<tab><tab> (When you see action, attach, convert-layout, etc. arguments). Only after this am I able to run zr or zrf.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 24
  • Comments: 15 (1 by maintainers)

Most upvoted comments

I finally got zellij zsh completion to work using the zsh definition using the following workarounds:

# 1 Using zsh’s native compinit:

Apparently the 2022 update of ZSH (currently the latest) had some changes to discourage loading of completion code manully, and favours auto loading when it’s needed. However some users (and app maintainers) have founds how to workaround it. The solution is to manually call compdef _<command_name> <command_name> after the definition to load it manually.

The sed below takes care of that.

autoload -U +X compinit && compinit
. <( zellij setup --generate-completion zsh | sed -Ee 's/^(_(zellij) ).*/compdef \1\2/' )

# 2 Using bashcompinit bash-completion wrapper:

autoload -U +X compinit bashcompinit && compinit && bashcompinit
. <( zellij setup --generate-completion bash )

I personally prefer # 1, but in case that stops to work # 2 can be a fallback until things are fixed again.

@imsnif could you update the --generate-completion zsh to replace the _zellij call with compdef _zellij zellij ?

I’m currently seeing the same behaviour. I also tried source from my .zshrc file directly, and while this does work it produces this error every time I open a new shell:

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

I believe adding the completions file inside $fpath is the correct way, but as mentioned here this results in autocomplete not working unless a command has been ran at least once.

I finally got zellij zsh completion to work using the zsh definition using the following workarounds:

# 1 Using zsh’s native compinit:

Apparently the 2022 update of ZSH (currently the latest) had some changes to discourage loading of completion code manully, and favours auto loading when it’s needed. However some users (and app maintainers) have founds how to workaround it. The solution is to manually call compdef _<command_name> <command_name> after the definition to load it manually.

The sed below takes care of that.

@Lockszmith-GH, thank you for this! I know this may not be the most appropriate place, but I figured I’d leave a comment here in case anybody else like me ends up on this thread too - I’ve gotten it working with the same method and I’ve also extended it so that it will tab complete session names for delete-session/d, attach/a, and kill-session/k. A patch for the completions can be found here, if you’re like me and you save them to a file. If you want to use a sed command like in the comment above, you can use either of the following:

. <( zellij setup --generate-completion zsh | sed -Ee '/^autoload -U is-at-least/a\
\
_zellij_sessions() {\
    local line sessions desc; sessions=("${(@f)$(zellij ls -n)}")\
    local -a session_names_with_desc\
    for line in "${sessions[@]}"; do\
        session_name=${line%% *}\
        desc=${line#* }\
        session_names_with_desc+=("$session_name:$desc")\
    done\
    _describe -t sessions '\''active session'\'' session_names_with_desc\
}\' -e 's/^(\(attach)/\1|a/' -e 's/::session-name -- Name of the session to attach to:/::session-name:_zellij_sessions/' -e 's/^(\(kill-session)/\1|k/' -e 's/::target-session -- Name of target session:/::target-session:_zellij_sessions/' -e 's/^(\(delete-session)/\1|d/' -e 's/^(_(zellij) ).*/compdef \1\2/')

I also ran into this…if so many people are experiencing this, since 2022, shouldn’t the docs be editted? Is there some reason no PR is being opened?

Just ran into this, macos + zsh here

Same issue here. I have this workaround in my zshrc

if [[ -x "$(command -v zellij)" ]];
then
    eval "$(zellij setup --generate-completion zsh | grep "^function")"
fi;