# Configure and run hooks

Source: https://libtmux.org/en/powershell/latest/guides/hooks/

> Configure and run hooks: LibTmux documentation.

Use a native server, session, window or pane to select a hook table. Server
hooks are global; tmux has no separate server hook table. Choose a hook
supported in the selected scope. `-Scope` overrides the scope and `-Global`
selects its global table.

Inspect a configured hook on a previously selected `$session`:

<!-- example: hooks.read -->
```powershell
$session | Get-TmuxHook -Name 'alert-bell'
```

Each native [`TmuxHook`](<https://libtmux.org/en/csharp/latest/reference/libtmux-tmuxhook/>) has a base [`Name`][tmuxhook-name] and
[`Values`][tmuxhook-values], an ordered collection of [`TmuxHookEntry`](<https://libtmux.org/en/csharp/latest/reference/libtmux-tmuxhookentry/>) objects
with [`Index`][tmuxhookentry-index] and [`Command`][tmuxhookentry-command]. Get
accepts base names only; inspect [`Values`][tmuxhook-values] for individual
entries. Omit `-Name` to list the selected table. Missing local hooks produce no
objects.

Set one indexed command and inspect the complete grouped hook:

<!-- example: hooks.set -->
```powershell
$command = 'display-message "build finished"'
$session | Set-TmuxHook -Name 'alert-bell[7]' -Command $command -PassThru
```

The command is tmux syntax, not PowerShell syntax. tmux normalizes it;
readback preserves that normalized text so it can be passed back unchanged.
Without `-PassThru`, setting emits no success output. An unindexed set
replaces entries. `-Append` keeps them and lets tmux choose a free index,
which may precede existing sparse indices. Use explicit indices when order
matters. Multiple changes do not form an atomic replacement.

Run a previously configured hook without waiting for its event:

<!-- example: hooks.invoke -->
```powershell
$session | Invoke-TmuxHook -Name 'alert-bell'
```

The call confirms acceptance by tmux. A hook can start asynchronous work;
use a producer signal and [Wait-TmuxChannel](https://libtmux.org/en/powershell/latest/reference/libtmux-wait-tmuxchannel/)
to observe that work completing.

Remove one indexed entry, preserving its neighbours:

<!-- example: hooks.remove -->
```powershell
$session | Remove-TmuxHook -Name 'alert-bell[7]'
```

Use the base name to remove the local hook. Mutations support `-WhatIf` and
`-Confirm`; previews perform no tmux I/O. Errors retain the native owner and
core exception. `-ErrorAction Continue` permits later owners;
[`Stop`][erroraction-stop] terminates the pipeline. Cancellation cannot undo a
dispatched command.

[tmuxhook-name]: https://libtmux.org/en/csharp/latest/reference/libtmux-tmuxhook-name/
[tmuxhook-values]: https://libtmux.org/en/csharp/latest/reference/libtmux-tmuxhook-values/
[tmuxhookentry-index]: https://libtmux.org/en/csharp/latest/reference/libtmux-tmuxhookentry-index/
[tmuxhookentry-command]: https://libtmux.org/en/csharp/latest/reference/libtmux-tmuxhookentry-command/
[erroraction-stop]: https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.actionpreference#system-management-automation-actionpreference-stop
