Cannot resize tmux pane from bash script
Solution 1:
Per the documentation, when you create a detached session (new-session -d
), it defaults to a size of 80×24. If you attach with a terminal window that is actually 24 lines high (or 25, since tmux uses one for a status line), then you should find that the below-Vim pane does in fact end up with just five lines.
The problem comes when you attach to the session with a terminal window that is much taller than 24 lines. When you do this, tmux resizes the panes to fill the full terminal window. The lower pane grows past its original five lines when this happens.
One way to work around this problem is to create the detached session with an initial size that matches that of the terminal window from which you will eventually attach to the session. One semi-portable way to do this is to parse the output of stty size
(some shells also provide LINES and COLUMNS parameters (especially when in interactive mode), but these parameters are not always available and reliable in shell scripts).
set -- $(stty size) # $1 = rows $2 = columns
tmux -2 new-session -d -s "$SESSION" -x "$2" -y "$(($1 - 1))" # status line uses a row
The failed to connect to server: Connection refused
message comes from your tmux has-session
command. It is reporting that it there is no existing server. Since you are only interested in the exit code, you can probably just send the output to /dev/null
to avoid seeing it at all. You can also put the command directly into the if
statement:
if tmux has-session -t "$SESSION" 2>/dev/null; then
⋮
fi
Incidentally, you should almost always put your parameter expansions in double quotes (to avoid word splitting and glob expansion). You only have the one parameter and its value (copied from USER
) is (usually) probably safe not to quote, but it is a good habit to always quote your expansions in almost all contexts.