# Environment

Source: https://libtmux.org/en/csharp/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` and `show-environment` for new
processes to inherit. Like the tables in [Options and
hooks](https://libtmux.org/en/csharp/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:** [`Server.FromEnvironment(env)`](<https://libtmux.org/en/csharp/latest/reference/libtmux-server-fromenvironment/>)

**Session:** [`Session.FromEnvironmentAsync()`](<https://libtmux.org/en/csharp/latest/reference/libtmux-session-fromenvironmentasync/>)

**Window:** [`Window.FromEnvironmentAsync()`](<https://libtmux.org/en/csharp/latest/reference/libtmux-window-fromenvironmentasync/>)

**Pane:** [`Pane.FromEnvironmentAsync()`](<https://libtmux.org/en/csharp/latest/reference/libtmux-pane-fromenvironmentasync/>)

### Examples

```csharp
Server server = Server.FromEnvironment(null);
Session session = await Session.FromEnvironmentAsync();
Window window = await Window.FromEnvironmentAsync();
Pane pane = await Pane.FromEnvironmentAsync();
```

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

Handle [`TmuxObjectNotFoundException`](<https://libtmux.org/en/csharp/latest/reference/libtmux-tmuxobjectnotfoundexception/>) when the environment cannot identify an
object.

## tmux's own environment variable store

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

**Set:** [`server.Environment.SetAsync(name, value)`](<https://libtmux.org/en/csharp/latest/reference/libtmux-tmuxenvironment-setasync/>),
[`session.Environment.SetAsync(...)`](<https://libtmux.org/en/csharp/latest/reference/libtmux-tmuxenvironment-setasync/>)

**Read all:** [`server.Environment.GetAllAsync()`](<https://libtmux.org/en/csharp/latest/reference/libtmux-tmuxenvironment-getallasync/>)

**Unset:** [`server.Environment.UnsetAsync(name)`](<https://libtmux.org/en/csharp/latest/reference/libtmux-tmuxenvironment-unsetasync/>), [`.RemoveAsync(name)`](<https://libtmux.org/en/csharp/latest/reference/libtmux-tmuxenvironment-removeasync/>)

### Examples

```csharp
await server.Environment.SetAsync("EDITOR", "vim");
await session.Environment.SetAsync("EDITOR", "hx");
await session.Environment.GetAllAsync();
```

`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`.

Use `.UnsetAsync` for `-u`, or [`.RemoveAsync`](<https://libtmux.org/en/csharp/latest/reference/libtmux-tmuxenvironment-removeasync/>) for `-r`.
