Skip to content

Instantly share code, notes, and snippets.

@LintaoAmons
Created October 8, 2022 09:50
Show Gist options
  • Select an option

  • Save LintaoAmons/22f6184b26bd5b93d8fe9f9276f50f75 to your computer and use it in GitHub Desktop.

Select an option

Save LintaoAmons/22f6184b26bd5b93d8fe9f9276f50f75 to your computer and use it in GitHub Desktop.
Popup terminal with tmux
# ... your other config
bind-key -n M-3 run-shell 'toggle-tmux-popup'
# you can switch `M-3` to any keybindings you like.
#!/bin/bash
# 👻 put this file into your $PATH, normally `~/.local/bin/toggle-tmux-popup`
# 👻 this script should have runable permission.
if [ "$(tmux display-message -p -F "#{session_name}")" = "popup" ];then
tmux detach-client
else
tmux popup -d '#{pane_current_path}' -xC -yC -w80% -h75% -E "tmux attach -t popup || tmux new -s popup"
fi
@q962
Copy link

q962 commented Oct 11, 2022

bind-key -n M-3 run-shell -b "\
    if test 1 -eq #{==:#{session_name},popup} ; then \
        tmux detach-client; \
    else \
        tmux popup -d \"#{pane_current_path}\" -xC -yC -w80% -h75% -E \"tmux attach -t popup || tmux new -s popup\"; \
    fi\
"

@q962
Copy link

q962 commented Oct 12, 2022

session 独立 popup window

# alt-3 弹出 popup
bind-key -n M-3 run-shell -b '\
    popup_id="popup_$(tmux display-message -p "##{session_id}" | cut -d"\$" -f2)"; \
    if test "$(echo #{session_name} | cut -d"_" -f 1)" = "popup" ; then \
        tmux detach-client; \
    else \
        tmux popup -d "#{pane_current_path}" -xC -yC -w80% -h75% \
            -E "tmux attach -t \"${popup_id}\" || tmux new -s \"${popup_id}\" "; \
    fi; \
'

@bfrg
Copy link

bfrg commented Nov 6, 2022

There's no need to use run-shell, the if-check can be done using the if-shell command with the -F option. The above snippet simplified:

# Toggle popup window with Alt-3
bind-key -n -N 'Toggle popup window' M-3 if-shell -F '#{==:#{session_name},popup}' {
    detach-client
} {
    display-popup -d "#{pane_current_path}" -xC -yC -w 80% -h 75% -E 'tmux attach-session -t popup || tmux new-session -s popup'
}

This is also significantly more readable. Though, I think it requires a more recent tmux version.

@guanghechen
Copy link

@LintaoAmons the -A option can auto create new session if it's not exist.

#! /usr/bin/env bash

tmux_popup_session_name="_popup"

if [ "$(tmux display-message -p -F "#{session_name}")" = "${tmux_popup_session_name}" ];then
    tmux detach-client
else
    tmux popup -d '#{pane_current_path}' -xC -yC -w80% -h80% -E\
      "tmux new-session -A -s ${tmux_popup_session_name}"
fi

@JackTheMico
Copy link

Pop-up windows based on the current directory name.

bind-key -n M-g if-shell -F '#{==:#{session_name},#{b:pane_current_path}}' {
    detach-client
} {
    display-popup -d "#{pane_current_path}" -xC -yC -w 90% -h 85% -E 'tmux new-session -A -s (tmux display-message -p "#{b:pane_current_path}")'
}

@gogongxt
Copy link

I found that popup tmux session cannot share clipboard in copy-mode-vi.
Does someone know what the problem and how to solve it?

@q962
Copy link

q962 commented Aug 18, 2025

bind-key -T copy-mode-vi y                 send-keys -X copy-pipe 'tmux save-buffer - | wl-copy'

@bfrg
Copy link

bfrg commented Aug 18, 2025

@gogongxt It works fine on my machine. I have the following in my .tmux.conf:

# Be more like Vim in copy-mode-vi
bind-key -T copy-mode-vi y send-keys -X copy-pipe 'xclip -i -selection clipboard'
bind-key -T copy-mode-vi Y send-keys -X copy-pipe-end-of-line 'xclip -i -selection clipboard'
bind-key -T copy-mode-vi v if-shell -F '#{selection_active}' { send-keys -X clear-selection } { send-keys -X begin-selection }

@lmBored
Copy link

lmBored commented Jan 9, 2026

Just want to share my config, this will open and cd to the current directory of the active pane, silently:

# popuptmux

#!/bin/sh
set -e
width=${1:-63%}
height=${2:-75%}
SESSION_NAME="popup"
CURRENT_PANE="$(tmux display-message -p -F "#{session_name}")"
if echo "$CURRENT_PANE" | grep -q '^popup.*'; then
    tmux detach-client
else
    current_path=$(tmux display-message -p -F "#{pane_current_path}")
    if tmux has-session -t "$SESSION_NAME" 2>/dev/null; then
        tmux setenv -t "$SESSION_NAME" POPUP_PATH "$current_path"
        PANE_CMD=$(tmux display-message -t "$SESSION_NAME" -p -F "#{pane_current_command}")
        case "$PANE_CMD" in
            *sh)
                PANE_PID=$(tmux display-message -t "$SESSION_NAME" -p -F "#{pane_pid}")
                kill -USR1 "$PANE_PID" 2>/dev/null || true
                ;;
            *)
                # If running something else (vim), do nothing
                ;;
        esac
        
        tmux popup -d "$current_path" -xC -yC -w$width -h$height -E "tmux attach -t $SESSION_NAME"
    else
        tmux popup -d "$current_path" -xC -yC -w$width -h$height -E "tmux new -s $SESSION_NAME -c \"$current_path\""
    fi
fi
# .zshrc

_popup_auto_cd() {
    [[ -n "$TMUX" ]] || return
    local cmd_out
    cmd_out=$(tmux show-environment POPUP_PATH 2>/dev/null)
    [[ -n "$cmd_out" ]] || return

    local target_path="${cmd_out#POPUP_PATH=}"
    if [[ -d "$target_path" && "$target_path" != "$PWD" ]]; then
        builtin cd "$target_path"
        if [[ -o zle ]]; then
            zle reset-prompt
        fi
    fi
    tmux set-environment -u POPUP_PATH
}

TRAPUSR1() {
    _popup_auto_cd
}

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