# libtmux.Pane:wait_for_text

- **Module:** libtmux
- **Package:** libtmux
- **Language:** Lua
- **Kind:** method
- **Source:** https://github.com/libtmux/libtmux-lua/blob/4af8884cb1c80854e583701b2b31a02399a4f3f8/lua/libtmux/_internal/entity.lua#L483
- **Page:** https://libtmux.org/en/lua/latest/reference/libtmux-pane-wait_for_text/

```
wait_for_text: fun(
  self: libtmux.Pane,
  text: string,
  options?: libtmux.WaitTextOptions,
):libtmux.Request<libtmux.TextWait>
```

## Examples

Wait for a command's output to appear on the visible screen. 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:

```console
$ luarocks --tree ./rocks install luv &&
  luarocks --tree ./rocks install libtmux &&
  eval "$(luarocks --tree ./rocks path)"
```

Save this complete launcher as `run.sh`. It starts a private tmux daemon and cleans up after success or failure. A failed daemon shutdown reports and retains its socket directory.

[Source example](https://github.com/libtmux/libtmux-lua/blob/4af8884cb1c80854e583701b2b31a02399a4f3f8/examples/api/run.sh).

```sh
#!/bin/sh
set -eu

program=${1:?Pass the complete Lua example filename}
binary=$(command -v "${TMUX_BIN:-tmux}")
case "$binary" in
    /*) ;;
    *) printf '%s\n' 'tmux must resolve to an absolute path' >&2; exit 1 ;;
esac
directory=$(mktemp -d "${TMPDIR:-/tmp}/libtmux-lua-api.XXXXXX")
socket="$directory/tmux.sock"

cleanup() {
    status=$?
    trap - 0 HUP INT TERM
    if [ -S "$socket" ] && ! "$binary" -S "$socket" kill-server; then
        printf 'Cannot stop tmux; kept %s\n' "$directory" >&2
        exit 1
    fi
    rm -rf "$directory" || exit 1
    exit "$status"
}
trap cleanup 0
trap 'exit 1' HUP INT TERM

unset TMUX TMUX_PANE
export TMUX_BIN="$binary" TMUX_SOCKET="$socket" ENV=/dev/null BASH_ENV=/dev/null
"$binary" -S "$socket" -f /dev/null new-session -d -s bootstrap -n bootstrap /bin/cat
"${LUA_BIN:-lua}" "$program"
"$binary" -S "$socket" has-session -t '=bootstrap'
```

Save the complete program as `wait_for_text.lua`. Closing the library connection leaves the daemon running; the launcher owns its lifetime.

[Source example](https://github.com/libtmux/libtmux-lua/blob/4af8884cb1c80854e583701b2b31a02399a4f3f8/examples/api/wait_for_text.lua).

```lua
local adapter = require("libtmux.runtime.luv")

local function must(value, err)
    if err ~= nil then
        error(tostring(err), 0)
    end
    return value
end

local binary = assert(os.getenv("TMUX_BIN"), "set TMUX_BIN to an absolute tmux executable")
local socket = assert(os.getenv("TMUX_SOCKET"), "set TMUX_SOCKET to the private socket")

must(adapter.run(function(runtime)
    local server = must(runtime:connect({ binary = binary, socket_path = socket }):await())
    local created = must(server:new_session({ name = "demo", argv = { "/bin/sh" } }):await())
    local pane = created.pane

    -- The shell echoes what is typed, so print the needle from two pieces.
    must(pane:send_text("printf '%s%s\\n' 'lua wait ' 'ready'"):await())
    must(pane:send_keys({ "Enter" }):await())
    local found = must(pane:wait_for_text("lua wait ready", { timeout = 5000 }):await())
    assert(found.text:find("lua wait ready", 1, true))
    print("lua wait ready")

    must(server:close():await())
    return true
end))
```

Run the saved files. Assertions and operation errors produce a failing exit status:

```console
$ eval "$(luarocks --tree ./rocks path)" &&
  sh run.sh wait_for_text.lua
```

## Parameters

- `text` (`string`)
- `options` (`libtmux.WaitTextOptions?`)

## Guides

Pages that discuss `wait_for_text`.

- [Compatibility targets](/en/lua/latest/guides/compatibility/)
- [Getting started](/en/lua/latest/guides/overview/)
- [Pane operations](/en/lua/latest/guides/panes/)
