# Environment

Source: https://libtmux.org/en/go/latest/topics/environment/

> Locate tmux objects from process variables and manage the environment inherited by new panes.

tmux exposes two environment APIs. Process variables such as `TMUX` and
`TMUX_PANE` let code inside a pane identify its server and pane. The server also
stores variables through [`set-environment`](https://libtmux.org/en/tmux/latest/manual/set-environment/) and [`show-environment`](https://libtmux.org/en/tmux/latest/manual/show-environment/) for new
processes to inherit. Like the tables in [Options and
hooks](https://libtmux.org/en/go/latest/topics/options-and-hooks/), this persistent store has explicit scopes.

## Locating yourself from inside a pane

Inside a pane, `TMUX` contains `<socket path>,<server pid>,<session id>`, and
`TMUX_PANE` contains the pane ID, such as `%1`. Use these variables to locate
the current tmux objects. Select the object your operation needs:

**Server:** [`NewServerFromEnv(env)`](<https://libtmux.org/en/go/latest/reference/tmux-newserverfromenv/>)

**Session:** [`SessionFromEnv(ctx, env)`](<https://libtmux.org/en/go/latest/reference/tmux-sessionfromenv/>)

**Window:** [`WindowFromEnv(ctx, env)`](<https://libtmux.org/en/go/latest/reference/tmux-windowfromenv/>)

**Pane:** [`PaneFromEnv(ctx, env)`](<https://libtmux.org/en/go/latest/reference/tmux-panefromenv/>)

### Examples

```go
pane, err := tmux.PaneFromEnv(ctx, nil)
if err != nil {
    return err
}
fmt.Println("current pane:", pane.ID())
```

Environment lookup requires valid tmux targeting variables. If your process
can run outside tmux, handle that case before using the result.

Check the returned [`error`](<https://pkg.go.dev/builtin#error>). A [`FromEnvError`](<https://libtmux.org/en/go/latest/reference/tmux-fromenverror/>) identifies a missing or malformed
variable. Passing `nil` reads the process environment; pass an explicit map to
resolve a captured environment.

## tmux's own environment variable store

Like [Options and hooks](https://libtmux.org/en/go/latest/topics/options-and-hooks/),
tmux's persistent environment store has global and per-session scopes. It is
read with [`show-environment`](https://libtmux.org/en/tmux/latest/manual/show-environment/) and updated with [`set-environment`](https://libtmux.org/en/tmux/latest/manual/set-environment/). Newly spawned
processes inherit it; existing processes retain their own environments.

**Set:** [`server.SetEnvironment(ctx, name, value, opts)`](<https://libtmux.org/en/go/latest/reference/tmux-server-setenvironment/>) (global, `-g`)

**Read all:** [`server.ShowEnvironment(ctx)`](<https://libtmux.org/en/go/latest/reference/tmux-server-showenvironment/>)

**Unset:** [`server.UnsetEnvironment(ctx, name)`](<https://libtmux.org/en/go/latest/reference/tmux-server-unsetenvironment/>)

### Examples

```go
if err := session.SetEnvironment(ctx, "EDITOR", "hx", tmux.SetEnvironmentOptions{}); err != nil {
    return err
}
values, err := session.ShowEnvironment(ctx)
if err != nil {
    return err
}
fmt.Println(values)
```

[`set-environment`](https://libtmux.org/en/tmux/latest/manual/set-environment/) writes a value. Its `-u` flag removes the entry, while `-r`
marks the variable for exclusion from new processes, including values tmux
inherited at startup. The listing retains an excluded variable as `-NAME`.
