new-session
Short form: new
Create a new session and attach the current terminal to it, unless -d is given. With -t, the new session joins a session group and shares its windows, while keeping its own current window.
How new-session works
new-session starts a new tmux session: a named workspace with its own windows that keeps running after you disconnect. Run it from a normal shell and tmux creates the session and puts your terminal inside it straight away.
Most people only need two flags. -s gives the session a name you can attach to later, and -d creates it in the background without attaching, which is what you want in setup scripts. -A makes the command safe to run again and again: it attaches if the session already exists and creates it if not, so tmux new -A -s main works well as a shell alias.
With -t, the new session joins a session group. Sessions in a group share the same windows, but each one keeps its own current window. That lets two terminals look at different windows of the same project at the same time, which a single shared session cannot do.
Synopsis
tmux new-session [-AdDEPX] [-c start-directory] [-e environment] [-F format] [-f flags] [-n window-name] [-s session-name] [-t target-session] [-x width] [-y height] [shell-command [argument ...]]Options
| Flag | Description |
|---|---|
| -A | Attach to existing session if it exists, otherwise create new |
| -d | Don't attach to the new session |
| -D | Detach any other clients attached to the session |
| -s session-name | Specify the session name |
| -n window-name | Name the initial window |
| -c start-directory | Specify starting directory |
| -t group-name | Group with an existing session |
| -x width | Set window width |
| -y height | Set window height |
Examples
Create a new session with default name
tmux new-sessionCreate a session named 'dev'
tmux new -s devCreate session 'work' with window named 'editor'
tmux new -s work -n editorCreate session starting in specific directory
tmux new -s project -c ~/code/myprojectAttach to 'main' if it exists, otherwise create it
tmux new -A -s mainCreate a detached session (runs in background)
tmux new -d -s backgroundStart a background session called api in your project folder. Its first window is named server and runs the dev server.
tmux new -d -s api -c ~/code/api -n server 'npm run dev'Create a second session that shares the windows of api (a session group), so another terminal can watch a different window of the same project.
tmux new -s mirror -t apiDefault Keybinding
new-session has no default key. Run it from a shell as tmux new-session, type it at the command prompt (Ctrl+b then :), or give it a key with bind-key.
Common Mistakes
- Typing tmux new in a shell that is already inside tmux fails with 'sessions should be nested with care, unset $TMUX to force'. Use tmux new -d -s name and then switch-client, or run new-session from the command prompt (Prefix + :), which creates the session and moves you to it.
- Skipping -s leaves you with sessions called 0, 1, 2 and so on, which are hard to tell apart later. Name sessions after the project or task.
- tmux 3.7a and later accept colons and periods in names, but those characters also separate session, window, and pane in targets. A name like api.v2 is awkward to target, so stick to letters, numbers, dashes, and underscores.
Pro Tips
- •Use -A flag to create or attach in one command - perfect for scripts
- •The -d flag is useful for creating sessions in startup scripts
- •Session names should be descriptive and short for easy switching