How do I disconnect all other users in tmux? - tmux

I've got a tmux session where the window is too small because some user somewhere is connected.
How do I tell tmux to disconnect all connected users?

You can use <prefix> D (where prefix is C-b by default), to chose which clients to detach; it will also list they col/lines as well as the last used time. Note the uppercase D, i.e. Shift+d.
You could also use tmux's detach-client option
detach-client [-P] [-a] [-s target-session] [-t target-client]
(alias: detach)
Detach the current client if bound to a key, the client specified
with -t, or all clients currently attached to the session speci-
fied by -s. The -a option kills all but the client given with
-t. If -P is given, send SIGHUP to the parent process of the
client, typically causing it to exit.
either from <prefix>:followed by detach [options] or on the command line inside tmux with tmux detach [options]

tmux a -dt <session-name>
a=attach
d=detach other clients (so only you can attach to this session)
t=target

I'll break out #PEdroArthur's great comment into a separate answer since it directly answers the most common use case: "being within a tmux session and wanting to disconnect all other sessions"
Run this from within your tmux session:
tmux detach-client -a

Related

Can "tmux load-buffer" specify a session?

The tmux paste-buffer command can specify a session using the -t option:
tmux paste-buffer -b buffer-name -t session-name:window
But tmux load-buffer has no -t option:
tmux load-buffer [-b buffer-name] path
If you pass something like session-name:buffer-name to the -b option, it simply treats "session-name:buffer-name" as the buffer name.
I want to load a buffer in a tmux session from outside the session, and if there's more than one session running I want to specify which one.
Is there a (clean) way to do this?
(I suppose that setting the $TMUX environment variable would be a solution, but (a) it's not exactly clean, and (b) I haven't figured out how to get the appropriate value for the desired session.)
Buffers are global and do not belong to a particular session so there is no way to specify a session to load-buffer. You can name the buffers whatever you want so you could use a form that includes the session name.

Is it possible to quit tmux, while "keeping" the window in the terminal

