How to start a named tmux session with a window name and a directory?

By Romain Dorgueil

Short answer

tmux new -d -s projet -n edit -c ~/code/projet

This creates a session named projet, names its first window edit, and starts that window in ~/code/projet. The -d flag keeps it detached, so the command returns immediately instead of dropping you into the session.

Details

Three flags do the work here:

  • -s projet sets the session name (instead of the default 0, 1, …).
  • -n edit names the first window. Without it, tmux names the window after the running program.
  • -c ~/code/projet sets the working directory for that first window’s pane.

-d (detached) is what makes this scriptable. The session is created in the background, so you can chain several of these calls to spin up a whole workspace from a shell script, then attach to it once at the end:

tmux attach -t projet

You can verify the result without attaching:

tmux display-message -t projet -p '#{window_name} #{pane_current_path}'
# -> edit /home/you/code/projet

Verified on tmux 3.2a and 3.6a.