# libtmux for Elixir

Source: https://libtmux.org/en/elixir/pr-173/

> Control tmux servers, sessions, windows and panes from Elixir with libtmux-elixir: install, guides, examples and API reference.

![](https://libtmux.org/en/brand/elixir/library/logo.svg)

Elixir-based tmux client

tmux client library for Elixir. Control tmux, navigate sessions, windows, and panes, watch for changes via control mode

```elixir
Mix.install([{:libtmux, "0.1.1"}])


alias LibTmux.{Command, Pane, Ref, Selection, Server, Session, Snapshot, Window}


tmux = System.find_executable("tmux") || raise "tmux is not on PATH"
id = Base.encode16(:crypto.strong_rand_bytes(8), case: :lower)
dir = Path.join(System.tmp_dir!(), "libtmux-" <> id)
socket = Path.join(dir, "tmux.sock")
File.mkdir!(dir)
File.chmod!(dir, 0o700)


# libtmux works on a running server, so tmux itself starts the private one.
create = ["new-session", "-d", "-s", "demo"]
{_, 0} = System.cmd(tmux, ["-S", socket, "-f", "/dev/null" | create])
server = Server.new(executable: tmux, socket: socket)


# Lay out the session: name its window, split it, add a second window.
snapshot = Server.snapshot!(server)
session = snapshot |> Snapshot.sessions() |> Selection.one!()
editor = Session.active_window(snapshot, session)
{:ok, session_ref} = Ref.from_observation(session)
{:ok, editor_ref} = Ref.from_observation(editor)
Window.rename!(server, editor_ref, "editor")
left =
  snapshot
  |> Snapshot.panes()
  |> Selection.one!(window_id: editor.id, active: true)
{:ok, left_ref} = Ref.from_observation(left)
%{id: right_id} = Pane.split!(server, left_ref, axis: :horizontal)
%{id: logs_id} = Session.new_window!(server, session_ref, "logs")
{:ok, right} = Ref.new(:pane, right_id, session.server_instance)


# Type a command into the new pane and wait for what it prints.
Pane.send_text!(server, right, "printf 'hello from %s\\n' tmux")
Pane.send_keys!(server, right, ["Enter"])


shown =
  Enum.find_value(1..250, fn _ ->
    screen = Pane.capture!(server, right)
    screen =~ "hello from tmux" || (Process.sleep(20) && nil)
  end)


shown || raise "the pane did not print hello from tmux"


# Move the focus an attached client sees: the pane, then the window.
select_pane = Command.new("select-pane", ["-t", right_id])
select_window = Command.new("select-window", ["-t", logs_id])
{:ok, _} = Server.run(server, select_pane)
{:ok, _} = Server.run(server, select_window)


# Walk the objects: down from the server, then up from one pane.
snapshot = Server.snapshot!(server)
sessions = Snapshot.sessions(snapshot)


for each <- sessions do
  IO.puts("session #{each.name}")
  links = Snapshot.related(snapshot, :session, each.id, :window_links)


  for link <- Enum.sort_by(links, & &1.index) do
    {:ok, window} = Snapshot.fetch(snapshot, :window, link.window_id)
    panes = Window.panes(snapshot, window)
    ids = Enum.map_join(panes, " ", & &1.id)
    IO.puts("  window #{link.index}:#{window.name}: #{ids}")
  end
end


pane = snapshot |> Snapshot.panes() |> Selection.one!(id: right_id)
window = Snapshot.related(snapshot, :pane, pane.id, :window)
[owner] = Snapshot.related(snapshot, :window, window.id, :sessions)
IO.puts("#{pane.id} is in window #{window.name} of session #{owner.name}")
```

```elixir
Mix.install([{:libtmux, "0.1.1"}])


alias LibTmux.{Command, Pane, Ref, Selection, Server, Session, Snapshot, Window}


tmux = System.find_executable("tmux") || raise "tmux is not on PATH"
id = Base.encode16(:crypto.strong_rand_bytes(8), case: :lower)
dir = Path.join(System.tmp_dir!(), "libtmux-" <> id)
socket = Path.join(dir, "tmux.sock")
File.mkdir!(dir)
File.chmod!(dir, 0o700)


# libtmux works on a running server, so tmux itself starts the private one.
create = ["new-session", "-d", "-s", "demo"]
{_, 0} = System.cmd(tmux, ["-S", socket, "-f", "/dev/null" | create])
server = Server.new(executable: tmux, socket: socket)


# Lay out the session: name its window, split it, add a second window.
snapshot = Server.snapshot!(server)
session = snapshot |> Snapshot.sessions() |> Selection.one!()
editor = Session.active_window(snapshot, session)
{:ok, session_ref} = Ref.from_observation(session)
{:ok, editor_ref} = Ref.from_observation(editor)
Window.rename!(server, editor_ref, "editor")
left =
  snapshot
  |> Snapshot.panes()
  |> Selection.one!(window_id: editor.id, active: true)
{:ok, left_ref} = Ref.from_observation(left)
%{id: right_id} = Pane.split!(server, left_ref, axis: :horizontal)
%{id: logs_id} = Session.new_window!(server, session_ref, "logs")
{:ok, right} = Ref.new(:pane, right_id, session.server_instance)


# Type a command into the new pane and wait for what it prints.
Pane.send_text!(server, right, "printf 'hello from %s\\n' tmux")
Pane.send_keys!(server, right, ["Enter"])


shown =
  Enum.find_value(1..250, fn _ ->
    screen = Pane.capture!(server, right)
    screen =~ "hello from tmux" || (Process.sleep(20) && nil)
  end)


shown || raise "the pane did not print hello from tmux"


# Move the focus an attached client sees: the pane, then the window.
select_pane = Command.new("select-pane", ["-t", right_id])
select_window = Command.new("select-window", ["-t", logs_id])
{:ok, _} = Server.run(server, select_pane)
{:ok, _} = Server.run(server, select_window)


# Walk the objects: down from the server, then up from one pane.
snapshot = Server.snapshot!(server)
sessions = Snapshot.sessions(snapshot)


for each <- sessions do
  IO.puts("session #{each.name}")
  links = Snapshot.related(snapshot, :session, each.id, :window_links)


  for link <- Enum.sort_by(links, & &1.index) do
    {:ok, window} = Snapshot.fetch(snapshot, :window, link.window_id)
    panes = Window.panes(snapshot, window)
    ids = Enum.map_join(panes, " ", & &1.id)
    IO.puts("  window #{link.index}:#{window.name}: #{ids}")
  end
end


pane = snapshot |> Snapshot.panes() |> Selection.one!(id: right_id)
window = Snapshot.related(snapshot, :pane, pane.id, :window)
[owner] = Snapshot.related(snapshot, :window, window.id, :sessions)
IO.puts("#{pane.id} is in window #{window.name} of session #{owner.name}")
```

```elixir
Mix.install([{:libtmux, "0.1.1"}])


alias LibTmux.{Command, Pane, Ref, Selection, Server, Session, Snapshot, Window}


tmux = System.find_executable("tmux") || raise "tmux is not on PATH"
id = Base.encode16(:crypto.strong_rand_bytes(8), case: :lower)
dir = Path.join(System.tmp_dir!(), "libtmux-" <> id)
socket = Path.join(dir, "tmux.sock")
File.mkdir!(dir)
File.chmod!(dir, 0o700)


try do
  # libtmux works on a running server, so tmux itself starts the private one.
  create = ["new-session", "-d", "-s", "demo"]
  {_, 0} = System.cmd(tmux, ["-S", socket, "-f", "/dev/null" | create])
  server = Server.new(executable: tmux, socket: socket)


  # Lay out the session: name its window, split it, add a second window.
  snapshot = Server.snapshot!(server)
  session = snapshot |> Snapshot.sessions() |> Selection.one!()
  editor = Session.active_window(snapshot, session)
  {:ok, session_ref} = Ref.from_observation(session)
  {:ok, editor_ref} = Ref.from_observation(editor)
  Window.rename!(server, editor_ref, "editor")
  left =
    snapshot
    |> Snapshot.panes()
    |> Selection.one!(window_id: editor.id, active: true)
  {:ok, left_ref} = Ref.from_observation(left)
  %{id: right_id} = Pane.split!(server, left_ref, axis: :horizontal)
  %{id: logs_id} = Session.new_window!(server, session_ref, "logs")
  {:ok, right} = Ref.new(:pane, right_id, session.server_instance)


  # Type a command into the new pane and wait for what it prints.
  Pane.send_text!(server, right, "printf 'hello from %s\\n' tmux")
  Pane.send_keys!(server, right, ["Enter"])


  shown =
    Enum.find_value(1..250, fn _ ->
      screen = Pane.capture!(server, right)
      screen =~ "hello from tmux" || (Process.sleep(20) && nil)
    end)


  shown || raise "the pane did not print hello from tmux"


  # Move the focus an attached client sees: the pane, then the window.
  select_pane = Command.new("select-pane", ["-t", right_id])
  select_window = Command.new("select-window", ["-t", logs_id])
  {:ok, _} = Server.run(server, select_pane)
  {:ok, _} = Server.run(server, select_window)


  # Walk the objects: down from the server, then up from one pane.
  snapshot = Server.snapshot!(server)
  sessions = Snapshot.sessions(snapshot)


  for each <- sessions do
    IO.puts("session #{each.name}")
    links = Snapshot.related(snapshot, :session, each.id, :window_links)


    for link <- Enum.sort_by(links, & &1.index) do
      {:ok, window} = Snapshot.fetch(snapshot, :window, link.window_id)
      panes = Window.panes(snapshot, window)
      ids = Enum.map_join(panes, " ", & &1.id)
      IO.puts("  window #{link.index}:#{window.name}: #{ids}")
    end
  end


  pane = snapshot |> Snapshot.panes() |> Selection.one!(id: right_id)
  window = Snapshot.related(snapshot, :pane, pane.id, :window)
  [owner] = Snapshot.related(snapshot, :window, window.id, :sessions)
  IO.puts("#{pane.id} is in window #{window.name} of session #{owner.name}")
after
  System.cmd(tmux, ["-S", socket, "kill-server"], stderr_to_stdout: true)
  File.rm_rf(dir)
end
```

```elixir
Mix.install([{:libtmux, "0.1.1"}])


alias LibTmux.{Command, Pane, Ref, Selection, Server, Session, Snapshot, Window}


tmux = System.find_executable("tmux") || raise "tmux is not on PATH"
id = Base.encode16(:crypto.strong_rand_bytes(8), case: :lower)
dir = Path.join(System.tmp_dir!(), "libtmux-" <> id)
socket = Path.join(dir, "tmux.sock")
File.mkdir!(dir)
File.chmod!(dir, 0o700)


try do
  # libtmux works on a running server, so tmux itself starts the private one.
  create = ["new-session", "-d", "-s", "demo"]
  {_, 0} = System.cmd(tmux, ["-S", socket, "-f", "/dev/null" | create])
  server = Server.new(executable: tmux, socket: socket)


  # Lay out the session: name its window, split it, add a second window.
  snapshot = Server.snapshot!(server)
  session = snapshot |> Snapshot.sessions() |> Selection.one!()
  editor = Session.active_window(snapshot, session)
  {:ok, session_ref} = Ref.from_observation(session)
  {:ok, editor_ref} = Ref.from_observation(editor)
  Window.rename!(server, editor_ref, "editor")
  left =
    snapshot
    |> Snapshot.panes()
    |> Selection.one!(window_id: editor.id, active: true)
  {:ok, left_ref} = Ref.from_observation(left)
  %{id: right_id} = Pane.split!(server, left_ref, axis: :horizontal)
  %{id: logs_id} = Session.new_window!(server, session_ref, "logs")
  {:ok, right} = Ref.new(:pane, right_id, session.server_instance)


  # Type a command into the new pane and wait for what it prints.
  Pane.send_text!(server, right, "printf 'hello from %s\\n' tmux")
  Pane.send_keys!(server, right, ["Enter"])


  shown =
    Enum.find_value(1..250, fn _ ->
      screen = Pane.capture!(server, right)
      screen =~ "hello from tmux" || (Process.sleep(20) && nil)
    end)


  shown || raise "the pane did not print hello from tmux"


  # Move the focus an attached client sees: the pane, then the window.
  select_pane = Command.new("select-pane", ["-t", right_id])
  select_window = Command.new("select-window", ["-t", logs_id])
  {:ok, _} = Server.run(server, select_pane)
  {:ok, _} = Server.run(server, select_window)


  # Walk the objects: down from the server, then up from one pane.
  snapshot = Server.snapshot!(server)
  sessions = Snapshot.sessions(snapshot)


  for each <- sessions do
    IO.puts("session #{each.name}")
    links = Snapshot.related(snapshot, :session, each.id, :window_links)


    for link <- Enum.sort_by(links, & &1.index) do
      {:ok, window} = Snapshot.fetch(snapshot, :window, link.window_id)
      panes = Window.panes(snapshot, window)
      ids = Enum.map_join(panes, " ", & &1.id)
      IO.puts("  window #{link.index}:#{window.name}: #{ids}")
    end
  end


  pane = snapshot |> Snapshot.panes() |> Selection.one!(id: right_id)
  window = Snapshot.related(snapshot, :pane, pane.id, :window)
  [owner] = Snapshot.related(snapshot, :window, window.id, :sessions)
  IO.puts("#{pane.id} is in window #{window.name} of session #{owner.name}")
after
  System.cmd(tmux, ["-S", socket, "kill-server"], stderr_to_stdout: true)
  File.rm_rf(dir)
end
```

Example details

\+ Error handling + Cleanup

No tmux server yet

tmux before the first call: no server is running.

%0 sh

\[demo]

tmux after `System.cmd`: added session demo.

%0 sh

\[demo]

tmux after [`Server.new`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-server-new-1/): unchanged, the call sent no command.

%0 sh

\[demo]

tmux after [`Server.snapshot!`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-server-snapshot-bang-2/): unchanged.

%0 sh

\[demo]

tmux after [`Selection.one!`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-selection-one-bang-2/): unchanged, the call sent no command.

%0 sh

\[demo]

tmux after [`Session.active_window`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-session-active_window-2/): unchanged, the call sent no command.

%0 sh

\[demo]

tmux after [`Ref.from_observation`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-ref-from_observation-1/): unchanged, the call sent no command.

%0 sh

\[demo]

tmux after [`Ref.from_observation`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-ref-from_observation-1/): unchanged, the call sent no command.

%0 sh

\[demo]

tmux after [`Window.rename!`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-window-rename-bang-4/): renamed window 0:sh to 0:editor.

%0 sh

\[demo]

tmux after [`Selection.one!`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-selection-one-bang-2/): unchanged, the call sent no command.

%0 sh

\[demo]

tmux after [`Ref.from_observation`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-ref-from_observation-1/): unchanged, the call sent no command.

%0 sh

%1 sh

\[demo]

tmux after [`Pane.split!`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-pane-split-bang-3/): added pane %1 to window 0:editor.

%0 sh

%1 sh

\[demo]

tmux after [`Session.new_window!`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-session-new_window-bang-4/): added window 1:logs.

%0 sh

%1 sh

\[demo]

tmux after [`Ref.new`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-ref-new-3/): unchanged, the call sent no command.

%0 sh

%1 sh

\[demo]

tmux after [`Pane.send_text!`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-pane-send_text-bang-4/): sent keys to pane %1.

%0 sh

%1 sh

\[demo]

tmux after [`Pane.send_keys!`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-pane-send_keys-bang-4/): sent keys to pane %1.

%0 sh

%1 sh

```
$ printf 'hello from %s\n' tmux
hello from tmux
$
```

\[demo]

tmux after [`Pane.capture!`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-pane-capture-bang-3/): read pane %1.

%0 sh

%1 sh

```
$ printf 'hello from %s\n' tmux
hello from tmux
$
```

\[demo]

tmux after [`Command.new`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-command-new-3/): unchanged, the call sent no command.

%0 sh

%1 sh

```
$ printf 'hello from %s\n' tmux
hello from tmux
$
```

\[demo]

tmux after [`Command.new`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-command-new-3/): unchanged, the call sent no command.

%0 sh

%1 sh

```
$ printf 'hello from %s\n' tmux
hello from tmux
$
```

\[demo]

tmux after [`Server.run`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-server-run-3/): selected pane %1 in window 0:editor.

%2 sh

\[demo]

tmux after [`Server.run`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-server-run-3/): selected window 1:logs.

%2 sh

\[demo]

tmux after [`Server.snapshot!`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-server-snapshot-bang-2/): unchanged.

%2 sh

\[demo]

tmux after [`Snapshot.sessions`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-snapshot-sessions/): unchanged, the call sent no command.

%2 sh

\[demo]

tmux after [`Snapshot.related`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-snapshot-related-4/): unchanged, the call sent no command.

%2 sh

\[demo]

tmux after [`Window.panes`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-window-panes-2/): unchanged, the call sent no command.

%2 sh

\[demo]

tmux after [`Window.panes`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-window-panes-2/): unchanged, the call sent no command.

%2 sh

\[demo]

tmux after [`Selection.one!`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-selection-one-bang-2/): unchanged, the call sent no command.

%2 sh

\[demo]

tmux after [`Snapshot.related`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-snapshot-related-4/): unchanged, the call sent no command.

%2 sh

\[demo]

tmux after [`Snapshot.related`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-snapshot-related-4/): unchanged, the call sent no command.

No sessions

tmux after `System.cmd`: the server has exited.

1. Before the first call

   No tmux server is running yet.

2. `System.cmd` returned `exit status`

   ```json
   {
     "status": 0
   }
   ```

   1 tmux command, 1 that changes tmux

   1. `new-session -d -s demo` no output

3. [`Server.new`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-server-new-1/) returned `Server`

   ```json
   {
     "socket": "/tmp/tmp/libtmux-0123456789abcdef/tmux.sock",
     "executable": "/usr/bin/tmux"
   }
   ```

   The call sent no tmux command.

4. [`Server.snapshot!`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-server-snapshot-bang-2/) returned `Snapshot`

   ```json
   {
     "windows": 1,
     "sessions": 1,
     "panes": 1
   }
   ```

   11 tmux commands

   1. `list-sessions -F '#{session_id} #{session_attached}'` $0 0
   2. `list-windows -a -F '#{session_id} #{window_index} #{window_id} #{window_active} #{window_width} #{window_height} #{window_zoomed_flag}'` $0 0 @0 1 80 24 0
   3. `list-panes -a -F '#{pane_id} #{window_id} #{pane_index} #{pane_active} #{pane_dead} #{pane_width} #{pane_height} #{pane_pid} #{pane_dead_status}'` %0 @0 0 1 0 80 24 3471676
   4. `list-clients -F 'v1:#{s|\\|\\134|;s|\$|\\044|;s||\\001|;s||\\002|;s||\\003|;s||\\004|;s||\\005|;s||\\006|;s||\\007|;s||\\010|;s| |\\011|;s| |\\012|;s| |\\013|;s| |\\014|;s| |\\015|;s||\\016|;s||\\017|;s||\\020|;s||\\021|;s||\\022|;s||\\023|;s||\\024|;s||\\025|;s||\\026|;s||\\027|;s||\\030|;s||\\031|;s||\\032|;s||\\033|;s||\\034|;s||\\035|;s||\\036|;s||\\037|;s||\\177|:cl…` no output
   5. `list-sessions -F 'r1:2 v1:#{s|\\|\\134|;s|\$|\\044|;s||\\001|;s||\\002|;s||\\003|;s||\\004|;s||\\005|;s||\\006|;s||\\007|;s||\\010|;s| |\\011|;s| |\\012|;s| |\\013|;s| |\\014|;s| |\\015|;s||\\016|;s||\\017|;s||\\020|;s||\\021|;s||\\022|;s||\\023|;s||\\024|;s||\\025|;s||\\026|;s||\\027|;s||\\030|;s||\\031|;s||\\032|;s||\\033|;s||\\034|;s||\\035|;s||\\036|;s||\\037|;s||\\1…` r1:2 v1:\0440 v1:demo
   6. `list-panes -a -F 'r1:4 v1:#{s|\\|\\134|;s|\$|\\044|;s||\\001|;s||\\002|;s||\\003|;s||\\004|;s||\\005|;s||\\006|;s||\\007|;s||\\010|;s| |\\011|;s| |\\012|;s| |\\013|;s| |\\014|;s| |\\015|;s||\\016|;s||\\017|;s||\\020|;s||\\021|;s||\\022|;s||\\023|;s||\\024|;s||\\025|;s||\\026|;s||\\027|;s||\\030|;s||\\031|;s||\\032|;s||\\033|;s||\\034|;s||\\035|;s||\\036|;s||\\037|;s||\\1…` r1:4 v1:%0 v1:sh v1:/tmp/marked v1:host
   7. `list-windows -a -F 'r1:3 v1:#{s|\\|\\134|;s|\$|\\044|;s||\\001|;s||\\002|;s||\\003|;s||\\004|;s||\\005|;s||\\006|;s||\\007|;s||\\010|;s| |\\011|;s| |\\012|;s| |\\013|;s| |\\014|;s| |\\015|;s||\\016|;s||\\017|;s||\\020|;s||\\021|;s||\\022|;s||\\023|;s||\\024|;s||\\025|;s||\\026|;s||\\027|;s||\\030|;s||\\031|;s||\\032|;s||\\033|;s||\\034|;s||\\035|;s||\\036|;s||\\037|;s||\…` r1:3 v1:@0 v1:sh v1:{"V":2,"L":{"t":"p","w":80,"h":24,"x":0,"y":0,"a":true,"i":0,"I":"%0"}}
   8. `list-sessions -F '#{session_id} #{session_attached}'` $0 0
   9. `list-windows -a -F '#{session_id} #{window_index} #{window_id} #{window_active} #{window_width} #{window_height} #{window_zoomed_flag}'` $0 0 @0 1 80 24 0
   10. `list-panes -a -F '#{pane_id} #{window_id} #{pane_index} #{pane_active} #{pane_dead} #{pane_width} #{pane_height} #{pane_pid} #{pane_dead_status}'` %0 @0 0 1 0 80 24 3471676
   11. `list-clients -F 'v1:#{s|\\|\\134|;s|\$|\\044|;s||\\001|;s||\\002|;s||\\003|;s||\\004|;s||\\005|;s||\\006|;s||\\007|;s||\\010|;s| |\\011|;s| |\\012|;s| |\\013|;s| |\\014|;s| |\\015|;s||\\016|;s||\\017|;s||\\020|;s||\\021|;s||\\022|;s||\\023|;s||\\024|;s||\\025|;s||\\026|;s||\\027|;s||\\030|;s||\\031|;s||\\032|;s||\\033|;s||\\034|;s||\\035|;s||\\036|;s||\\037|;s||\\177|:cl…` no output

   Before these, the library asked tmux about itself 2 times (its version, its daemon, how a control client reads escapes).

5. [`Selection.one!`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-selection-one-bang-2/) returned `Session`

   ```json
   {
     "id": "$0",
     "name": "demo"
   }
   ```

   The call sent no tmux command.

6. [`Session.active_window`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-session-active_window-2/) returned `Window`

   ```json
   {
     "id": "@0",
     "name": "sh"
   }
   ```

   The call sent no tmux command.

7. [`Ref.from_observation`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-ref-from_observation-1/) returned `Ref`

   ```json
   {
     "id": "$0",
     "entity": "session"
   }
   ```

   The call sent no tmux command.

8. [`Ref.from_observation`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-ref-from_observation-1/) returned `Ref`

   ```json
   {
     "id": "@0",
     "entity": "window"
   }
   ```

   The call sent no tmux command.

9. [`Window.rename!`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-window-rename-bang-4/) returned `:ok`

   ```json
   "ok"
   ```

   1 tmux command, 1 that changes tmux

   1. `rename-window -t @0 -- editor` no output

   Before these, the library asked tmux about itself 1 time (its version, its daemon, how a control client reads escapes).

10. [`Selection.one!`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-selection-one-bang-2/) returned `Pane`

    ```json
    {
      "id": "%0"
    }
    ```

    The call sent no tmux command.

11. [`Ref.from_observation`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-ref-from_observation-1/) returned `Ref`

    ```json
    {
      "id": "%0",
      "entity": "pane"
    }
    ```

    The call sent no tmux command.

12. [`Pane.split!`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-pane-split-bang-3/) returned `map`

    ```json
    {
      "id": "%1"
    }
    ```

    1 tmux command, 1 that changes tmux

    1. `split-window -t %0 -P -F #{pane_id} -h -d --` %1

    Before these, the library asked tmux about itself 1 time (its version, its daemon, how a control client reads escapes).

13. [`Session.new_window!`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-session-new_window-bang-4/) returned `map`

    ```json
    {
      "id": "@1"
    }
    ```

    1 tmux command, 1 that changes tmux

    1. `new-window -d -P -F #{window_id} -t $0: -n logs --` @1

    Before these, the library asked tmux about itself 1 time (its version, its daemon, how a control client reads escapes).

14. [`Ref.new`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-ref-new-3/) returned `Ref`

    ```json
    {
      "id": "%1",
      "entity": "pane"
    }
    ```

    The call sent no tmux command.

15. [`Pane.send_text!`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-pane-send_text-bang-4/) returned `:ok`

    ```json
    "ok"
    ```

    1 tmux command, 1 that changes tmux

    1. `send-keys -H -t %1 -- 70 72 69 6E 74 66 20 27 68 65 6C 6C 6F 20 66 72 6F 6D 20 25 73 5C 6E 27 20 74 6D 75 78` no output

    Before these, the library asked tmux about itself 1 time (its version, its daemon, how a control client reads escapes).

16. [`Pane.send_keys!`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-pane-send_keys-bang-4/) returned `:ok`

    ```json
    "ok"
    ```

    1 tmux command, 1 that changes tmux

    1. `send-keys -t %1 -- Enter` no output

    Before these, the library asked tmux about itself 1 time (its version, its daemon, how a control client reads escapes).

17. [`Pane.capture!`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-pane-capture-bang-3/) returned `binary`

    ```json
    "$ printf 'hello from %s\\n' tmux\nhello from tmux\n$\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
    ```

    1 tmux command

    1. `capture-pane -p -t %1 -S 0 -E -` $ printf 'hello from %s\n' tmux hello from tmux $

    Before these, the library asked tmux about itself 1 time (its version, its daemon, how a control client reads escapes).

18. [`Command.new`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-command-new-3/) returned `Command`

    ```json
    {
      "args": [
        "-t",
        "%1"
      ],
      "name": "select-pane"
    }
    ```

    The call sent no tmux command.

19. [`Command.new`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-command-new-3/) returned `Command`

    ```json
    {
      "args": [
        "-t",
        "@1"
      ],
      "name": "select-window"
    }
    ```

    The call sent no tmux command.

20. [`Server.run`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-server-run-3/) returned `Result`

    ```json
    {
      "exit_status": 0,
      "stdout": "",
      "stderr": ""
    }
    ```

    1 tmux command, 1 that changes tmux

    1. `select-pane -t %1` no output

21. [`Server.run`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-server-run-3/) returned `Result`

    ```json
    {
      "exit_status": 0,
      "stdout": "",
      "stderr": ""
    }
    ```

    1 tmux command, 1 that changes tmux

    1. `select-window -t @1` no output

22. [`Server.snapshot!`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-server-snapshot-bang-2/) returned `Snapshot`

    ```json
    {
      "windows": 2,
      "sessions": 1,
      "panes": 3
    }
    ```

    11 tmux commands

    1. `list-sessions -F '#{session_id} #{session_attached}'` $0 0
    2. `list-windows -a -F '#{session_id} #{window_index} #{window_id} #{window_active} #{window_width} #{window_height} #{window_zoomed_flag}'` $0 0 @0 0 80 24 0 $0 1 @1 1 80 24 0
    3. `list-panes -a -F '#{pane_id} #{window_id} #{pane_index} #{pane_active} #{pane_dead} #{pane_width} #{pane_height} #{pane_pid} #{pane_dead_status}'` %0 @0 0 0 0 40 24 3471676 %1 @0 1 1 0 39 24 3472292 %2 @1 0 1 0 80 24 3472410
    4. `list-clients -F 'v1:#{s|\\|\\134|;s|\$|\\044|;s||\\001|;s||\\002|;s||\\003|;s||\\004|;s||\\005|;s||\\006|;s||\\007|;s||\\010|;s| |\\011|;s| |\\012|;s| |\\013|;s| |\\014|;s| |\\015|;s||\\016|;s||\\017|;s||\\020|;s||\\021|;s||\\022|;s||\\023|;s||\\024|;s||\\025|;s||\\026|;s||\\027|;s||\\030|;s||\\031|;s||\\032|;s||\\033|;s||\\034|;s||\\035|;s||\\036|;s||\\037|;s||\\177|:cl…` no output
    5. `list-sessions -F 'r1:2 v1:#{s|\\|\\134|;s|\$|\\044|;s||\\001|;s||\\002|;s||\\003|;s||\\004|;s||\\005|;s||\\006|;s||\\007|;s||\\010|;s| |\\011|;s| |\\012|;s| |\\013|;s| |\\014|;s| |\\015|;s||\\016|;s||\\017|;s||\\020|;s||\\021|;s||\\022|;s||\\023|;s||\\024|;s||\\025|;s||\\026|;s||\\027|;s||\\030|;s||\\031|;s||\\032|;s||\\033|;s||\\034|;s||\\035|;s||\\036|;s||\\037|;s||\\1…` r1:2 v1:\0440 v1:demo
    6. `list-panes -a -F 'r1:4 v1:#{s|\\|\\134|;s|\$|\\044|;s||\\001|;s||\\002|;s||\\003|;s||\\004|;s||\\005|;s||\\006|;s||\\007|;s||\\010|;s| |\\011|;s| |\\012|;s| |\\013|;s| |\\014|;s| |\\015|;s||\\016|;s||\\017|;s||\\020|;s||\\021|;s||\\022|;s||\\023|;s||\\024|;s||\\025|;s||\\026|;s||\\027|;s||\\030|;s||\\031|;s||\\032|;s||\\033|;s||\\034|;s||\\035|;s||\\036|;s||\\037|;s||\\1…` r1:4 v1:%0 v1:sh v1:/tmp/marked v1:host r1:4 v1:%1 v1:sh v1:/tmp/marked v1:host r1:4 v1:%2 v1:sh v1:/tmp/marked v1:host
    7. `list-windows -a -F 'r1:3 v1:#{s|\\|\\134|;s|\$|\\044|;s||\\001|;s||\\002|;s||\\003|;s||\\004|;s||\\005|;s||\\006|;s||\\007|;s||\\010|;s| |\\011|;s| |\\012|;s| |\\013|;s| |\\014|;s| |\\015|;s||\\016|;s||\\017|;s||\\020|;s||\\021|;s||\\022|;s||\\023|;s||\\024|;s||\\025|;s||\\026|;s||\\027|;s||\\030|;s||\\031|;s||\\032|;s||\\033|;s||\\034|;s||\\035|;s||\\036|;s||\\037|;s||\…` r1:3 v1:@0 v1:editor v1:{"V":2,"L":{"t":"h","w":80,"h":24,"x":0,"y":0,"c":\[{"t":"p","w":40,"h":24,"x":0,"y":0,"l":0,"i":0,"I":"%0"},{"t":"p","w":39,"h":24,"x":41,"y":0,"a":true,"i":1,"I":"%1"}]}} r1:3 v1:@1 v1:logs v1:{"V":2,"L":{"t":"p","w":80,"h":24,"x":0,"y":0,"a":true,"i":0,"I":"%2"}}
    8. `list-sessions -F '#{session_id} #{session_attached}'` $0 0
    9. `list-windows -a -F '#{session_id} #{window_index} #{window_id} #{window_active} #{window_width} #{window_height} #{window_zoomed_flag}'` $0 0 @0 0 80 24 0 $0 1 @1 1 80 24 0
    10. `list-panes -a -F '#{pane_id} #{window_id} #{pane_index} #{pane_active} #{pane_dead} #{pane_width} #{pane_height} #{pane_pid} #{pane_dead_status}'` %0 @0 0 0 0 40 24 3471676 %1 @0 1 1 0 39 24 3472292 %2 @1 0 1 0 80 24 3472410
    11. `list-clients -F 'v1:#{s|\\|\\134|;s|\$|\\044|;s||\\001|;s||\\002|;s||\\003|;s||\\004|;s||\\005|;s||\\006|;s||\\007|;s||\\010|;s| |\\011|;s| |\\012|;s| |\\013|;s| |\\014|;s| |\\015|;s||\\016|;s||\\017|;s||\\020|;s||\\021|;s||\\022|;s||\\023|;s||\\024|;s||\\025|;s||\\026|;s||\\027|;s||\\030|;s||\\031|;s||\\032|;s||\\033|;s||\\034|;s||\\035|;s||\\036|;s||\\037|;s||\\177|:cl…` no output

    Before these, the library asked tmux about itself 2 times (its version, its daemon, how a control client reads escapes).

23. [`Snapshot.sessions`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-snapshot-sessions/) returned `Selection`

    ```json
    [
      {
        "id": "$0",
        "name": "demo"
      }
    ]
    ```

    The call sent no tmux command.

24. [`Snapshot.related`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-snapshot-related-4/) returned `[WindowLink]`

    ```json
    [
      {
        "index": 0,
        "window": "@0"
      },
      {
        "index": 1,
        "window": "@1"
      }
    ]
    ```

    The call sent no tmux command.

25. [`Window.panes`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-window-panes-2/) returned `Selection`

    ```json
    [
      "%0",
      "%1"
    ]
    ```

    The call sent no tmux command.

26. [`Window.panes`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-window-panes-2/) returned `Selection`

    ```json
    [
      "%2"
    ]
    ```

    The call sent no tmux command.

27. [`Selection.one!`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-selection-one-bang-2/) returned `Pane`

    ```json
    {
      "id": "%1"
    }
    ```

    The call sent no tmux command.

28. [`Snapshot.related`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-snapshot-related-4/) returned `Window`

    ```json
    {
      "id": "@0",
      "name": "editor"
    }
    ```

    The call sent no tmux command.

29. [`Snapshot.related`](https://libtmux.org/en/elixir/pr-173/reference/libtmux-snapshot-related-4/) returned `[Session]`

    ```json
    {
      "id": "$0",
      "name": "demo"
    }
    ```

    The call sent no tmux command.

30. `System.cmd` returned `exit status`

    ```json
    {
      "status": 0
    }
    ```

    1 tmux command, 1 that changes tmux

    1. `kill-server` no output

1/28

Run this example

POSIX shell, tmux 3.3 or newer, Elixir 1.20.4 or newer, Erlang/OTP 29.1.2 or a newer 29.x release, access to hex.pm, and a C11 compiler on Linux.

Save the program above as `quickstart.exs` in a new directory, along with the files below.

Cleanup is omitted; the private tmux server and its temporary files may remain. Select Cleanup to remove them when the program finishes. The program stops its private tmux server and removes any temporary files it created when it finishes. An unmatched result or a raised error ends the script with its message and stack trace, so there is no separate error handling to add. Every version checks required results.

### Setup and run

```bash
unset TMUX TMUX_PANE
export ENV=/dev/null BASH_ENV=/dev/null SHELL=/bin/sh
elixir quickstart.exs
```

### Expected output

```text
session demo
  window 0:editor: %0 %1
  window 1:logs: %2
%1 is in window editor of session demo
```

[Core Library documentation →](https://libtmux.org/en/elixir/pr-173/core/)[More Elixir examples →](https://libtmux.org/en/elixir/pr-173/examples/)

### Install `libtmux`

mix.exsFrom Git

mix.exs

mix.exs

`libtmux` 0.1.1 0.x [Hex](https://hex.pm/packages/libtmux)

```elixir
{:libtmux, "~> 0.1.1"}
```

Then

```console
$ mix deps.get
```

```elixir
{:libtmux, github: "libtmux/libtmux-elixir", tag: "v0.1.1"}
```

Add the tuple to the deps list in mix.exs, then fetch it. Needs Elixir 1.20.4 or newer, Erlang/OTP 29.1.2 or a newer 29.x release, tmux 3.3 or newer (the planned floor), and a C11 compiler on Linux.

Pins the tagged source; the registry release is the default.

### [Topics](https://libtmux.org/en/elixir/pr-173/topics/)

[Subject-by-subject, with Elixir in every code block.](https://libtmux.org/en/elixir/pr-173/topics/)

### [Guides](https://libtmux.org/en/elixir/pr-173/guides/)

[Task guides for Elixir.](https://libtmux.org/en/elixir/pr-173/guides/)

### [Examples](https://libtmux.org/en/elixir/pr-173/examples/)

[Complete programs, sourced from the code each port tests.](https://libtmux.org/en/elixir/pr-173/examples/)

### [Concepts](https://libtmux.org/en/elixir/pr-173/concepts/)

[The domain model every port shares.](https://libtmux.org/en/elixir/pr-173/concepts/)

## API references

### [Official reference](https://libtmux.org/en/elixir/pr-173/reference/)

[Elixir API signatures and documentation, with links to related guides and APIs in other languages.](https://libtmux.org/en/elixir/pr-173/reference/)

### [HexDocs documentation (opens in a new tab)](https://libtmux.hexdocs.pm/)

[ExDoc API reference and guides for published package versions on HexDocs.](https://libtmux.hexdocs.pm/)
