# libtmux.Server

- **Module:** libtmux
- **Package:** libtmux
- **Language:** Python
- **Kind:** class
- **Source:** https://github.com/tmux-python/libtmux/blob/9fdd083a8181a827889337b63cd4e6daea661a8c/src/libtmux/server.py#L85
- **Page:** https://libtmux.org/en/py/latest/reference/libtmux-server/

:term:`tmux(1)` :term:`Server` [server_manual]_.

- :attr:`Server.sessions` [:class:`Session`, ...]

  - :attr:`Session.windows` [:class:`Window`, ...]

    - :attr:`Window.panes` [:class:`Pane`, ...]

      - :class:`Pane`

When instantiated stores information on live, running tmux server.

## Example

```python
>>> server
Server(socket_name=libtmux_test...)
```

## Example

```python
>>> server.sessions
[Session($1 ...)]
```

## Example

```python
>>> server.sessions[0].windows
[Window(@1 1:..., Session($1 ...))]
```

## Example

```python
>>> server.sessions[0].active_window
Window(@1 1:..., Session($1 ...))
```

## Example

```python
>>> server.sessions[0].active_pane
Pane(%1 Window(@1 1:..., Session($1 ...)))
```

## Example

The server can be used as a context manager to ensure proper cleanup:

```python
>>> with Server() as server:
...     session = server.new_session()
...     # Do work with the session
...     # Server will be killed automatically when exiting the context
```

## Members

