Servers

  • identified by socket path and socket name
  • may have >1 servers running of tmux at the same time.
  • hold Sessions (which hold Windows, which hold Panes)

In tmux, a server is automatically started on your behalf when you first run tmux.

class libtmux.Server(socket_name=None, socket_path=None, config_file=None, colors=None, **kwargs)

Bases: libtmux.common.TmuxRelationalObject, libtmux.common.EnvironmentMixin

The tmux(1) server.

When instantiated stores information on live, running tmux server.

child_id_attribute = u’session_id’

unique child ID key used by TmuxRelationalObject

formatter_prefix = u’server_’

namespace used TmuxMappingObject

socket_name = None

[-L socket-name]

socket_path = None

[-S socket-path]

config_file = None

[-f file]

colors = None

-2 or -8

cmd(*args, **kwargs)

Return util.tmux_cmd send tmux commands with sockets, colors.

Return type:util.tmux_cmd
Versionchanged:0.8 Renamed from .tmux to .cmd.
_list_sessions()

Return list of sessions in dict form.

Retrieved from $ tmux(1) list-sessions stdout.

The list is derived from stdout in util.tmux_cmd which wraps subprocess.Popen.

Return type:list of dict
_sessions

Property / alias to return _list_sessions().

list_sessions()

Return list of Session from the tmux(1) session.

Return type:list of Session
sessions

Property / alias to return list_sessions().

children

Alias of sessions, , used by TmuxRelationalObject

_list_windows()

Return list of windows in dict form.

Retrieved from $ tmux(1) list-windows stdout.

The list is derived from stdout in util.tmux_cmd which wraps subprocess.Popen.

Return type:list
_update_windows()

Update internal window data and return self for chainability.

Return type:Server
_list_panes()

Return list of panes in dict form.

Retrieved from $ tmux(1) list-panes stdout.

The list is derived from stdout in util.tmux_cmd which wraps subprocess.Popen.

Return type:list
_update_panes()

Update internal pane data and return self for chainability.

Return type:Server
attached_sessions

Return active Session objects.

This will not work where multiple tmux sessions are attached.

Return type:list of Session
has_session(target_session, exact=True)

Return True if session exists. $ tmux has-session.

Parameters:
  • target_session (str) – session name
  • exact (bool) – match the session name exactly. tmux uses fnmatch by default. Internally prepends = to the session in $ tmux has-session. tmux 2.1 and up only.
Raises:

exc.BadSessionName

Return type:

bool

kill_server()

$ tmux kill-server.

find_where(attrs)

Return object on first match.

Versionchanged:0.4 Renamed from .findWhere to .find_where.
get_by_id(id)

Return object based on child_id_attribute.

Based on .get() from backbone.js.

Parameters:id (str) –
Return type:object
kill_session(target_session=None)

Kill the tmux session with $ tmux kill-session, return self.

Param:target_session: str. note this accepts fnmatch(3). ‘asdf’ will kill ‘asdfasd’.
Raises:exc.BadSessionName
Return type:Server
remove_environment(name)

Remove environment variable $ tmux set-environment -r <name>.

Parameters:name – the environment variable name. such as ‘PATH’.
set_environment(name, value)

Set environment $ tmux set-environment <name> <value>.

Parameters:
  • name – the environment variable name. such as ‘PATH’.
  • value (str) – environment value.
show_environment(name=None)

Show environment $tmux show-environment -t [session] <name>.

Return dict of environment variables for the session or the value of a specific variable if the name is specified.

Parameters:name – the environment variable name. such as ‘PATH’.
unset_environment(name)

Unset environment variable $ tmux set-environment -u <name>.

Parameters:name – the environment variable name. such as ‘PATH’.
where(attrs, first=False)

Return objects matching child objects properties.

Parameters:attrs (dict) – tmux properties to match
Return type:list
switch_client(target_session)

$ tmux switch-client.

Param:target_session: str. name of the session. fnmatch(3) works.
Raises:exc.BadSessionName
attach_session(target_session=None)

$ tmux attach-session aka alias: $ tmux attach.

Param:target_session: str. name of the session. fnmatch(3) works.
Raises:exc.BadSessionName
new_session(session_name=None, kill_session=False, attach=False, start_directory=None, *args, **kwargs)

Return Session from $ tmux new-session.

Uses -P flag to print session info, -F for return formatting returns new Session object.

$ tmux new-session -d will create the session in the background $ tmux new-session -Ad will move to the session name if it already exists. todo: make an option to handle this.

Parameters:
  • session_name (str) –

    session name:

    $ tmux new-session -s <session_name>
    
  • attach (bool) –

    create session in the foreground. attach=False is equivalent to:

    $ tmux new-session -d
    
  • kill_session (bool) – Kill current session if $ tmux has-session Useful for testing workspaces.
  • start_directory (str) – specifies the working directory in which the new session is created.
Raises:

exc.BadSessionName

Return type:

Session