# libtmux.Session.new_window

- **Module:** libtmux
- **Package:** libtmux
- **Language:** Python
- **Kind:** method
- **Source:** https://github.com/tmux-python/libtmux/blob/38e368c11117fb4aeb2f082d552cd4f210eae06a/src/libtmux/session.py#L779
- **Page:** https://libtmux.org/en/py/stable/reference/libtmux-session-new_window/

tmux command: [`new-window`](/en/tmux/latest/manual/new-window/)

```
libtmux.Session.new_window(
    self,
    window_name: str | None = None,
    start_directory: StrPath | None = None,
    attach: bool = False,
    window_index: str = "",
    window_shell: str | None = None,
    environment: dict[str, str] | None = None,
    direction: WindowDirection | None = None,
    target_window: str | None = None,
    kill_existing: bool | None = None,
    select_existing: bool | None = None,
) -> Window
```

Create new window, returns new :class:`Window`.

By default, this will make the window active. For the new window
to be created and not set to current, pass in ``attach=False``.

## Examples

```python
>>> window_initial = session.new_window(window_name='Example')
>>> window_initial
Window(@... 2:Example, Session($1 libtmux_...))
>>> window_initial.window_index
'2'
```

```python
>>> window_before = session.new_window(
... window_name='Window before', direction=WindowDirection.Before)
>>> window_initial.refresh()
>>> window_before
Window(@... 1:Window before, Session($1 libtmux_...))
>>> window_initial
Window(@... 3:Example, Session($1 libtmux_...))
```

```python
>>> window_after = session.new_window(
... window_name='Window after', direction=WindowDirection.After)
>>> window_initial.refresh()
>>> window_after.refresh()
>>> window_after
Window(@... 3:Window after, Session($1 libtmux_...))
>>> window_initial
Window(@... 4:Example, Session($1 libtmux_...))
>>> window_before
Window(@... 1:Window before, Session($1 libtmux_...))
```

## Parameters

- `window_name` (`str | None`)
- `start_directory` (`StrPath | None`): working directory in which the new window is created.
- `attach` (`bool`): make new window the current window after creating it, default True.
- `window_index` (`str`): create the new window at the given index position. Default is empty
string which will create the window in the next available position.
- `window_shell` (`str | None`): execute a command on starting the window.  The window will close
when the command exits.
When this command exits the window will close.  This feature is
useful for long-running processes where the closing of the
window upon completion is desired. Note:
- `environment` (`dict[str, str] | None`)
- `direction` (`WindowDirection | None`): Insert window before or after target window.
- `target_window` (`str | None`): Used by :meth:`Window.new_window` to specify the target window.
- `kill_existing` (`bool | None`): Destroy the window at the target index if it already exists
(``-k`` flag). (added 0.56)
- `select_existing` (`bool | None`): If a window with the given name already exists, select it instead
of creating a new one (``-S`` flag). (added 0.56)

## Returns

:class:`Window`
    The newly created window.

## In other ports

### Create a window

- Ruby: [`LibTmux::Session#new_window`](/en/ruby/latest/reference/libtmux-session-new_window/) (method)
- Lua: [`libtmux.Session:new_window`](/en/lua/latest/reference/libtmux-session-new_window/) (method)
- TypeScript: [`session.Session.newWindow`](/en/ts/latest/reference/session-session-newwindow/) (method)
- Rust: [`session.Session.new_window`](/en/rs/latest/reference/session-session-new_window/) (method)
- Go: [`tmux.Session.NewWindow`](/en/go/latest/reference/tmux-session-newwindow/) (method)
- Java: [`io.github.libtmux.Session.Session.newWindow`](/en/java/latest/reference/io-github-libtmux-session-session-newwindow/) (method)
- C#: [`LibTmux.Session.CreateWindowAsync`](/en/csharp/latest/reference/libtmux-session-createwindowasync/) (method)
- C++: [`libtmux::Session::new_window`](/en/cxx/latest/reference/libtmux-session-new_window/) (method)
- Swift: [`Server.newWindow(in:named:startDirectory:)`](/en/swift/latest/reference/server-newwindow(in-named-startdirectory-)/) (method)
- Kotlin: [`io.github.libtmux.kotlin.Session.newWindow`](/en/kotlin/latest/reference/io-github-libtmux-kotlin-session-newwindow/) (method)
- Scala: [`io.github.libtmux.scaladsl.Session.newWindow`](/en/scala/latest/reference/io-github-libtmux-scaladsl-session-newwindow/) (method)
