[zsh,tmux] Set the tmux window name from the ZSH prompt

This commit is contained in:
Eryn Wells 2022-05-22 17:03:00 -07:00
parent d5ede2e563
commit 98dd139677
2 changed files with 47 additions and 25 deletions

View file

@ -15,6 +15,7 @@ set -g visual-bell off
# Pass xterm titles through # Pass xterm titles through
set -g set-titles on set -g set-titles on
set -g set-titles-string "#T" set -g set-titles-string "#T"
set -g allow-rename on
# Use vi keys for copy mode # Use vi keys for copy mode
set -g mode-keys vi set -g mode-keys vi
@ -30,26 +31,30 @@ set -g status-left-style bg=green,fg=black
set -g status-right "#[bg=red,fg=white] #h #[bg=blue,fg=white] %H:%M %Y-%m-%d #[default]" set -g status-right "#[bg=red,fg=white] #h #[bg=blue,fg=white] %H:%M %Y-%m-%d #[default]"
# Start window and pane indexing from 1 instead of 0 # Start window and pane indexing from 1 instead of 0
set-option -g base-index 1 set -g base-index 1
set-option -g pane-base-index 1 set -g pane-base-index 1
# Tabs like this: "(<index>:<window_name>)" # Tabs like this: "(<index>:<window_name>)"
setw -g window-status-style "fg=white,bg=black" set -g window-status-style "fg=white,bg=black"
setw -g window-status-format " #I #W " set -g window-status-format " #I #W "
setw -g window-status-current-style "fg=white,bg=blue" set -g window-status-current-style "fg=white,bg=blue"
setw -g window-status-current-format " #I #W " set -g window-status-current-format " #I #W "
setw -g window-status-separator "" set -g window-status-separator ""
setw -g window-status-bell-style "bg=red" set -g window-status-bell-style "bg=red"
setw -g window-status-activity-style "bg=red" set -g window-status-activity-style "bg=red"
setw -g alternate-screen on set -g alternate-screen on
setw -g clock-mode-style 24 set -g clock-mode-style 24
# Lock the screen after 120 seconds # Lock the screen after 120 seconds
set-option -g lock-after-time 0 set -g lock-after-time 0
set-option -g lock-command "tmux clock-mode" set -g lock-command "tmux clock-mode"
#
# KEY BINDINGS
#
bind-key C-a last-window bind-key C-a last-window
# TODO: Decide if this should have a -n (allow invoking the command without the leader). # TODO: Decide if this should have a -n (allow invoking the command without the leader).

View file

@ -27,10 +27,12 @@ function prompt_loquacious_setup
add-zsh-hook chpwd set_repo_name add-zsh-hook chpwd set_repo_name
add-zsh-hook precmd set_xterm_title add-zsh-hook precmd set_xterm_title
add-zsh-hook precmd set_tmux_window_name
add-zsh-hook precmd print_newline add-zsh-hook precmd print_newline
add-zsh-hook precmd set_prompt_info add-zsh-hook precmd set_prompt_info
add-zsh-hook preexec print_newline add-zsh-hook preexec print_newline
add-zsh-hook preexec set_tmux_window_name
prompt_opts=(cr subst percent) prompt_opts=(cr subst percent)
@ -80,13 +82,13 @@ function set_repo_name
function set_prompt_info function set_prompt_info
{ {
PS1_HISTORY="`prompt_colorize -b -f 'green' '%h'`" PS1_HISTORY="%F{green}%H%f"
PS1_NAME="`prompt_colorize -f 'magenta' '%n'` " PS1_NAME="%F{magenta}%n%f "
PS1_CWD="in `prompt_colorize -f 'green' '%~'` " PS1_CWD="in %F{green}%~%f "
PS1_STATUS="%(?..`prompt_colorize -b -f 'red' '!'`) " PS1_STATUS="%(?..%F{red}!%f) "
if [[ -n "$SSH_CONNECTION" ]]; then if [[ -n "$SSH_CONNECTION" && "$TERM_PROGRAM" -ne "tmux" ]]; then
PS1_HOST="at `prompt_colorize -f 'red' '%m'` " PS1_HOST="at %F{red}%m%f "
else else
PS1_HOST='' PS1_HOST=''
fi fi
@ -106,22 +108,23 @@ function set_prompt_info
fi fi
# Show background job count if any exist. # Show background job count if any exist.
RPS1="%(1j.[`prompt_colorize -f 'magenta' '%j'`].)" RPS1="%(1j.[%F{magenta}%j%f].)"
PS1_LINE='%# ' PS1_LINE='%(!.%F{red}.%F{default})%#%f '
} }
function set_xterm_title function set_xterm_title
{ {
local title='' local tmux_session_name=''
local title='%n@%m'
if [[ -n "$TMUX" ]]; then if [[ -n "$TMUX" ]]; then
title+="`tmux display-message -p '#S'`: " local tmux_session_name=`tmux display-message -p '#S'`
title+=" (${tmux_session_name})"
fi fi
# Set xterm and screen titles # Set xterm and screen titles
if [[ -n "$DISPLAY" || -n "$TERM_PROGRAM" ]]; then if [[ -n "$DISPLAY" || -n "$TERM_PROGRAM" ]]; then
print -Pn "\e]2;%n@%m\a" print -Pn "\e]2;${title}\a"
fi fi
# For Apple Terminal.app, add a link to the current directory. # For Apple Terminal.app, add a link to the current directory.
@ -130,6 +133,20 @@ function set_xterm_title
fi fi
} }
function set_tmux_window_name
{
if [[ -z "$TMUX" ]]; then
return
fi
local process="${${(z)1}[1]}"
if [[ -n "$process" ]]; then
tmux rename-window "$process"
else
tmux rename-window "`print -Pn "%2~"`"
fi
}
function set_zle_mode_info function set_zle_mode_info
{ {
if [[ -z "$1" || "$1" == 'viins' || "$1" == 'main' ]]; then if [[ -z "$1" || "$1" == 'viins' || "$1" == 'main' ]]; then