tmux

package
v0.0.0-...-374cbe0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 7 Imported by: 0

README

pkg/tmux

Tmux session and window management primitives. Wraps the tmux CLI directly via os/exec (with bitfield/script reserved for piped fzf flows).

Session API

  • Switch(name string) error — switches to name, creating the session if it doesn't exist. Inside tmux uses switch-client; outside it uses attach.
  • SwitchExisting(name string) error — switches without creating; used by prev/next rotation through history.
  • NewSession(name string) error — creates a detached session with -c <name> (working directory set to the session name).
  • KillSession(name string) error — terminates the session.
  • HasSession(name string) error — returns nil if the session exists, error otherwise.
  • SessionExists(name string) (bool, error) — same check, surfaced as a boolean.
  • ListSessions() ([]string, error) — lists session names, swallowing the "no server running" error as an empty list.
  • Attach(name string) error / SwitchClient(name string) error — direct primitives behind Switch.
  • DisplaySessions() (string, error) — fzf picker over sessions with pane-capture preview.
  • GetCurrentSession() (string, error) — current session name (uses $TMUX_PANE / display-message).

Window API

  • ListWindows() ([]string, error)
  • NewWindow(name, command, directory string) error
  • KillWindow(windowID string) error
  • SelectWindow(name string) error

Session Name Canonicalisation

Sessions are named after directory paths. Dots in directory names conflict with tmux's target-pattern syntax (session:window.pane), so canonicalSessionName() replaces . with _ before any tmux call. Callers pass real paths; the canonicalisation happens inside the package.

Switch-then-Persist Pattern

History (~/.scripts.yaml) is updated only after a successful switch. If tmux returns an error, the file is left alone — preventing a broken session from polluting recent history.

Error Handling

  • runTmux / runTmuxOutput capture combined output and join non-empty stderr into the returned error with fmt.Errorf("%w: %s", err, message).
  • isExitCode / isNoServerRunning classify expected error shapes (no server running ⇒ empty session list).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Attach

func Attach(name string) error

Attach attaches the current tmux instance to the given session.

NOTE: We can't use the `scripts` package because (for some unknown reason to me) tmux` requires that we bind `stdout`, `stderr`, and `stdin` to the spawned process for it to work.

func DisplaySessions

func DisplaySessions() (string, error)

DisplaySessions dynamically renders all the current active sessions and allows you to traverse to them.

func GetCurrentSession

func GetCurrentSession() (string, error)

GetCurrentSession returns the name of the current tmux session.

func HasSession

func HasSession(name string) error

HasSession checks if the given session exists.

func KillSession

func KillSession(name string) error

KillSessions kills a session.

func KillWindow

func KillWindow(windowID string) error

KillWindow kills a window by its ID.

func ListSessions

func ListSessions() ([]string, error)

ListSessions returns a list of all the running Tmux sessions

func ListWindows

func ListWindows() ([]string, error)

ListWindows returns a list of window IDs in the current session.

func Ls

func Ls() ([]string, error)

Ls returns a list of `tmux` running sessions.

func NewSession

func NewSession(name string) error

NewSession creates a new tmux session.

func NewWindow

func NewWindow(name, command, directory string) error

NewWindow creates a new window with the given name, command, and directory.

func SelectWindow

func SelectWindow(name string) error

SelectWindow selects (focuses) a window by name.

func SessionExists

func SessionExists(name string) (bool, error)

SessionExists returns whether a session exists.

func Switch

func Switch(name string) error

Switch ensures that you create/switch/attach to a new session by name.

The value of `name` is supposed to be a directory path.

func SwitchClient

func SwitchClient(name string) error

SwitchClient switches the client to the given session.

func SwitchExisting

func SwitchExisting(name string) error

SwitchExisting switches to an existing session without creating it.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL