refactor(zellij): separate root alias from sub-command prefix

Split _zellij_root_alias and _zellij_short_prefix so that when z is
taken (e.g. by zoxide), only the root alias falls back to zj while
sub-commands keep the shorter z prefix (zl, za, zk, zd, …).

Every alias and function now checks for conflicts before defining,
and compdef targets are collected dynamically to avoid binding
completions to names that were never created.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Hobeom 2026-03-09 22:59:45 +09:00
commit 15fbdaab58
2 changed files with 102 additions and 43 deletions

View file

@ -10,35 +10,43 @@ plugins=(... zellij)
## Dynamic prefix ## Dynamic prefix
The default alias prefix is `zj`. To use the shorter `z` prefix instead, set the following The default prefix is `zj`. To use the shorter `z` prefix instead, set the following variable
variable before oh-my-zsh is sourced: before oh-my-zsh is sourced:
```zsh ```zsh
ZSH_ZELLIJ_PREFIX_Z=true ZSH_ZELLIJ_PREFIX_Z=true
``` ```
> **Note:** If `z` is already defined as an alias, function, or command by another plugin When `ZSH_ZELLIJ_PREFIX_Z` is set, the root alias (`z`) and sub-command prefix are handled
> (e.g., zoxide), the `zj` prefix is used for the main `zellij` alias even when separately:
> `ZSH_ZELLIJ_PREFIX_Z` is set.
- If `z` is **not** taken → `z`=zellij, sub-commands use `z` prefix (`zl`, `za`, …)
- If `z` **is** taken (e.g., by zoxide) → `zj`=zellij, but sub-commands still use `z` prefix (`zl`, `za`, …)
This means only the root alias falls back to `zj`; the shorter sub-command shortcuts remain
usable.
All aliases and functions perform a conflict check before being defined — if a name is already
taken by another alias, function, or command, it is silently skipped.
## Aliases ## Aliases
| Alias (default) | Alias (with `z`) | Command | Description | | Alias (default) | Alias (with `z`) | Alias (`z` + conflict) | Command | Description |
| ---------------- | ---------------- | ---------------------------- | ------------------------ | | ---------------- | ---------------- | ---------------------- | ---------------------------- | ------------------------ |
| `zj` | `z` | `zellij` | Zellij command | | `zj` | `z` | `zj` | `zellij` | Zellij command |
| `zjl` | `zl` | `zellij list-sessions` | List sessions | | `zjl` | `zl` | `zl` | `zellij list-sessions` | List sessions |
| `zjs` | `zs` | `zellij -s` | Start a named session | | `zjs` | `zs` | `zs` | `zellij -s` | Start a named session |
| `zjda` | `zda` | `zellij delete-all-sessions` | Delete all sessions | | `zjda` | `zda` | `zda` | `zellij delete-all-sessions` | Delete all sessions |
| `zjka` | `zka` | `zellij kill-all-sessions` | Kill all sessions | | `zjka` | `zka` | `zka` | `zellij kill-all-sessions` | Kill all sessions |
| `zjr` | — | `zellij run` | Run a command in a pane | | `zjr` | — | — | `zellij run` | Run a command in a pane |
## Functions ## Functions
| Function (default) | Function (with `z`) | Command | Description | | Function (default) | Function (with `z`) | Command | Description |
| ------------------- | ------------------- | ------------------------------ | --------------------------------- | | ------------------- | ------------------- | ------------------------ | -------------------- |
| `zja` | `za` | `zellij attach` | Attach to a session | | `zja` | `za` | `zellij attach` | Attach to a session |
| `zjd` | `zd` | `zellij delete-session` | Delete a session | | `zjd` | `zd` | `zellij delete-session` | Delete a session |
| `zjk` | `zk` | `zellij kill-session` | Kill a session | | `zjk` | `zk` | `zellij kill-session` | Kill a session |
The following convenience functions are always available (unless the name is already taken): The following convenience functions are always available (unless the name is already taken):

View file

