# libtmux.Format

- **Module:** libtmux
- **Package:** libtmux
- **Language:** Python
- **Kind:** class
- **Source:** https://github.com/tmux-python/libtmux/blob/d2a146c9a045437b92013d65943b7ac70013fa24/src/libtmux/formats.py#L217
- **Page:** https://libtmux.org/en/py/latest/reference/libtmux-format/

A tmux format that tmux expands, instead of storing it as typed.

Names, titles, start directories and file paths passed to libtmux as plain
``str`` are kept as typed. Wrap a format your program wrote in
:class:`Format` to have tmux expand it, as it does for ``#{...}`` in
``tmux.conf``. Where tmux always expands an argument, such as
:meth:`~libtmux.Pane.display_message`, a :class:`Format` is accepted too,
so text from users or files can be combined with it safely.

Only pass text your program wrote. Add text from users or files with ``+``
or :meth:`join`, which keep it literal; place it inside an expression,
such as a conditional or comparison, with :meth:`escape`. Never place it
inside ``#(...)``, which runs a shell command.

## Examples

Open a pane in the directory of the session's active pane:

```python
>>> pane.split(start_directory=Format("#{pane_current_path}"))
Pane(...)
```

Name a window after its session, with user text kept literal:

```python
>>> user_text = "x#S"
>>> window.rename_window(Format("#{session_name}-") + user_text)
Window(...)
>>> window.window_name == f"{session.session_name}-x#S"
True
```

A plain ``str`` is never expanded:

```python
>>> window.rename_window("#{session_name}")
Window(...)
>>> window.window_name
'#{session_name}'
```

Plain text cannot be added where tmux would read it as format syntax:

```python
>>> Format("#{?pane_active,") + "yes"
Traceback (most recent call last):
...
ValueError: cannot add plain text to Format('#{?pane_active,'): ...
```

## Members

### Common operations

- [`escape`](/en/py/latest/reference/libtmux-format-escape/) (method): Return a format that tmux expands back to *text*.
- [`join`](/en/py/latest/reference/libtmux-format-join/) (method): Join *parts* with this format between them, escaping each ``str``.

### Other members

- [`template`](/en/py/latest/reference/libtmux-format-template/) (attribute)
- [`__add__`](/en/py/latest/reference/libtmux-format-__add__/) (method): Append *other*; a ``str`` is escaped and stays literal.
- [`__bool__`](/en/py/latest/reference/libtmux-format-__bool__/) (method): Return whether the format is non-empty, as for ``str``.
- [`__radd__`](/en/py/latest/reference/libtmux-format-__radd__/) (method): Prepend *other*; a ``str`` is escaped and stays literal.
- [`__repr__`](/en/py/latest/reference/libtmux-format-__repr__/) (method): Show the format as it would be written.
- [`__str__`](/en/py/latest/reference/libtmux-format-__str__/) (method): Return the format as tmux receives it.
