tmux

tmux capture-pane fails to capture pane output


I tried using tmux capture-pane -S -10 -E -1 to capture the last 10 lines of output, but it alway output first line.

more infomation:

tmux version `tmux 3.1c`
system:
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
  1. create Window
root@zero:~# tmux new -d -s test
root@zero:~# tmux ls
test: 1 windows (created Sun Jun 15 17:54:22 2025)
  1. send command
root@zero:~# tmux send-keys -t test:0.0 "echo '1'" Enter
root@zero:~# tmux send-keys -t test:0.0 "echo '2'" Enter
root@zero:~# tmux send-keys -t test:0.0 "echo '3'" Enter
  1. capture-pane
root@zero:~# tmux capture-pane -p -t test:0.0 -S -10 -E -1
root@zero:~# echo '1'
root@zero:~# tmux capture-pane -p -t test:0.0 -S -10 -E -1
root@zero:~# echo '1'
root@zero:~# tmux capture-pane -p -t test:0.0 -S -10 -E -2
root@zero:~# echo '1'
root@zero:~# tmux capture-pane -p -t test:0.0 -S -4 -E -2
root@zero:~# echo '1'
root@zero:~# tmux capture-pane -p -t test:0.0 
root@zero:~# echo '1'
1
root@zero:~# echo '2'
2
root@zero:~# echo '3'
3
root@zero:~#

Solution

  • -S -10 -E -1 would not work since it's capturing the most recent 10 lines in history.

    According to man tmux:

    -S and -E specify the starting and ending line numbers, zero is the first line of the visible pane and negative numbers are lines in the history. '-' to -S is the start of the history and to -E the end of the visible pane. The default is to capture only the visible contents of the pane.

    So there's no direct way to specify "last 10 lines of output".

    If all your panes are of the same size you can work it around by using $LINES:

    tmux capture-pane -p -t test:0.0 -S $((LINES - 10)) -E -
    

    Note that this works only if the most recent output is at the bottom most of the pane, otherwise the result would include blank lines.