@ -2,36 +2,82 @@ if (( ! $+commands[zellij] )); then
return return
fi fi
_omz_zellij_taken() {
(( $+aliases[$1] || $+functions[$1] || $+commands[$1] ))
}
typeset -ga _zellij_comp_targets _zellij_all_session_targets _zellij_running_session_targets _zellij_exited_session_targets
_zellij_comp_targets=()
_zellij_all_session_targets=()
_zellij_running_session_targets=()
_zellij_exited_session_targets=()
if [[ -n ${ZSH_ZELLIJ_PREFIX_Z:-} ]]; then if [[ -n ${ZSH_ZELLIJ_PREFIX_Z:-} ]]; then
_zellij_prefix=z _zellij_short_prefix=z
if _omz_zellij_taken z; then
_zellij_root_alias=zj
else
_zellij_root_alias=z
fi
else else
_zellij_prefix=zj _zellij_short_prefix=zj
_zellij_root_alias=zj
fi fi
if ! _omz_zellij_taken "$_zellij_root_alias"; then
if [[ -n ${ZSH_ZELLIJ_PREFIX_Z:-} ]] && (( ! $+aliases[z] && ! $+functions[z] && ! $+commands[z] )); then alias ${_zellij_root_alias}='zellij'
alias z='zellij' _zellij_comp_targets+=("$_zellij_root_alias")
else
alias zj='zellij'
fi fi
# alias ${_zellij_prefix}='zellij' if ! _omz_zellij_taken "${_zellij_short_prefix}l"; then
alias ${_zellij_prefix}l='zellij list-sessions' alias ${_zellij_short_prefix}l='zellij list-sessions'
alias ${_zellij_prefix}s='zellij -s' _zellij_comp_targets+=("${_zellij_short_prefix}l")
alias ${_zellij_prefix}da='zellij delete-all-sessions' fi
alias ${_zellij_prefix}ka='zellij kill-all-sessions'
[[ $_zellij_prefix != z ]] && alias ${_zellij_prefix}r='zellij run'
eval "${_zellij_prefix}a() { command zellij attach \"\$@\"; }" if ! _omz_zellij_taken "${_zellij_short_prefix}s"; then
eval "${_zellij_prefix}d() { command zellij delete-session \"\$@\"; }" alias ${_zellij_short_prefix}s='zellij -s'
eval "${_zellij_prefix}k() { command zellij kill-session \"\$@\"; }" _zellij_comp_targets+=("${_zellij_short_prefix}s")
fi
if ! _omz_zellij_taken "${_zellij_short_prefix}da"; then
alias ${_zellij_short_prefix}da='zellij delete-all-sessions'
_zellij_comp_targets+=("${_zellij_short_prefix}da")
fi
if ! _omz_zellij_taken "${_zellij_short_prefix}ka"; then
alias ${_zellij_short_prefix}ka='zellij kill-all-sessions'
_zellij_comp_targets+=("${_zellij_short_prefix}ka")
fi
if [[ $_zellij_short_prefix != z ]] && ! _omz_zellij_taken "${_zellij_short_prefix}r"; then
alias ${_zellij_short_prefix}r='zellij run'
_zellij_comp_targets+=("${_zellij_short_prefix}r")
fi
if ! _omz_zellij_taken "${_zellij_short_prefix}a"; then
eval "${_zellij_short_prefix}a() { command zellij attach \"\$@\"; }"
_zellij_all_session_targets+=("${_zellij_short_prefix}a")
fi
if ! _omz_zellij_taken "${_zellij_short_prefix}d"; then
eval "${_zellij_short_prefix}d() { command zellij delete-session \"\$@\"; }"
_zellij_exited_session_targets+=("${_zellij_short_prefix}d")
fi
if ! _omz_zellij_taken "${_zellij_short_prefix}k"; then
eval "${_zellij_short_prefix}k() { command zellij kill-session \"\$@\"; }"
_zellij_running_session_targets+=("${_zellij_short_prefix}k")
fi
(( $+functions[zr] || $+aliases[zr] || $+commands[zr] )) || zr() { command zellij run -- "$@"; } (( $+functions[zr] || $+aliases[zr] || $+commands[zr] )) || zr() { command zellij run -- "$@"; }
(( $+functions[zrf] || $+aliases[zrf] || $+commands[zrf] )) || zrf() { command zellij run --floating -- "$@"; } (( $+functions[zrf] || $+aliases[zrf] || $+commands[zrf] )) || zrf() { command zellij run --floating -- "$@"; }
(( $+functions[ze] || $+aliases[ze] || $+commands[ze] )) || ze() { command zellij edit "$@"; } (( $+functions[ze] || $+aliases[ze] || $+commands[ze] )) || ze() { command zellij edit "$@"; }
_ZELLIJ_COMP_FILE="${ZSH_CACHE_DIR}/completions/_zellij" _ZELLIJ_COMP_DIR="${ZSH_CACHE_DIR}/completions"
mkdir -p "${_ZELLIJ_COMP_FILE:h}" _ZELLIJ_COMP_FILE="${_ZELLIJ_COMP_DIR}/_zellij"
mkdir -p "$_ZELLIJ_COMP_DIR"
(( ${fpath[(I)$_ZELLIJ_COMP_DIR]} )) || fpath=("$_ZELLIJ_COMP_DIR" $fpath)
if [[ ! -s $_ZELLIJ_COMP_FILE ]]; then if [[ ! -s $_ZELLIJ_COMP_FILE ]]; then
command zellij setup --generate-completion zsh >| "$_ZELLIJ_COMP_FILE" 2>/dev/null command zellij setup --generate-completion zsh >| "$_ZELLIJ_COMP_FILE" 2>/dev/null
@ -57,7 +103,7 @@ _omz_zellij_running_sessions() {
local out local out
local -a sessions local -a sessions
out="$(_omz_zellij_ls_raw)" out="$(_omz_zellij_ls_raw)"
sessions=("${(@f)$(printf '%s\n' "$out" | LC_ALL=C sed -nE '/EXITED/!s/^([^[:space:]]+).*/\1/p')}") sessions=("${(@f)$(printf '%s\n' "$out" | LC_ALL=C sed -nE '/\(EXITED/!s/^([^[:space:]]+).*/\1/p')}")
(( ${#sessions[@]} )) && compadd -Q -a sessions (( ${#sessions[@]} )) && compadd -Q -a sessions
} }
@ -66,18 +112,23 @@ _omz_zellij_exited_sessions() {
local out local out
local -a sessions local -a sessions
out="$(_omz_zellij_ls_raw)" out="$(_omz_zellij_ls_raw)"
sessions=("${(@f)$(printf '%s\n' "$out" | LC_ALL=C sed -nE '/EXITED/s/^([^[:space:]]+).*/\1/p')}") sessions=("${(@f)$(printf '%s\n' "$out" | LC_ALL=C sed -nE '/\(EXITED/s/^([^[:space:]]+).*/\1/p')}")
(( ${#sessions[@]} )) && compadd -Q -a sessions (( ${#sessions[@]} )) && compadd -Q -a sessions
} }
if (( $+functions[compdef] )); then if (( $+functions[compdef] )); then
autoload -Uz _zellij autoload -Uz _zellij
compdef _zellij zellij ${_zellij_prefix} ${_zellij_prefix}l ${_zellij_prefix}s compdef _zellij zellij ${_zellij_comp_targets[@]}
[[ $_zellij_prefix != z ]] && compdef _zellij ${_zellij_prefix}r (( ${#_zellij_all_session_targets[@]} )) && compdef _omz_zellij_all_sessions ${_zellij_all_session_targets[@]}
compdef _omz_zellij_all_sessions ${_zellij_prefix}a (( ${#_zellij_running_session_targets[@]} )) && compdef _omz_zellij_running_sessions ${_zellij_running_session_targets[@]}
compdef _omz_zellij_running_sessions ${_zellij_prefix}k (( ${#_zellij_exited_session_targets[@]} )) && compdef _omz_zellij_exited_sessions ${_zellij_exited_session_targets[@]}
compdef _omz_zellij_exited_sessions ${_zellij_prefix}d
fi fi
unset _ZELLIJ_COMP_DIR
unset _ZELLIJ_COMP_FILE unset _ZELLIJ_COMP_FILE
unset _zellij_prefix unset _zellij_root_alias
unset _zellij_short_prefix
unset _zellij_comp_targets
unset _zellij_all_session_targets
unset _zellij_running_session_targets
unset _zellij_exited_session_targets