482

I started a tmux session on a smaller terminal. When I "attach" to the same session on a larger resolution monitor, it draws dots around the console. It doesn't fit the new window size. Is there any way to redraw and clean the window? CTRL+L or CTRL-B + R doesn't help.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Nobu
  • 9,965
  • 4
  • 40
  • 47

11 Answers11

689

tmux limits the dimensions of a window to the smallest of each dimension across all the sessions to which the window is attached. If it did not do this there would be no sensible way to display the whole window area for all the attached clients.

The easiest thing to do is to detach any other clients from the sessions when you attach:

tmux attach -d

Alternately, you can move any other clients to a different session before attaching to the session:

takeover() {
    # create a temporary session that displays the "how to go back" message
    tmp='takeover temp session'
    if ! tmux has-session -t "$tmp"; then
        tmux new-session -d -s "$tmp"
        tmux set-option -t "$tmp" set-remain-on-exit on
        tmux new-window -kt "$tmp":0 \
            'echo "Use Prefix + L (i.e. ^B L) to return to session."'
    fi

    # switch any clients attached to the target session to the temp session
    session="$1"
    for client in $(tmux list-clients -t "$session" | cut -f 1 -d :); do
        tmux switch-client -c "$client" -t "$tmp"
    done

    # attach to the target session
    tmux attach -t "$session"
}
takeover 'original session' # or the session number if you do not name sessions

The screen will shrink again if a smaller client switches to the session.

There is also a variation where you only "take over" the window (link the window into a new session, set aggressive-resize, and switch any other sessions that have that window active to some other window), but it is harder to script in the general case (and different to “exit” since you would want to unlink the window or kill the session instead of just detaching from the session).

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Chris Johnsen
  • 214,407
  • 26
  • 209
  • 186
  • Note that you can get the current session with `$(tmux display-message -p '#S')`, see: http://superuser.com/questions/410017/how-do-i-know-current-tmux-session-name-by-running-tmux-command. – GManNickG Apr 11 '14 at 02:24
  • 3
    This answer gets props for detail and knowledge base. But see Shi B.'s answer `Ctrl-b + D` for ease of use (and remembering). – fbicknel Sep 08 '17 at 15:37
  • "tmux detach -a" worked for me; "tmux attach -d" didn't. – mahemoff Sep 29 '17 at 11:00
  • (1) newer tmux errors on `set-option... set-remain-on-exit on`. See [tmux #787](https://github.com/tmux/tmux/issues/787). Now, use: `tmux set-hook -t "$tmp" window-linked 'set remain-on-exit on`. – user1902689 Oct 28 '18 at 08:06
  • 1
    (2) on newer tmux, have to use `new-window` last argument of something like `'echo "Use Prefix + L (i.e. ^B L) to return to session.'; while(true); do read; done"` or you won't see the message. (In some circumstances, you'll see it if you scroll up, or if terminal is taller.) If you use this, you're probably better off without my comment "(1)" or anything setting `remain-on-exit`. – user1902689 Oct 28 '18 at 08:09
  • How do you select which client after C-b D. I don't see any documentation what to do after that. People just say it lets you select which client to detach. Not seeing anything documented on what to do after you type the D. – Brian Wiley Nov 26 '20 at 09:48
553

You can always press CTRL-B + SHIFT-D to choose which client you want to detach from the session.

tmux will list all sessions with their current dimension. Then you simply detach from all the smaller sized sessions.

Bruno Bronosky
  • 66,273
  • 12
  • 162
  • 149
Shi B.
  • 5,653
  • 1
  • 12
  • 7
  • 8
    Clarification. It is capital D. Small 'd' detaches the client. – Raghuram Murthy P Dec 08 '17 at 18:38
  • 4
    `CTRL-B` if that's your prefix. Mine has been [`CTRL-A`](https://github.com/blaskovicz/dotfiles/blob/master/tmux.conf#L23) since I switched from screen. – Blaskovicz Jan 25 '18 at 16:17
  • 1
    Actually you need to detach from all the smaller sized sessions from the same session group. However CTRL-B + SHIFT-D doesn't show each session belongs to which group. I'd prefer to run `tmux list-sessions` which shows session name, size and group info, then `tmux kill-session -t ` to kill sessions of the same group. – Zhichang Yu Aug 09 '19 at 12:43
