zellij icon indicating copy to clipboard operation
zellij copied to clipboard

Also complete session names in generated zsh completions

Open cyruseuros opened this issue 1 year ago • 1 comments

Exactly like in https://github.com/zellij-org/zellij/issues/1030 - personally I've never written any custom zsh completions so unfortunately I can't contribute here.

cyruseuros avatar Jan 24 '24 14:01 cyruseuros

I asked Claud to translate the comp.fish to zsh and I got this

# Function to list sessions
_zellij_list_sessions() {
    zellij list-sessions --short --no-formatting 2>/dev/null
}

# Function to list aliases
_zellij_list_aliases() {
    zellij list-aliases 2>/dev/null
}

# Zellij completions
_zellij() {
    local state

    _arguments \
        '1: :->command' \
        '*: :->args'

    case $state in
        command)
            _arguments '1:command:(attach a kill-session k setup)'
            ;;
        args)
            case ${words[2]} in
                attach|a|kill-session|k)
                    _alternative 'sessions:session:($(_zellij_list_sessions))'
                    ;;
                setup)
                    _arguments '*:option:((--generate-completion\:"Generate shell completion" bash\:"Bash" elvish\:"Elvish" fish\:"Fish" zsh\:"Zsh" powershell\:"PowerShell"))'
                    ;;
            esac
            ;;
    esac
}

compdef _zellij zellij

# Custom functions
zr() {
    command zellij run --name "$1" -- zsh -c "$*"
}

zrf() {
    command zellij run --name "$1" --floating -- zsh -c "$*"
}

zri() {
    command zellij run --name "$1" --in-place -- zsh -c "$*"
}

ze() {
    command zellij edit "$@"
}

zef() {
    command zellij edit --floating "$@"
}

zei() {
    command zellij edit --in-place "$@"
}

# zpipe function and its completions
zpipe() {
    if (( $# > 0 )); then
        command zellij pipe -p "$@"
    else
        command zellij pipe
    fi
}

_zpipe() {
    _alternative 'aliases:alias:($(_zellij_list_aliases))'
}

compdef _zpipe zpipe

this seem to work, but does make some of the stuff kind of ugly tho, and I dont know what it does

c60cb859 avatar Aug 05 '24 06:08 c60cb859

It is unfortunate that clap can not generate this. Also it does not seem to be straight forward to add it manually either. Instead, I am providing a patch file that you can download, which will add dynamic session completion to the attach/kill-session/delete-session commands. (https://gist.github.com/sstark/22318d37b8fc9a21c60a354eec460fff). It was generated using zellij 0.41.2.

First download the gist and save it to _zellij.patch. Then you can apply it to the generated completion. Here are the commands for zsh:

curl -O https://gist.githubusercontent.com/sstark/22318d37b8fc9a21c60a354eec460fff/raw/57f0d1d6bfe23e68813d2b8d96ba13c381c347bf/_zellij.patch
patch -s -o - =(zellij setup --generate-completion zsh) _zellij.patch > _zellij

Copy the resulting patched _zellij file to wherever you keep your completions.

sstark avatar Nov 22 '24 10:11 sstark

Just wanted to say thanks to @sstark.

Below is a one-liner to generate the _zellij completion script:

patch -s -o - \
    <(zellij setup --generate-completion zsh) \
    <(curl -sL https://gist.githubusercontent.com/sstark/22318d37b8fc9a21c60a354eec460fff/raw/57f0d1d6bfe23e68813d2b8d96ba13c381c347bf/_zellij.patch) \
    > ${MY_ZSH_COMLETION_PATH:-~/.local/share/zsh/completions}/_zellij

On one system, I actually do source it (because - reasons, not particularly good ones), and it looks like this:

. <(
    patch -s -o - \
        <(zellij setup --generate-completion zsh) \
        <(curl -sL https://gist.githubusercontent.com/sstark/22318d37b8fc9a21c60a354eec460fff/raw/57f0d1d6bfe23e68813d2b8d96ba13c381c347bf/_zellij.patch) \
        | sed -Ee 's/^(_(zellij) ).*/compdef \1\2/'
)

#or

. <( sed -Ee 's/^(_(zellij) ).*/compdef \1\2/' ~/.local/share/lib/_zellij_zsh_lib )

Hope others find this helpful.

Lockszmith-GH avatar Dec 16 '24 19:12 Lockszmith-GH

Good resource but now "out of date" as the completions have changed in more recent releases.

As these are generated by Clap now, I'm not sure where would be appropriate to add these patch changes so they are automatically included in future builds.

This is super helpful once combined with fzf if you have more than a couple of detached sessions.

arrrgi avatar Apr 18 '25 06:04 arrrgi

Zsh's latest version, since 2022, is 5.9 (source) My Zellij version is 0.42.2

I'm using these zsh versions flawlessly:

$ zsh --version # output from different machines
 
zsh 5.9 (arm64-apple-darwin24.0)
zsh 5.8 (x86_64-redhat-linux-gnu)
zsh 5.9 (x86_64-debian-linux-gnu)

@arrrgi, I feel like I'm misunderstanding your comment. What breaks?

Lockszmith-GH avatar Apr 23 '25 20:04 Lockszmith-GH

It does patch, but it also has a mismatch leading to the following on the last line of the file

}1 out of 5 hunks failed--saving rejects to -.rej

arrrgi avatar Apr 23 '25 21:04 arrrgi

you are correct, there is an error, however, it only fails to patch the zpipe command, not a critical issue. the session list is still loaded.

I'll try and create a proper correction later, in the meantime you can use 2> /dev/null to suppress the error message.

Lockszmith-GH avatar Apr 24 '25 14:04 Lockszmith-GH

@Lockszmith-GH happy to retract my previous comment that this is "out-of-date"

I tried updating the patch here https://gist.githubusercontent.com/arrrgi/63263e473117188db7e804ab9b4eb202/raw/d8830a3dc1cfdde1c4933f3f7a35cc9e4ae86f36/_zellij.patch

However, the one-liner command you shared just gives me a zero byte file, and I could only get this to work with:

zellij setup --generate-completion zsh > /tmp/_zellij.tmp && \
  curl -sL https://gist.githubusercontent.com/arrrgi/63263e473117188db7e804ab9b4eb202/raw/d8830a3dc1cfdde1c4933f3f7a35cc9e4ae86f36/_zellij.patch | patch \
  -s -o _zellij.new /tmp/_zellij.tmp - && \
  rm /tmp/_zellij.tmp

Which feels pretty dirty in comparison. ie. I had to use temporary files. I'd love to understand what's wrong with the patch, which I created with diff -u _zellij.zsh _zellij > _zellij.patch

arrrgi avatar Apr 25 '25 02:04 arrrgi

If you look at the patch, it's fairly easy to manually add the changes to the output of zellij's generated completion. I am not planning to update this, as I am not using zellij currently, sorry. Also, this should be fixed in zellij properly.

sstark avatar Apr 25 '25 04:04 sstark

Agreed, this is a clunky workaround for now

arrrgi avatar Apr 25 '25 06:04 arrrgi