OwnedServer.server
A handle to one running tmux server, reached through a connected runtime.
Source and package
- Qualified name
-
libtmux.Owned Server. server - Module
- libtmux
- Declared in
OwnedServer- Package
- libtmux
- Source
- lua/libtmux/_internal/server.lua
- server : libtmux
.Server -
Description
libtmux.Runtime:connect()resolves to a server for a daemon that is already running; it never starts one, and closing the handle leaves the daemon running.libtmux.Runtime:start_server()starts a private daemon and owns it (seelibtmux.OwnedServer). With no socket named,connectpasses tmux none, and tmux picks the server (seelibtmux.ConnectOptions). A method that talks to tmux returns alibtmux.Request: calllibtmux.Request:await()from a task the runtime manages, or pass a callback tolibtmux.Request:on_complete(). A failed request reports alibtmux.Error. If the daemon restarts or its socket is replaced, the handle stops working and the caller connects again.What is on here
A tmux server does a great many things, so this class has a great many methods. They fall into a few groups:
- Connecting
libtmux.Runtime:connect()takeslibtmux.ConnectOptions: an absolute path to the tmux binary and, optionally, to the daemon's socket.Server:close()closes the connections the handle owns.Server:observe()takes a session handle and resolves to alibtmux.Observation, a control-mode view of that session whose watches report pane output, notifications and format values.- Finding one thing
- Take one record from a
libtmux.Snapshot, or from the rows of a query, withlibtmux.Selection:one(), which reports no match or several as errors, orlibtmux.Selection:one_or_nil(), which also accepts no match. Pass it, with its snapshot, toServer:handle(): that binds the record to a live session, window, window link, pane, client or buffer handle of the same class, and returns it directly rather than as a request. - Listing everything
Server:snapshot()lists sessions, windows, window links, panes, clients and buffers, and resolves to alibtmux.Snapshotwhose collections are selections. The capture spans several tmux commands, so it is not an atomic view: observed races land inlibtmux.Snapshot.racesand clearlibtmux.Snapshot.complete, andlibtmux.SnapshotOptions.strictadds a verification pass.- Querying things and getting around
Server:query()reads live state and resolves to the rows that match a criteria table, with the snapshot and plan behind them;Server:query_panes()does the same for panes.Server:explain()andServer:explain_panes()return the plan without contacting tmux. On a snapshot already in hand,libtmux.Selection:where()andlibtmux.Selection:filter()narrow a collection in memory, andlibtmux.Selection:first(),libtmux.Selection:count()andlibtmux.Selection:exists()inspect it; thelibtmux.querymodule does the same for ordinary Lua tables. Records lead to each other without another command:libtmux.SnapshotSession.window_links,libtmux.SnapshotWindow.panes,libtmux.SnapshotPane.window,libtmux.SnapshotWindowLink.sessionandlibtmux.SnapshotWindowLink.window.-
Server:query()Server:query_panes()Server:explain()Server:explain_panes()libtmux.Selection:where()libtmux.Selection:filter()libtmux.Selection:first()libtmux.Selection:count()libtmux.Selection:exists()libtmux.querylibtmux.SnapshotSession.window_linkslibtmux.SnapshotWindow.paneslibtmux.SnapshotPane.windowlibtmux.SnapshotWindowLink.sessionlibtmux.SnapshotWindowLink.window
- Changing things
Server:new_session()creates a detached session and resolves to alibtmux.Creationholding handles for the session and the window, pane and window link it starts with.Server:switch_client()points an attached client, selected by the name and TTY a snapshot reports, at a session handle, andServer:detach_client()detaches it.- Options and hooks
Server:get_option(),Server:list_options(),Server:set_option()andServer:unset_option()address the server's own options; passscope = "global_session"or"global_window"for tmux's global defaults.Server:get_hook(),Server:list_hooks(),Server:set_hook()andServer:unset_hook()need one of those two scopes spelled out. The global environment hasServer:get_environment(),Server:list_environment()andServer:set_environment();Server:unset_environment()deletes a name, whileServer:remove_environment()records a removal marker instead.- Everything else tmux keeps
- Paste buffers, always by explicit name:
Server:set_buffer()stores bytes,Server:show_buffer()reads them back andServer:delete_buffer()removes the buffer. None infers the most recent buffer. Key bindings, configuration files, shell jobs and wait-for channels have no method here; reach them throughServer:command(). - Things that need a terminal
- None. The luv and Neovim adapters do not own an interactive terminal, so
libtmux.Session:attach()returnsunsupported_tty. - Running a command
- Anything tmux can do that is not here is reachable through
Server:command(), which runs one tmux command from literal arguments and resolves to its stdout, stderr, exit code and signal; a nonzero exit is reported in that result, not as a failure.Server:group()sends several commands as one ordered group with a single aggregate result, andServer:batch()runs independent commands and reports an outcome for each.
Examples
Start a private tmux server, create a session on it, and stop it. Use Lua 5.1 or newer with LuaRocks, tmux 3.3a or newer, and a C compiler and CMake for luv on Linux. In a new directory, install the library and its standalone runtime dependency from LuaRocks:
Terminal window $ luarocks --tree ./rocks install luv &&luarocks --tree ./rocks install libtmux &&eval "$(luarocks --tree ./rocks path)"Save the complete program as
start_server.lua. It starts its own private tmux server, uses it and stops it, so no existing session is needed. View sourcestart_server.luaComplete program local adapter = require("libtmux.runtime.luv")local function must(value, err)if err ~= nil thenerror(tostring(err), 0)endreturn valueend-- tmux is /usr/bin/tmux unless TMUX_BIN names another absolute path.local binary = os.getenv("TMUX_BIN") or "/usr/bin/tmux"must(adapter.run(function(runtime)-- The server is private: its own directory, socket and environment.local owned = must(runtime:start_server({ binary = binary }):await())local server = owned.serverlocal before = must(server:snapshot():await())must(server:new_session({ name = "demo", argv = { "/bin/cat" } }):await())local after = must(server:snapshot():await())print(("sessions: %d, then %d"):format(#before.sessions, #after.sessions))must(owned:stop():await())return trueend))Run the saved program. It runs
/usr/bin/tmux; setTMUX_BINto another absolute path to use a different binary. Assertions and operation errors produce a failing exit status:Terminal window $ eval "$(luarocks --tree ./rocks path)" &&lua start_server.lua
Guides
task materialPages that discuss server.