- `socket_name` (attribute): Passthrough to ``[-L socket-name]``
- `socket_path` (attribute): Passthrough to ``[-S socket-path]``
- `config_file` (attribute): Passthrough to ``[-f file]``
- `colors` (attribute): ``256`` or ``88``
- `child_id_attribute` (attribute): Unique child ID used by :class:`~libtmux.common.TmuxRelationalObject`
- `formatter_prefix` (attribute): Namespace used for :class:`~libtmux.common.TmuxMappingObject`
- `default_option_scope` (attribute): For option management.
- `default_hook_scope` (attribute): For hook management.
- `tmux_bin` (attribute): Custom path to tmux binary. Falls back to ``shutil.which("tmux")``.
- `__init__` (method)
- `from_env` (method): Return the tmux server this process's pane is attached to.
- `__enter__` (method): Enter the context, returning self.
- `__exit__` (method): Exit the context, killing the server if it exists.
- `is_alive` (method): Return True if tmux server alive.
- `raise_if_dead` (method): Raise if server not connected.
- `cmd` (method): Execute tmux command respective of socket name and file, return output.
- `attached_sessions` (property): Return active :class:`Session` instances.
- `has_session` (method): Return True if session exists.
- `kill` (method): Kill tmux server.
- `kill_session` (method): Kill tmux session.
- `run_shell` (method): Execute a shell command via ``$ tmux run-shell``.
- `wait_for` (method): Wait for, signal, or lock a channel via ``$ tmux wait-for``.
- `bind_key` (method): Bind a key to a command via ``$ tmux bind-key``.
- `unbind_key` (method): Unbind a key via ``$ tmux unbind-key``.
- `list_keys` (method): List key bindings via ``$ tmux list-keys``.
- `list_commands` (method): List tmux commands via ``$ tmux list-commands``.
- `lock_server` (method): Lock the tmux server via ``$ tmux lock-server``.
- `server_access` (method): Manage server access control via ``$ tmux server-access``.
- `refresh_client` (method): Refresh a client's display via ``$ tmux refresh-client``.
- `suspend_client` (method): Suspend a client via ``$ tmux suspend-client``.
- `lock_client` (method): Lock a client via ``$ tmux lock-client``.
- `detach_client` (method): Detach a specific client via ``$ tmux detach-client``.
- `detach_all_clients` (method): Detach every client on this server via ``$ tmux detach-client -a``.
- `confirm_before` (method): Run a command after confirmation via ``$ tmux confirm-before``.
- `command_prompt` (method): Open a command prompt via ``$ tmux command-prompt``.
- `display_menu` (method): Display a popup menu via ``$ tmux display-menu``.
- `start_server` (method): Start the tmux server via ``$ tmux start-server``.
- `show_messages` (method): Show server message log via ``$ tmux show-messages``.
- `display_message` (method): Display message at server scope via ``$ tmux display-message``.
- `show_prompt_history` (method): Show prompt history via ``$ tmux show-prompt-history``.
- `clear_prompt_history` (method): Clear prompt history via ``$ tmux clear-prompt-history``.
- `set_buffer` (method): Set a paste buffer via ``$ tmux set-buffer``.
- `show_buffer` (method): Show content of a paste buffer via ``$ tmux show-buffer``.
- `delete_buffer` (method): Delete a paste buffer via ``$ tmux delete-buffer``.
- `save_buffer` (method): Save a paste buffer to a file via ``$ tmux save-buffer``.
- `load_buffer` (method): Load a file into a paste buffer via ``$ tmux load-buffer``.
- `list_buffers` (method): List paste buffers via ``$ tmux list-buffers``.
- `if_shell` (method): Execute a tmux command conditionally via ``$ tmux if-shell``.
- `source_file` (method): Source a tmux configuration file via ``$ tmux source-file``.
- `list_clients` (method): List connected clients via ``$ tmux list-clients``.
- `switch_client` (method): Switch tmux client.
- `attach_session` (method): Attach tmux session.
- `new_session` (method): Create new session, returns new :class:`Session`.
- `sessions` (property): Sessions contained in server.
- `windows` (property): Windows contained in server's sessions.
- `panes` (property): Panes contained in tmux server (across all windows in all sessions).
- `clients` (property): Clients attached to this tmux server.
- `search_sessions` (method): Sessions, optionally filtered by tmux before rows are returned.
- `search_windows` (method): All windows across sessions, optionally filtered by tmux.
- `search_panes` (method): All panes across the server, optionally filtered by tmux.
- `__eq__` (method): Equal operator for :class:`Server` object.
- `__repr__` (method): Representation of :class:`Server` object.
- `kill_server` (method): Kill tmux server.
- `_list_panes` (method): Return list of panes in :py:obj:`dict` form.
- `_update_panes` (method): Update internal pane data and return ``self`` for chainability.
- `get_by_id` (method): Return session by id. Deprecated in favor of :meth:`.sessions.get()`.
- `where` (method): Filter through sessions, return list of :class:`Session`.
- `find_where` (method): Filter through sessions, return first :class:`Session`.
- `_list_windows` (method): Return list of windows in :py:obj:`dict` form.
- `_update_windows` (method): Update internal window data and return ``self`` for chainability.
- `_sessions` (property): Property / alias to return :meth:`~._list_sessions`.
- `_list_sessions` (method): Return list of session object dictionaries.
- `list_sessions` (method): Return list of :class:`Session` from the ``tmux(1)`` session.
- `children` (property): Was used by TmuxRelationalObject (but that's longer used in this class).
- `_add_option` (attribute)
- `set_environment` (method): Set environment ``$ tmux set-environment <name> <value>``.
- `unset_environment` (method): Unset environment variable ``$ tmux set-environment -u <name>``.
- `remove_environment` (method): Remove environment variable ``$ tmux set-environment -r <name>``.
- `show_environment` (method): Show environment ``$ tmux show-environment -t [session]``.
- `getenv` (method): Show environment variable ``$ tmux show-environment -t [session] <name>``.
- `set_option` (method): Set option for tmux target.
- `unset_option` (method): Unset option for tmux target.
- `_show_options_raw` (method): Return a dict of options for the target.
- `_show_options_dict` (method): Return dict of options for the target.
- `_show_options` (method): Return a dict of options for the target.
- `show_options` (method): Return all options for the target.
- `_show_option_raw` (method): Return raw option output for target.
- `_show_option` (method): Return option value for the target.
- `show_option` (method): Return option value for the target.
- `hooks` (attribute)
- `_tmux_bin` (property): Resolve tmux_bin from self (Server) or self.server (Session/Window/Pane).
- `run_hook` (method): Run a hook immediately. Useful for testing.
- `set_hook` (method): Set hook for tmux target.
- `unset_hook` (method): Unset hook for tmux target.
- `show_hooks` (method): Return a dict of hooks for the target.
- `_show_hook` (method): Return value for the hook.
- `show_hook` (method): Return value for a hook.
- `set_hooks` (method): Set multiple indexed hooks at once.
