# libtmux.Session

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

:term:`tmux(1)` :term:`Session` [session_manual]_.

Holds :class:`Window` objects.

## Example

```python
>>> session
Session($1 ...)
```

## Example

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

## Example

```python
>>> session.active_window
Window(@1 ...:..., Session($1 ...))
```

## Example

```python
>>> session.active_pane
Pane(%1 Window(@1 ...:..., Session($1 ...)))
```

## Example

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

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

## Members

- `default_option_scope` (attribute)
- `default_hook_scope` (attribute)
- `server` (attribute)
- `__enter__` (method): Enter the context, returning self.
- `__exit__` (method): Exit the context, killing the session if it exists.
- `refresh` (method): Refresh session attributes from tmux.
- `from_session_id` (method): Create Session from existing session_id.
- `from_env` (method): Return the session this process's pane belongs to.
- `windows` (property): Windows contained by session.
- `panes` (property): Panes contained by session's windows.
- `search_windows` (method): Windows in this session, optionally filtered by tmux.
- `search_panes` (method): Panes in this session, optionally filtered by tmux.
- `cmd` (method): Execute tmux subcommand within session context.
- `lock_session` (method): Lock this session via ``$ tmux lock-session``.
- `detach_client` (method): Detach every client attached to this session.
- `last_window` (method): Select the last (previously selected) window.
- `next_window` (method): Select the next window.
- `previous_window` (method): Select the previous window.
- `select_window` (method): Select window and return the selected window.
- `active_pane` (property): Return the active :class:`Pane` object.
- `active_window` (property): Return the active :class:`Window` object.
- `attach` (method): Return ``$ tmux attach-session`` aka alias: ``$ tmux attach``.
- `kill` (method): Kill :class:`Session`, closes linked windows and detach all clients.
- `switch_client` (method): Switch client to session.
- `rename_session` (method): Rename session and return new :class:`Session` object.
- `new_window` (method): Create new window, returns new :class:`Window`.
- `kill_window` (method): Close a tmux window, and all panes inside it, ``$ tmux kill-window``.
- `__eq__` (method): Equal operator for :class:`Session` object.
- `__repr__` (method): Representation of :class:`Session` object.
- `id` (property): Alias of :attr:`Session.session_id`.
- `name` (property): Alias of :attr:`Session.session_name`.
- `attached_pane` (property): Return the active :class:`Pane` object.
- `attached_window` (property): Return the active :class:`Window` object.
- `attach_session` (method): Return ``$ tmux attach-session`` aka alias: ``$ tmux attach``.
- `kill_session` (method): Destroy session.
- `get` (method): Return key-based lookup. Deprecated by attributes.
- `__getitem__` (method): Return item lookup by key. Deprecated in favor of attributes.
- `get_by_id` (method): Return window by id. Deprecated in favor of :meth:`.windows.get()`.
- `where` (method): Filter through windows, return list of :class:`Window`.
- `find_where` (method): Filter through windows, return first :class:`Window`.
- `_list_windows` (method): Return list of windows (deprecated in favor of :attr:`.windows`).
- `_windows` (property): Property / alias to return :meth:`Session._list_windows`.
- `list_windows` (method): Return a list of :class:`Window` from the ``tmux(1)`` session.
- `children` (property): Was used by TmuxRelationalObject (but that's longer used in this class).
- `active_window_index` (attribute)
- `alternate_saved_x` (attribute)
- `alternate_saved_y` (attribute)
- `bracket_paste_flag` (attribute)
- `buffer_name` (attribute)
- `buffer_sample` (attribute)
- `buffer_size` (attribute)
- `client_activity` (attribute)
- `client_cell_height` (attribute)
- `client_cell_width` (attribute)
- `client_control_mode` (attribute)
- `client_created` (attribute)
- `client_discarded` (attribute)
- `client_flags` (attribute)
- `client_height` (attribute)
- `client_key_table` (attribute)
- `client_last_session` (attribute)
- `client_mode_format` (attribute)
- `client_name` (attribute)
- `client_pid` (attribute)
- `client_prefix` (attribute)
- `client_readonly` (attribute)
- `client_session` (attribute)
- `client_termfeatures` (attribute)
- `client_termname` (attribute)
- `client_termtype` (attribute)
- `client_tty` (attribute)
- `client_uid` (attribute)
- `client_user` (attribute)
- `client_utf8` (attribute)
- `client_width` (attribute)
- `client_written` (attribute)
- `command_list_alias` (attribute)
- `command_list_name` (attribute)
- `command_list_usage` (attribute)
- `config_files` (attribute)
- `copy_cursor_line` (attribute)
- `copy_cursor_word` (attribute)
- `copy_cursor_x` (attribute)
- `copy_cursor_y` (attribute)
- `current_file` (attribute)
- `cursor_character` (attribute)
- `cursor_flag` (attribute)
- `cursor_x` (attribute)
- `cursor_y` (attribute)
- `history_bytes` (attribute)
- `history_limit` (attribute)
- `history_size` (attribute)
- `insert_flag` (attribute)
- `keypad_cursor_flag` (attribute)
- `keypad_flag` (attribute)
- `last_window_index` (attribute)
- `line` (attribute)
- `mouse_all_flag` (attribute)
- `mouse_any_flag` (attribute)
- `mouse_button_flag` (attribute)
- `mouse_sgr_flag` (attribute)
- `mouse_standard_flag` (attribute)
- `next_session_id` (attribute)
- `origin_flag` (attribute)
- `pane_active` (attribute)
- `pane_at_bottom` (attribute)
- `pane_at_left` (attribute)
- `pane_at_right` (attribute)
- `pane_at_top` (attribute)
- `pane_bg` (attribute)
- `pane_bottom` (attribute)
- `pane_current_command` (attribute)
- `pane_current_path` (attribute)
- `pane_dead` (attribute)
- `pane_dead_signal` (attribute)
- `pane_dead_status` (attribute)
- `pane_dead_time` (attribute)
- `pane_fg` (attribute)
- `pane_flags` (attribute)
- `pane_floating_flag` (attribute)
- `pane_format` (attribute)
- `pane_height` (attribute)
- `pane_id` (attribute)
- `pane_in_mode` (attribute)
- `pane_index` (attribute)
- `pane_input_off` (attribute)
- `pane_last` (attribute)
- `pane_left` (attribute)
- `pane_marked` (attribute)
- `pane_marked_set` (attribute)
- `pane_mode` (attribute)
- `pane_path` (attribute)
- `pane_pb_progress` (attribute)
- `pane_pb_state` (attribute)
- `pane_pid` (attribute)
- `pane_pipe` (attribute)
- `pane_pipe_pid` (attribute)
- `pane_right` (attribute)
- `pane_search_string` (attribute)
- `pane_start_command` (attribute)
- `pane_start_path` (attribute)
- `pane_synchronized` (attribute)
- `pane_tabs` (attribute)
- `pane_title` (attribute)
- `pane_top` (attribute)
- `pane_tty` (attribute)
- `pane_width` (attribute)
- `pane_x` (attribute)
- `pane_y` (attribute)
- `pane_z` (attribute)
- `pane_zoomed_flag` (attribute)
- `pid` (attribute)
- `scroll_position` (attribute)
- `scroll_region_lower` (attribute)
- `scroll_region_upper` (attribute)
- `search_match` (attribute)
- `selection_end_x` (attribute)
- `selection_end_y` (attribute)
- `selection_start_x` (attribute)
- `selection_start_y` (attribute)
- `session_activity` (attribute)
- `session_alerts` (attribute)
- `session_attached` (attribute)
- `session_attached_list` (attribute)
- `session_created` (attribute)
- `session_format` (attribute)
- `session_group` (attribute)
- `session_group_attached` (attribute)
- `session_group_attached_list` (attribute)
- `session_group_list` (attribute)
- `session_group_many_attached` (attribute)
- `session_group_size` (attribute)
- `session_grouped` (attribute)
- `session_id` (attribute)
- `session_last_attached` (attribute)
- `session_many_attached` (attribute)
- `session_marked` (attribute)
- `session_name` (attribute)
- `session_path` (attribute)
- `session_stack` (attribute)
- `session_windows` (attribute)
- `socket_path` (attribute)
- `start_time` (attribute)
- `synchronized_output_flag` (attribute)
- `uid` (attribute)
- `user` (attribute)
- `version` (attribute)
- `window_active` (attribute)
- `window_active_clients` (attribute)
- `window_active_clients_list` (attribute)
- `window_active_sessions` (attribute)
- `window_active_sessions_list` (attribute)
- `window_activity` (attribute)
- `window_activity_flag` (attribute)
- `window_bell_flag` (attribute)
- `window_bigger` (attribute)
- `window_cell_height` (attribute)
- `window_cell_width` (attribute)
- `window_end_flag` (attribute)
- `window_flags` (attribute)
- `window_format` (attribute)
- `window_height` (attribute)
- `window_id` (attribute)
- `window_index` (attribute)
- `window_last_flag` (attribute)
- `window_layout` (attribute)
- `window_linked` (attribute)
- `window_linked_sessions` (attribute)
- `window_linked_sessions_list` (attribute)
- `window_marked_flag` (attribute)
- `window_name` (attribute)
- `window_offset_x` (attribute)
- `window_offset_y` (attribute)
- `window_panes` (attribute)
- `window_raw_flags` (attribute)
- `window_silence_flag` (attribute)
- `window_stack_index` (attribute)
- `window_start_flag` (attribute)
- `window_visible_layout` (attribute)
- `window_width` (attribute)
- `window_zoomed_flag` (attribute)
- `wrap_flag` (attribute)
- `_refresh` (method): Refresh dataclass fields from a single ``list-*`` row.
- `_add_option` (attribute)
- `__init__` (method)
- `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.
