# libtmux.Session.cmd

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

```
libtmux.Session.cmd(
    self,
    cmd: str,
    args: t.Any,
    target: str | int | None = None,
) -> tmux_cmd
```

Execute tmux subcommand within session context.

Automatically binds target by adding  ``-t`` for object's session ID to the
command. Pass ``target`` to keyword arguments to override.

## Examples

```python
>>> session.cmd('new-window', '-P').stdout[0]
'libtmux...:....0'
```

From raw output to an enriched `Window` object:

```python
>>> Window.from_window_id(window_id=session.cmd(
... 'new-window', '-P', '-F#{window_id}').stdout[0], server=session.server)
Window(@... ...:..., Session($1 libtmux_...))
```

## Changed

0.34 — Passing target by ``-t`` is ignored. Use ``target`` keyword argument instead.

0.8 — Renamed from ``.tmux`` to ``.cmd``.

## Parameters

- `cmd` (`str`)
- `args` (`t.Any`)
- `target` (`str | int | None`): Optional custom target override. By default, the target is the session ID.

## Returns

:meth:`server.cmd`

## In other ports

### Run a tmux command addressed at a session

- TypeScript: [`session.Session.cmd`](/en/ts/latest/reference/session-session-cmd/) (method)
- Rust: [`session.Session.cmd`](/en/rs/latest/reference/session-session-cmd/) (method)
- Go: [`tmux.Session.Cmd`](/en/go/latest/reference/tmux-session-cmd/) (method)
