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 -As work attaches to a session or creates it, one command for both

Tmux
~/tips/tip-tmux-new-as

tmux new -As work

Attaches to the tmux session you name, or creates it when it does not exist yet.

Most people type tmux new once and tmux attach every time after, and then forget which one they need. One command covers both.

tmux new -As work

The -A flag turns new-session into attach-or-create, and -s work names the session. Run it after every SSH login and you always land in the same place.

The reason I like this one is that it removes a decision. When you log in to a server you do not have to remember whether you left a session running yesterday. The command either picks it up or starts a fresh one with the same name, and the next login does the same thing.

If you want it automatic, you can put the line in a shell alias such as alias t='tmux new -As work'. Pick one session name per machine and keep it, because a stable name is what makes the habit work. A second session for a different project is one more name, nothing more.

Run it from a normal shell. Inside tmux you get a nested session instead, and tmux warns you about that.

Try it safely: start a throwaway session with tmux new -s scratch, run it there, and close the session again with tmux kill-session -t scratch. Nothing else is touched.

Further reading: tmux manual page (OpenBSD, official), tmux wiki: Getting Started (official), On this site: tmux new -As work attaches to a session or creates it, On this site: tmux new -d -s job 'command' runs a command in the background that survives logout

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