Sessions¶
Bases:
Obj,EnvironmentMixin,OptionsMixin,HooksMixintmux(1) Session [session_manual].
Holds
Windowobjects.- Parameters:
server (
Server)
Examples
>>> session Session($1 ...)
>>> session.windows [Window(@1 ...:..., Session($1 ...)]
>>> session.active_window Window(@1 ...:..., Session($1 ...))
>>> session.active_pane Pane(%1 Window(@1 ...:..., Session($1 ...)))
The session can be used as a context manager to ensure proper cleanup:
>>> with server.new_session() as session: ... window = session.new_window() ... # Do work with the window ... # Session will be killed automatically when exiting the context
References
[session_manual]- tmux session. openbsd manpage for TMUX(1).
“When tmux is started it creates a new session with a single window and displays it on screen…”
“A session is a single collection of pseudo terminals under the management of tmux. Each session has one or more windows linked to it.”
https://man.openbsd.org/tmux.1#DESCRIPTION. Accessed April 1st, 2018.
-
attribute
-
attribute
Create Session from existing session_id.
Windows contained by session.
Can be accessed via
.windows.get()and.windows.filter()
Panes contained by session’s windows.
Can be accessed via
.panes.get()and.panes.filter()
Execute tmux subcommand within session context.
Automatically binds target by adding
-tfor object’s session ID to the command. Passtargetto keyword arguments to override.Examples
>>> session.cmd('new-window', '-P').stdout[0] 'libtmux...:....0'
From raw output to an enriched Window object:
>>> Window.from_window_id(window_id=session.cmd( ... 'new-window', '-P', '-F#{window_id}').stdout[0], server=session.server) Window(@... ...:..., Session($1 libtmux_...))
Notes
Changed in version 0.34: Passing target by
-tis ignored. Usetargetkeyword argument instead.Changed in version 0.8: Renamed from
.tmuxto.cmd.
Select window and return the selected window.
Return
$ tmux attach-sessionaka alias:$ tmux attach.Examples
>>> session = server.new_session()
>>> session not in server.attached_sessions True
Kill
Session, closes linked windows and detach all clients.$ tmux kill-session.Examples
Kill a session:
>>> session_1 = server.new_session()
>>> session_1 in server.sessions True
>>> session_1.kill()
>>> session_1 not in server.sessions True
Kill all sessions except the current one:
>>> one_session_to_rule_them_all = server.new_session()
>>> other_sessions = server.new_session( ... ), server.new_session()
>>> all([w in server.sessions for w in other_sessions]) True
>>> one_session_to_rule_them_all.kill(all_except=True)
>>> all([w not in server.sessions for w in other_sessions]) True
>>> one_session_to_rule_them_all in server.sessions True
Switch client to session.
- Raises:
- Return type:
Create new window, returns new
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.- Parameters:
window_name (str, optional)
start_directory (str, optional) – working directory in which the new window is created.
attach (bool, optional) – 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, optional) –
execute a command on starting the window. The window will close when the command exits.
Note
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.
direction (WindowDirection, optional) – Insert window before or after target window.
target_window (str, optional) – Used by
Window.new_window()to specify the target window.versionchanged: (..) – 0.28.0:
attachdefault changed fromTruetoFalse.
- Return type:
See also
Examples
>>> window_initial = session.new_window(window_name='Example') >>> window_initial Window(@... 2:Example, Session($1 libtmux_...)) >>> window_initial.window_index '2'
>>> 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_...))
>>> 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_...))
Close a tmux window, and all panes inside it,
$ tmux kill-window.Kill the current window or the window at
target-window. removing it from any sessions to which it is linked.- Parameters:
- Raises:
libtmux.exc.LibTmuxException – If tmux returns an error.
- Return type:
Alias of
Session.session_name.>>> session.name 'libtmux_...'
>>> session.name == session.session_name True
Return the active
Paneobject.Notes
Deprecated since version 0.31: Deprecated in favor of
active_pane().
Return the active
Windowobject.Notes
Deprecated since version 0.31: Deprecated in favor of
active_window().
- Return type:
Return key-based lookup. Deprecated by attributes.
Deprecated since version 0.17: Deprecated by attribute lookup.e.g.
session['session_name']is now accessed viasession.session_name.
Return window by id. Deprecated in favor of
windows.get().Deprecated since version 0.16: Deprecated by
windows.get().
Filter through windows, return list of
Window.Deprecated since version 0.17: Deprecated by
windows.filter().
Filter through windows, return first
Window.Deprecated since version 0.17: Slated to be removed in favor of
windows.get().
Property / alias to return
Session._list_windows().Deprecated since version 0.17: Slated to be removed in favor of
windows.
-
__init__(server, active_window_index=None, alternate_saved_x=None, alternate_saved_y=None, buffer_name=None, buffer_sample=None, buffer_size=None, client_cell_height=None, client_cell_width=None, client_discarded=None, client_flags=None, client_height=None, client_key_table=None, client_name=None, client_pid=None, client_termname=None, client_tty=None, client_uid=None, client_user=None, client_width=None, client_written=None, command_list_alias=None, command_list_name=None, command_list_usage=None, config_files=None, copy_cursor_line=None, copy_cursor_word=None, copy_cursor_x=None, copy_cursor_y=None, current_file=None, cursor_character=None, cursor_flag=None, cursor_x=None, cursor_y=None, history_bytes=None, history_limit=None, history_size=None, insert_flag=None, keypad_cursor_flag=None, keypad_flag=None, last_window_index=None, line=None, mouse_all_flag=None, mouse_any_flag=None, mouse_button_flag=None, mouse_sgr_flag=None, mouse_standard_flag=None, next_session_id=None, origin_flag=None, pane_active=None, pane_at_bottom=None, pane_at_left=None, pane_at_right=None, pane_at_top=None, pane_bg=None, pane_bottom=None, pane_current_command=None, pane_current_path=None, pane_dead_signal=None, pane_dead_status=None, pane_dead_time=None, pane_fg=None, pane_height=None, pane_id=None, pane_index=None, pane_left=None, pane_pid=None, pane_right=None, pane_search_string=None, pane_start_command=None, pane_start_path=None, pane_tabs=None, pane_title=None, pane_top=None, pane_tty=None, pane_width=None, pid=None, scroll_position=None, scroll_region_lower=None, scroll_region_upper=None, search_match=None, selection_end_x=None, selection_end_y=None, selection_start_x=None, selection_start_y=None, session_activity=None, session_alerts=None, session_attached=None, session_attached_list=None, session_created=None, session_group=None, session_group_attached=None, session_group_list=None, session_group_size=None, session_id=None, session_last_attached=None, session_name=None, session_path=None, session_stack=None, session_windows=None, socket_path=None, start_time=None, uid=None, user=None, version=None, window_active=None, window_active_clients=None, window_active_sessions=None, window_activity=None, window_cell_height=None, window_cell_width=None, window_height=None, window_id=None, window_index=None, window_layout=None, window_linked=None, window_linked_sessions=None, window_linked_sessions_list=None, window_marked_flag=None, window_name=None, window_offset_x=None, window_offset_y=None, window_panes=None, window_raw_flags=None, window_stack_index=None, window_width=None, wrap_flag=None, default_option_scope=None, default_hook_scope=None)¶method[source]
When not a user (custom) option, scope can be implied.
- Parameters:
server (Server)
default_option_scope (OptionScope | None)
default_hook_scope (OptionScope | None)
- Return type:
-
_add_option = None¶attribute
Return value for the hook.
- Parameters:
hook (str)
global_ (bool)
scope (OptionScope | _DefaultOptionScope | None)
- Raises:
- Return type:
Return option value for the target.
todo: test and return True/False for on/off string
- Parameters:
- Raises:
- Return type:
ConvertedValue | None
Examples
>>> import typing as t >>> from libtmux.common import tmux_cmd >>> from libtmux.constants import OptionScope
>>> class MyServer(OptionsMixin): ... socket_name = server.socket_name ... def cmd(self, cmd: str, *args: object): ... cmd_args: t.List[t.Union[str, int]] = [cmd] ... if self.socket_name: ... cmd_args.insert(0, f"-L{self.socket_name}") ... cmd_args.insert(0, "-f/dev/null") ... return tmux_cmd(*cmd_args, *args) ... ... default_option_scope = OptionScope.Server
>>> MyServer().cmd('new-session', '-d') <libtmux.common.tmux_cmd object at ...>
>>> MyServer()._show_option('exit-unattached', global_=True) False
Return raw option output for target.
- Parameters:
- Raises:
- Return type:
Examples
>>> import typing as t >>> from libtmux.common import tmux_cmd >>> from libtmux.constants import OptionScope
>>> class MyServer(OptionsMixin): ... socket_name = server.socket_name ... def cmd(self, cmd: str, *args: object): ... cmd_args: t.List[t.Union[str, int]] = [cmd] ... if self.socket_name: ... cmd_args.insert(0, f"-L{self.socket_name}") ... cmd_args.insert(0, "-f/dev/null") ... return tmux_cmd(*cmd_args, *args) ... ... default_option_scope = OptionScope.Server
>>> MyServer().cmd('new-session', '-d') <libtmux.common.tmux_cmd object at ...>
>>> MyServer()._show_option_raw('exit-unattached', global_=True) <libtmux.common.tmux_cmd object at ...>
>>> MyServer()._show_option_raw('exit-unattached', global_=True).stdout ['exit-unattached off']
>>> isinstance(MyServer()._show_option_raw('exit-unattached', global_=True).stdout, list) True
>>> isinstance(MyServer()._show_option_raw('exit-unattached', global_=True).stdout[0], str) True
Return a dict of options for the target.
- Parameters:
g (bool, optional) –
Deprecated since version 0.50.0: Use
global_instead.scope (OptionScope | _DefaultOptionScope | None)
- Return type:
ExplodedComplexUntypedOptionsDict
Examples
>>> import typing as t >>> from libtmux.common import tmux_cmd >>> from libtmux.constants import OptionScope
>>> class MyServer(OptionsMixin): ... socket_name = server.socket_name ... def cmd(self, cmd: str, *args: object): ... cmd_args: t.List[t.Union[str, int]] = [cmd] ... if self.socket_name: ... cmd_args.insert(0, f"-L{self.socket_name}") ... cmd_args.insert(0, "-f/dev/null") ... return tmux_cmd(*cmd_args, *args) ... ... default_option_scope = OptionScope.Server
>>> MyServer()._show_options() {...}
Return dict of options for the target.
- Parameters:
g (bool, optional) –
Deprecated since version 0.50.0: Use
global_instead.scope (OptionScope | _DefaultOptionScope | None)
- Return type:
UntypedOptionsDict
Examples
>>> import typing as t >>> from libtmux.common import tmux_cmd >>> from libtmux.constants import OptionScope
>>> class MyServer(OptionsMixin): ... socket_name = server.socket_name ... def cmd(self, cmd: str, *args: object): ... cmd_args: t.List[t.Union[str, int]] = [cmd] ... if self.socket_name: ... cmd_args.insert(0, f"-L{self.socket_name}") ... cmd_args.insert(0, "-f/dev/null") ... return tmux_cmd(*cmd_args, *args) ... ... default_option_scope = OptionScope.Server
>>> MyServer()._show_options_dict() {...}
>>> isinstance(MyServer()._show_options_dict(), dict) True
Return a dict of options for the target.
Examples
>>> import typing as t >>> from libtmux.common import tmux_cmd >>> from libtmux.constants import OptionScope
>>> class MyServer(OptionsMixin): ... socket_name = server.socket_name ... def cmd(self, cmd: str, *args: object): ... cmd_args: t.List[t.Union[str, int]] = [cmd] ... if self.socket_name: ... cmd_args.insert(0, f"-L{self.socket_name}") ... cmd_args.insert(0, "-f/dev/null") ... return tmux_cmd(*cmd_args, *args) ... ... default_option_scope = OptionScope.Server
>>> MyServer()._show_options_raw() <libtmux.common.tmux_cmd object at ...>
>>> MyServer()._show_options_raw().stdout [...]
Resolve tmux_bin from self (Server) or self.server (Session/Window/Pane).
Show environment variable
$ tmux show-environment -t [session] <name>.Return the value of a specific variable if the name is specified.
Added in version 0.13.
Remove environment variable
$ tmux set-environment -r <name>.- Parameters:
name (str) – The environment variable name, e.g. ‘PATH’.
- Raises:
ValueError – If tmux returns an error.
- Return type:
Run a hook immediately. Useful for testing.
- Parameters:
hook (str)
scope (OptionScope | _DefaultOptionScope | None)
- Return type:
Self
Set environment
$ tmux set-environment <name> <value>.- Parameters:
- Raises:
ValueError – If tmux returns an error.
- Return type:
Set hook for tmux target.
Wraps
$ tmux set-hook <hook> <value>.- Parameters:
- Raises:
- Return type:
Self
Set multiple indexed hooks at once.
- Parameters:
hook (str) – Hook name, e.g. ‘session-renamed’
values (HookValues) – Values to set. Can be: - dict[int, str]: {0: ‘cmd1’, 1: ‘cmd2’} - explicit indices - SparseArray[str]: preserves indices from another hook - list[str]: [‘cmd1’, ‘cmd2’] - sequential indices starting at 0
clear_existing (bool) – If True, unset all existing hook values first
scope (OptionScope | None) – Scope for the hook
- Returns:
Returns self for method chaining.
- Return type:
Self
Examples
Set hooks with explicit indices:
>>> session.set_hooks('session-renamed', { ... 0: 'display-message "hook 0"', ... 1: 'display-message "hook 1"', ... }) Session($...)
>>> hooks = session.show_hook('session-renamed') >>> sorted(hooks.keys()) [0, 1]
>>> session.unset_hook('session-renamed') Session($...)
Set hooks from a list (sequential indices):
>>> session.set_hooks('after-new-window', [ ... 'select-pane -t 0', ... 'send-keys "clear" Enter', ... ]) Session($...)
>>> hooks = session.show_hook('after-new-window') >>> sorted(hooks.keys()) [0, 1]
Replace all existing hooks with
clear_existing=True:>>> session.set_hooks( ... 'session-renamed', ... {0: 'display-message "new"'}, ... clear_existing=True, ... ) Session($...)
>>> hooks = session.show_hook('session-renamed') >>> sorted(hooks.keys()) [0]
>>> session.unset_hook('session-renamed') Session($...)
>>> session.unset_hook('after-new-window') Session($...)
Set option for tmux target.
Wraps
$ tmux set-option <option> <value>.- Parameters:
option (str) – option to set, e.g. ‘aggressive-resize’
value (str) – option value. True/False will turn in ‘on’ and ‘off’, also accepts string of ‘on’ or ‘off’ directly.
deprecated: (..) – 0.28: Deprecated by
gfor global, useglobal_instead.scope (OptionScope | _DefaultOptionScope | None)
- Raises:
- Return type:
Self
Examples
>>> import typing as t >>> from libtmux.common import tmux_cmd >>> from libtmux.constants import OptionScope >>> from libtmux._internal.sparse_array import SparseArray
>>> class MyServer(OptionsMixin): ... socket_name = server.socket_name ... def cmd(self, cmd: str, *args: object): ... cmd_args: t.List[t.Union[str, int]] = [cmd] ... if self.socket_name: ... cmd_args.insert(0, f"-L{self.socket_name}") ... cmd_args.insert(0, "-f/dev/null") ... return tmux_cmd(*cmd_args, *args) ... ... default_option_scope = OptionScope.Server
>>> MyServer().set_option('escape-time', 1250) <libtmux.options.MyServer object at ...>
>>> MyServer()._show_option('escape-time') 1250
>>> MyServer().set_option('escape-time', 495) <libtmux.options.MyServer object at ...>
>>> MyServer()._show_option('escape-time') 495
Show environment
$ tmux show-environment -t [session].Return dict of environment variables for the session.
Changed in version 0.13: Removed per-item lookups. Use
libtmux.common.EnvironmentMixin.getenv().- Returns:
environmental variables in dict, if no name, or str if name entered.
- Return type:
Return value for a hook.
For array hooks (e.g.,
session-renamed), returns aSparseArraywith hook values at their original indices. Use.keys()for indices and.values()for values.- Parameters:
hook (str) – Hook name to query
global_ (bool)
scope (OptionScope | _DefaultOptionScope | None)
- Returns:
Hook value. For array hooks, returns SparseArray.
- Return type:
str | int | SparseArray[str] | None
- Raises:
Examples
>>> session.set_hook('session-renamed[0]', 'display-message "test"') Session($...)
>>> hooks = session.show_hook('session-renamed') >>> isinstance(hooks, SparseArray) True
>>> sorted(hooks.keys()) [0]
>>> session.unset_hook('session-renamed') Session($...)
Return a dict of hooks for the target.
- Parameters:
global (bool, optional) – Pass
-gflag for global hooks, default False.scope (OptionScope | _DefaultOptionScope | None, optional) – Hook scope (Server/Session/Window/Pane), defaults to object’s scope.
- Returns:
Dictionary mapping hook names to their values.
- Return type:
HookDict
Examples
>>> session.set_hook('session-renamed[0]', 'display-message "test"') Session($...)
>>> hooks = session.show_hooks() >>> isinstance(hooks, dict) True
>>> 'session-renamed[0]' in hooks True
>>> session.unset_hook('session-renamed') Session($...)
Return option value for the target.
- Parameters:
- Raises:
- Return type:
Examples
>>> import typing as t >>> from libtmux.common import tmux_cmd >>> from libtmux.constants import OptionScope
>>> class MyServer(OptionsMixin): ... socket_name = server.socket_name ... def cmd(self, cmd: str, *args: object): ... cmd_args: t.List[t.Union[str, int]] = [cmd] ... if self.socket_name: ... cmd_args.insert(0, f"-L{self.socket_name}") ... cmd_args.insert(0, "-f/dev/null") ... return tmux_cmd(*cmd_args, *args) ... ... default_option_scope = OptionScope.Server
>>> MyServer().cmd('new-session', '-d') <libtmux.common.tmux_cmd object at ...>
>>> MyServer().show_option('exit-unattached', global_=True) False
Return all options for the target.
- Parameters:
global (bool, optional) – Pass
-gflag for global options, default False.scope (OptionScope | _DefaultOptionScope | None, optional) – Option scope (Server/Session/Window/Pane), defaults to object’s scope.
include_hooks (bool, optional) – Include hook options (
-Hflag).include_inherited (bool, optional) – Include inherited options (
-Aflag).global_ (bool)
- Returns:
Dictionary with all options, arrays exploded and values converted.
- Return type:
ExplodedComplexUntypedOptionsDict
- Raises:
Examples
>>> options = server.show_options() >>> isinstance(options, dict) True
>>> 'buffer-limit' in options True
Unset environment variable
$ tmux set-environment -u <name>.- Parameters:
name (str) – The environment variable name, e.g. ‘PATH’.
- Raises:
ValueError – If tmux returns an error.
- Return type:
Unset hook for tmux target.
Wraps
$ tmux set-hook -u <hook>/$ tmux set-hook -U <hook>- Parameters:
hook (str) – hook to unset, e.g. ‘after-show-environment’
scope (OptionScope | _DefaultOptionScope | None)
- Raises:
- Return type:
Self
Unset option for tmux target.
Wraps
$ tmux set-option -u <option>/$ tmux set-option -U <option>- Parameters:
option (str) – option to unset, e.g. ‘aggressive-resize’
scope (OptionScope | _DefaultOptionScope | None)
- Raises:
- Return type:
Self
Examples
>>> import typing as t >>> from libtmux.common import tmux_cmd >>> from libtmux.constants import OptionScope
>>> class MyServer(OptionsMixin): ... socket_name = server.socket_name ... def cmd(self, cmd: str, *args: object): ... cmd_args: t.List[t.Union[str, int]] = [cmd] ... if self.socket_name: ... cmd_args.insert(0, f"-L{self.socket_name}") ... cmd_args.insert(0, "-f/dev/null") ... return tmux_cmd(*cmd_args, *args) ... ... default_option_scope = OptionScope.Server
>>> MyServer().set_option('escape-time', 1250) <libtmux.options.MyServer object at ...>
>>> MyServer()._show_option('escape-time') 1250
>>> MyServer().unset_option('escape-time') <libtmux.options.MyServer object at ...>
>>> isinstance(MyServer()._show_option('escape-time'), int) True