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.
Best Answer
You can probably use a Vim
:!
command to runtmux send-keys
to send a Control-R† to your other pane. Sincesend-keys
can send keystrokes to any pane (not just the active one), you do not even need to switch active panes back and forth.Here it is a Vim mapping (you could put it in your
.vimrc
or just paste it into:
prompt to try it):This maps the
\rl
key sequence (<Leader>
defaults to\
, but it can be customized), to the following sequence of Vim commands (separated by<Bar>
; see:help map_bar
):We use
execute
here so that the next Vim command (redraw
) is not taken as part of the:!
shell command.We use the
silent
prefix command to avoid the “Press ENTER to continue” prompt.You can omit
slient
if you want the prompt or want to see the output of the:!
command (e.g. the tmux command is not working, and you want to see if it is giving an error message).This would normally happen after the “Press ENTER” prompt, but we are suppressing it with
silent
.† I do not have lynx at hand, but Control-R seems to be the reload key based on my search for “lynx reload” (i.e. “Reloading files and refreshing the display” of the user guide).
‡ Besides
bottom
, there are other ways of specifying the target pane (search for “target-pane” in the tmux man page):.+1
,.-1
: next, previous pane in this windowtop
,bottom
,left
,right
, and combinations oftop
/bottom
withleft
/right
(i.e.
bottom-left
)%42
(tmux 1.5+): a%
-prefixed pane number from the TMUX_PANE environment variable of the target paneThis last form might be handy if your Lynx pane is not always in the same tmux window as your instance of Vim. Before launching Lynx, save the value of TMUX_PANE into a temporary file, then read the file to form the target-pane argument: