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 new-window -S -n logs jumps to a named window or creates it

Tmux
~/tips/tip-tmux-new-window-select-existing

tmux new-window -S -n logs 'tail -f /var/log/syslog'

Switches to a tmux window with the given name if it exists, and creates it if it does not.

I like one window for logs and one for the editor. What I do not like is ending up with three windows called logs because I pressed the shortcut again.

Normally new-window always creates a window. With -S and a name from -n, tmux first looks for a window with that name. The manual says that if one exists it is selected, and otherwise the window is created.

tmux new-window -S -n logs 'tail -f /var/log/syslog'

Run it once and you get a window named logs running that command. Run it again from another window and tmux switches back to the existing one, without a second tail.

That fits a key binding in ~/.tmux.conf:

bind L new-window -S -n logs 'tail -f /var/log/syslog'

After Ctrl-b L you land in the logs window, wherever you were. When -d is given as well, the manual says the command does nothing if the window already exists, which makes a quiet “start it if missing” line for a startup script.

The match is on the window name only. If you renamed the window, or an automatic rename changed it, tmux does not find it and creates a new one. When the command ends, the window closes unless remain-on-exit is set, so a one-off command is gone once it finishes.

Further reading: tmux manual

More Tmux tips tmux new -d -s report -x 200 -y 50 'top' tmux wait-for build-done set -g allow-passthrough on tmux kill-server Ctrl-b [ All Tmux tips (125) All topics

Link