201

A simpler solution on recent versions of tmux (tested on 1.9) you can now do :

tmux detach -a

-a is for all other client on this session except the current one

You can alias it in your .[bash|zsh]rc

alias takeover="tmux detach -a"

Workflow: You can connect to your session normally, and if you are bothered by another session that forced down your tmux window size you can simply call takeover.

gbin
  • 2,960
  • 1
  • 14
  • 11
  • 15
    Thanks much! worked. If you are doing it in an active tmux session, just press CTRL+B (or whichever your custom tmux command) then `:detach -a` – Iacchus Mar 11 '16 at 05:47
  • Or just execute `tmux detach -a` on the shell command line inside Tmux. – jdhao Oct 22 '18 at 13:59
61

This is still the top post when searching, but it's no longer valid. Best answer is here, but the TLDR is

<c-b>:resize-window -A

datakid
  • 2,293
  • 5
  • 23
  • 35
24

You can use <Ctrl-B> : + at -d <CR> to redraw the tmux window.

Patrick J. Holt
  • 516
  • 4
  • 7
8

The other answers did not help me as I only had client attached (the previous one that started the session was already detached).

To fix it I followed the answer here (I was not using xterm).

Which simply said:

  1. Detach from tmux session
  2. Run resize linux command
  3. Reattach to tmux session
pev.hall
  • 467
  • 4
  • 8
  • This, combined with @datakid 's https://stackoverflow.com/a/61764869/9238801 `tmux resize-window -A` answer (after reattaching) worked for me for running interactive compute nodes over a few layers of ssh – Matthew Strasiotto Nov 17 '20 at 13:56
3

I just ran into this problem and stumbled across a different situation. Although it's probably just a unicorn, I thought I'd lay it out.

I had one session that was smaller, and I noticed that the font sizes were different: the smaller session had the smaller fonts. Apparently, I had changed window font sizes for some reason.

So in OS X, I just did Cmd-+ on the smaller sized session, and it snapped back into place.

JESii
  • 4,678
  • 2
  • 39
  • 44
  • This fixed weird placement of tmux status bar in macOS terminal. When I have terminal tabs open, it messes up the tmux status bar position. Playing with zoom via `cmd-+` or `-` fixes it. – Paul Dec 14 '18 at 00:36
2

Probably an strange edge case but for me the only thing that fixed it was unmaximizing the window and then maximizing it again.

Danny
  • 3,077
  • 2
  • 23
  • 26
0
ps ax | grep tmux
17685 pts/22   S+     0:00 tmux a -t 13g2
17920 pts/11   S+     0:00 tmux a -t 13g2
18065 pts/19   S+     0:00 grep tmux

kill the other one.

Tom
  • 15,798
  • 4
  • 37
  • 48
  • 9
    Seems like a valid answer to the title question, despite there being better answers. I don't understand why it got downvoted to oblivion. – JoL Jan 03 '17 at 20:11
0

I had the same problem because of using iTerm's tmux integration (i.e., tmux -CC a). None of the detach options mentioned in the other answers worked for me, because there was no "other sessions" to detach from.

My understanding is iTerm's tmux client seems to hard set the window size on the attached session, so the subsequent attaches seem to respect the previously resized window size. Alas, I ended up reattaching iTerm client to tmux via tmux -CC a and manually resized to full window size in GUI (not happy using mouse here, but that is what worked in the end, unfortunately). Clean detach from iTerm and subsequent attaches follows the size set in iTerm.

Thamme Gowda
  • 11,249
  • 5
  • 50
  • 57
  • 1
    This comment contains a recipe to fix it (`:set-option -u window-size`): https://stackoverflow.com/questions/7814612/is-there-any-way-to-redraw-tmux-window-when-switching-smaller-monitor-to-bigger#comment126480767_61764869 – Алексей Киричун Sep 15 '22 at 11:38
-1

I use Ctrl-b + q which makes it flash number for each pane, redrawing them on the way.

auselen
  • 27,577
  • 7
  • 73
  • 114