Use case
I run command over a bastion in SSH and since the connection can be dropped we are using tmux or screen.
For short commands I'm using send-keys, let's say I'm using a command like kubectl get pods. I'd like to keep the output of this command but terminate the tmux session.
Using remain-on-exit is not what I'm looking for as it keeps a dead window.
I've seen those question already
https://unix.stackexchange.com/questions/17116/prevent-pane-window-from-closing-when-command-completes-tmux
TMUX: how to make new window stay when start shell-command quits?
I'd like to keep the output of this command but terminate the tmux session.
I use this in my ~/.tmux.conf, and now when I exit my running shell, pane output is saved to unique log file:
set -g remain-on-exit
set-hook pane-died 'capture-pane -S - -E - ; save-buffer "$HOME/logs/tmux/tmux-saved.#{host_short}-#{session_id}:#{window_id}:#{pane_id}-#{pane_pid}-#{client_activity}.log"; delete-buffer; kill-pane'
I'm not sure it's exactly what you are looking for, but you could use the capture-pane command to save the contents of a pane (subject to what is still in the pane's history) to a file.

Tmux command to run shell command on active pane?

I understand that run-shell command runs the code in a "global" session which isn't necessarily linked to any active window, pane or session. However I would like to bind a key to run a command in the shell for the active pane.
Use case: map a key to opening vim in the current active pane (perhaps with certain parameters). Just like you would run run-shell "command command-parameters*".
Is there a way to do this in tmux?
Sending commands to the current pane
If you run tmux send-keys vim Enter, that will send the literal keypresses to your current pane to spawn vim. You can then bind this to whatever key combination you like.
Caveat: Because this is sending literal key presses, the pane needs a shell running already to interpret what is sent (e.g. bash / zsh / fish).
Targeting a specific pane
You can target specific destinations with the -t flag. For example, to open vim in the current session, window 5, pane 2:
tmux send-keys -t ":5.1" vim Enter
The syntax of a target is quite flexible (see the COMMANDS section of the manpage, for target-session and target-window), and this is just one example:
session-name:window-id.pane-id
When session-name is empty (as in the above example), the current session is used. The 5 identifies which window you're referring to. The .1 specifies the second pane in window #5 (counting up from 0).
$TMUX_PANE
Each pane in tmux has its own unique ID (and it is unique across all sessions). It's exposed via the $TMUX_PANE environment variable. This can also be used as a target for various tmux commands, e.g. tmux rename-window -t $TMUX_PANE $new_name.

How to copy a tmux session from one machine to another?

Is it possible to export/import tmux sessions?
At the university we have identical machines (same binaries and file structure) and sometimes I want to move a tmux session from one machine to another. Is there a way to do this?
As pointed on the comments, it is very unlikely that you would be able to restore all the state of tmux session.
If your purpose is to access the tmux session from a different machine (hostB), while the tmux session is still running on the original machine (hostA), you could simple access it through ssh:
hostB $ ssh hostA
hostA $ tmux attach
The link you mentioned contains a discussion about how to re-create a tmux session. If you are interested in starting a similar tmux session, you should try tmuxinator:
# ~/.tmuxinator/sample.yml
name: sample
root: ~/
windows:
- editor:
layout: main-vertical
panes:
- vim
- guard
- server: ssh serverX
- logs: tail -f log/development.log
By issuing mux start sample you will have a tmux session with three windows:
the first with two panes, the first one running vim
the second with a ssh to a given server
the third displaying the tail of a log file
Using this file you could start similar sessions on different machines.

How do you detach a remote screen session in byobu (tmux)?

I am currently in a byobu-tmux session and am ssh'ed into a screen session. How do I detach the remote screen session without detaching byobu-tmux session? Some things to note, I can't run byobu-config because I'm on osx and don't have python-newt (w/ snack) installed. And, I've run byobu-ctrl-a in Emacs mode, but that doesn't seem to allow me to ctrl-a d out of the remote screen session.
It is easy when you use tmux commands:
byobu-tmux detach
byobu-tmux
or even just:
byobu detach
byobu
You should be able to double-escape with Ctrl-a.
To send a detach message to the inner byobu-screen session, press:
Ctrl-a Ctrl-a d
Full disclosure: I am the author and maintainer of Byobu.
Try letting go of ctrl after the first a, so the sequence is ctrl-a, a, d. Man screen:
C-a C-a (other) Toggle to the window displayed
previously. Note that this
binding defaults to the command character typed twice, unless
overridden. For instance, if you use the option "-e]x", this
command becomes "]]".
C-a a (meta) Send the command character (C-a) to
window. See escape com‐
mand.
Or if you're using tmux instead of screen for Byobu, try just ctrl-a d. Byobu's default prefix key is ctrl-b, so if you're using that default, doubling up the ctrl-a keystroke would not be necessary.
Source: https://askubuntu.com/a/309215/106100
I was able to do this by listing all clients inside the current client:
$ byobu list-clients
/dev/pts/67: 1 [80x24 xterm] (utf8)
/dev/pts/70: 1 [157x48 xterm-256color] (utf8)
Then detach the remote client (determined based on screen size):
$ byobu detach -t /dev/pts/67
Now I can use my full window size
perhaps not relevant to tmux but for byobu, I found the following command to be very helpful: detach all sessions except the current one:
/usr/lib/byobu/include/tmux-detach-all-but-current-client
hope this helps
You need to switch the prefix of your local session if it conflicts with the remote session. For example, if both are using CTRL+A then you'd be in trouble. You can either send a raw command (there's a sequence for that, but I can't remember it), or go the easy route and remap your local session to Ctrl+B, then you can input Ctrl+A that will get routed to the remote session. Also not related to tmux but the ssh connection itself you can input "~." and it'll disconnect from the ssh session. Hope it helps.
I've been an avid user of byobu on Linux for the best part of a decade. After struggling with configuring the brew install of byobu on OSX for most of these years, I finally managed to setup my byobu configs in a round about way. First I executed this:
echo '/usr/local/lib/python2.7/site-packages' | sudo tee /Library/Python/2.7/site-packages/homebrew.pth
Then I ran the byobu config file:
byobu-config
Finally I cleaned up
sudo rm /Library/Python/2.7/site-packages/homebrew.pth
Python crashed along the way with a few pop-ups, however, byobu now works for me as it should. I do need to repeat these steps when I want to change config again though... still looking for a cleaner solution.

Resources