A comprehensive library designed to interact with tmux, communicating through the tmux CLI and aiming to offer complete functionality for tmux integration.
- Go 1.22.3 or higher
- tmux installed on the system
go get github.com/GianlucaP106/gotmux
This library provides a comprehensive Go interface to tmux, offering:
- Complete Session Management
- Create, rename, and delete sessions
- Attach/detach from sessions
- List and query session information
- Group session management
- Session activity monitoring
- Window Operations
- Create and manage windows
- Move windows between sessions
- Set window layouts
- Navigate between windows
- Window layout customization
- Window activity flags
- Pane Control
- Split panes horizontally or vertically
- Resize and rearrange panes
- Capture pane content
- Pane synchronization
- Command execution in panes
- Server & Client Information
- Query server status and version
- List connected clients
- Get terminal information
- Monitor client activity
- Socket management
This library aims to be feature complete with tmux. Currently not all features are supported but they are planned to be implemented. Contributions are welcome.
import (
"fmt"
"log"
// import gotmux
"github.com/GianlucaP106/gotmux/gotmux"
)
func main() {
// construct tmux client with socket path
tmux, err := gotmux.NewTmux("/private/tmp/tmux-501/default")
if err != nil {
log.Fatal(err)
}
// you can also construct a default tmux client
tmux, err = gotmux.DefaultTmux()
if err != nil {
log.Fatal(err)
}
// create a new session
session, err := tmux.New()
if err != nil {
log.Fatal(err)
}
// attach with current terminal (if possible)
err = session.Attach()
if err != nil {
log.Fatal(err)
}
// kill the session
err = session.Kill()
if err != nil {
log.Fatal(err)
}
}
func main() {
tmux, err := gotmux.DefaultTmux()
if err != nil {
log.Fatal(err)
}
// Create a named session with specific directory
session, err := tmux.NewSession(&gotmux.SessionOptions{
StartDirectory: "/home",
Name: "somename",
})
if err != nil {
log.Fatal(err)
}
// Attach to the session
err = session.Attach()
if err != nil {
log.Fatal(err)
}
}
func main() {
tmux, err := gotmux.DefaultTmux()
if err != nil {
log.Fatal(err)
}
session, err := tmux.New()
if err != nil {
log.Fatal(err)
}
// Create and manage windows
window, err := session.New()
if err != nil {
log.Fatal(err)
}
// Get and split panes
pane, err := window.GetPaneByIndex(0)
if err != nil {
log.Fatal(err)
}
err = pane.Split()
if err != nil {
log.Fatal(err)
}
}
func main() {
tmux, err := gotmux.DefaultTmux()
if err != nil {
log.Fatal(err)
}
// List clients
clients, err := tmux.ListClients()
if err != nil {
log.Fatal(err)
}
for _, c := range clients {
fmt.Println(c.Tty, c.Session)
}
// Get server information
server, err := tmux.GetServerInformation()
if err != nil {
log.Fatal(err)
}
fmt.Println(server.Version)
}
func main() {
tmux, err := gotmux.DefaultTmux()
if err != nil {
log.Fatal(err)
}
session, err := tmux.New()
if err != nil {
log.Fatal(err)
}
window, err := session.New()
if err != nil {
log.Fatal(err)
}
// Available layouts:
// - WindowLayoutEvenHorizontal
// - WindowLayoutEvenVertical
// - WindowLayoutMainVertical
// - WindowLayoutTiled
err = window.SelectLayout(gotmux.WindowLayoutEvenVertical)
if err != nil {
log.Fatal(err)
}
}
See the complete list of examples
All types provide comprehensive access to tmux attributes:
type Session struct {
Activity string
Alerts string
Attached int
AttachedList []string
Created string
Format bool
Group string
GroupAttached int
GroupAttachedList []string
GroupList []string
GroupManyAttached bool
GroupSize int
Grouped bool
Id string
LastAttached string
ManyAttached bool
Marked bool
Name string
Path string
Stack string
Windows int
tmux *Tmux
}
New()
- Create a new sessionAttach()
- Attach to a sessionKill()
- Kill a sessionRename()
- Rename a sessionListWindows()
- List windows in a session
New()
- Create a new windowKill()
- Kill a windowMove()
- Move a windowSelectLayout()
- Select window layoutListPanes()
- List panes in a window
Split()
- Split a paneKill()
- Kill a paneCapture()
- Capture pane contentSelect()
- Select a pane
The library uses Go's standard error handling patterns. All operations that can fail return an error as their last return value:
session, err := tmux.New()
if err != nil {
// Handle error
}
This project is licensed under the MIT License - see the LICENSE file for details.