# libtmux.Server.new_session

- **Module:** libtmux.Server
- **Package:** libtmux
- **Language:** Python
- **Kind:** method
- **Source:** https://github.com/tmux-python/libtmux/blob/036c521c4b83ce6e434eb50afe2a0d08a6a05e46/src/libtmux/server.py#L2200
- **Page:** https://libtmux.org/reference/py/libtmux-server-new_session/

```
libtmux.Server.new_session(self, session_name: str | None = None, kill_session: bool = False, attach: bool = False, start_directory: StrPath | None = None, window_name: str | None = None, window_command: str | None = None, x: int | DashLiteral | None = None, y: int | DashLiteral | None = None, environment: dict[str, str] | None = None, args: t.Any, detach_others: bool | None = None, no_size: bool | None = None, client_flags: str | None = None, kwargs: t.Any) -> Session
```

Create new session, returns new :class:`Session`.

Uses ``-P`` flag to print session info, ``-F`` for return formatting
returns new Session object.

``$ tmux new-session -d`` will create the session in the background
``$ tmux new-session -Ad`` will move to the session name if it already
exists. todo: make an option to handle this.

## Parameters

- `self`
- `session_name` (str | None) — ::
$ tmux new-session -s <session_name>
- `kill_session` (bool)
- `attach` (bool) — create session in the foreground. ``attach=False`` is equivalent
to::
$ tmux new-session -d
- `start_directory` (StrPath | None)
- `window_name` (str | None)
- `window_command` (str | None)
- `x` (int | DashLiteral | None)
- `y` (int | DashLiteral | None)
- `environment` (dict[str, str] | None)
- `args` (t.Any)
- `detach_others` (bool | None)
- `no_size` (bool | None)
- `client_flags` (str | None)
- `kwargs` (t.Any)

## Returns

:class:`Session`

## Raises

- `exc.BadSessionName`

## Example

Sessions can be created without a session name (0.14.2+):

```python
>>> server.new_session()
Session($2 2)
```

## Example

Creating them in succession will enumerate IDs (via tmux):

```python
>>> server.new_session()
Session($3 3)
```

## Example

With a `session_name`:

```python
>>> server.new_session(session_name='my session')
Session($4 my session)
```
