Skip to content

Instantly share code, notes, and snippets.

@alextorma
Created February 11, 2025 21:13
Show Gist options
  • Save alextorma/b4cdb7bdee2898a74c0103c29561a0e8 to your computer and use it in GitHub Desktop.
Save alextorma/b4cdb7bdee2898a74c0103c29561a0e8 to your computer and use it in GitHub Desktop.
Zellij ZSH Tab Completion Session Name Patch
--- comp.zsh 2025-02-10 22:36:57
+++ session_completion.zsh 2025-02-11 16:11:57
@@ -2,6 +2,17 @@
autoload -U is-at-least
+_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
+}
+
_zellij() {
typeset -A opt_args
typeset -a _arguments_options
@@ -122,7 +133,7 @@
'--help[Print help information]' \
&& ret=0
;;
-(attach)
+(attach|a)
_arguments "${_arguments_options[@]}" \
'*--index=[Number of the session index in the active sessions ordered creation date]:INDEX: ' \
'*-c[Create a session if one does not exist]' \
@@ -133,7 +144,7 @@
'*--force-run-commands[If resurrecting a dead session, immediately run all its commands on startup]' \
'-h[Print help information]' \
'--help[Print help information]' \
-'::session-name -- Name of the session to attach to:' \
+'::session-name:_zellij_sessions' \
":: :_zellij__attach_commands" \
"*::: :->attach" \
&& ret=0
@@ -201,20 +212,20 @@
;;
esac
;;
-(kill-session)
+(kill-session|k)
_arguments "${_arguments_options[@]}" \
'-h[Print help information]' \
'--help[Print help information]' \
-'::target-session -- Name of target session:' \
+'::target-session:_zellij_sessions' \
&& ret=0
;;
-(delete-session)
+(delete-session|d)
_arguments "${_arguments_options[@]}" \
'*-f[Kill the session if it'\''s running before deleting it]' \
'*--force[Kill the session if it'\''s running before deleting it]' \
'-h[Print help information]' \
'--help[Print help information]' \
-'::target-session -- Name of target session:' \
+'::target-session:_zellij_sessions' \
&& ret=0
;;
(kill-all-sessions)
@@ -1229,7 +1240,7 @@
_describe -t commands 'zellij action write-chars commands' commands "$@"
}
-_zellij "$@"
+compdef _zellij zellij
function zr () { zellij run --name "$*" -- zsh -ic "$*";}
function zrf () { zellij run --name "$*" --floating -- zsh -ic "$*";}
function zri () { zellij run --name "$*" --in-place -- zsh -ic "$*";}
@alextorma
Copy link
Author

If you saved Zellij's ZSH completions to a file your .zshrc sources, this patch can be applied to it to make completions of session names work with everything else.

Alternatively, you can modify these completions on the fly with the following command(s):

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/' "${XDG_CONFIG_HOME:-$HOME/.config}"/zellij/comp.zsh
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/'

For example, in my .zshrc I have the following line:

[ -e "${XDG_CONFIG_HOME:-$HOME/.config}"/zellij/comp.zsh ] && source <( 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/' \
"${XDG_CONFIG_HOME:-$HOME/.config}"/zellij/comp.zsh )

Credit to @Lockszmith-GH for the initial help in getting these completions sourced correctly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment