brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: tmux run-shell -b runs a shell command from a key without making you wait

Tmux
~/tips/tip-tmux-run-shell-background

bind S run-shell -b '~/bin/sync-notes.sh'

Starts a shell command from a tmux key and lets you keep working while it runs.

run-shell is how a tmux key starts a shell command. Add -b and tmux starts the command in the background, so the key returns at once and you can keep typing while it works.

Without -b, tmux waits for the command to finish before it runs the next command in its queue. For a script that takes a second you won’t notice. For a sync or a backup that takes a minute, tmux feels stuck, and I find that more annoying than a slow script.

bind S run-shell -b '~/bin/sync-notes.sh'

Put that line in ~/.tmux.conf, reload, and Ctrl-b S starts the script. Replace the path with a script of your own. When the command prints something, tmux shows the output in view mode in the current pane once the command is done, and q closes that view. If the command fails, tmux also shows the exit status, for example 'exit 3' returned 3.

Two more flags are handy in a binding. -d 5 waits five seconds before it starts the command, and -c sets the folder the command runs in:

bind G run-shell -b -c /srv/site 'git fetch --quiet'

You can try it from a normal shell too: tmux run-shell -b 'sleep 5; echo done' returns straight away, and the same line without -b sits there for five seconds.

The command runs with /bin/sh, not with your login shell. Aliases and functions from your .zshrc or .bashrc are not there, so call a script or use full commands. And for a chatty command I’d send the output to a log file, or the view mode pops up with a wall of text every time.

Further reading: tmux manual page (OpenBSD), tmux wiki: Getting Started

More Tmux tips set -s extended-keys on set -s history-file ~/.tmux_history tmux list-keys -T prefix ] set -g prefix2 C-a set -g status-justify centre All Tmux tips (133) All topics

Link