# session.Session.set_hooks

- **Module:** session.Session
- **Package:** libtmux
- **Language:** Rust
- **Kind:** method
- **Source:** https://github.com/libtmux/libtmux-rs/blob/f0e37052c232636b61d095817046e6bfc8f2ca40/crates/libtmux/src/session/settings.rs#L277
- **Page:** https://libtmux.org/en/rs/latest/reference/session-session-set_hooks/

```
session.Session.set_hooks(self, name: &str, hooks: &IndexedHooks, replace: ReplaceMode) -> Result<(), Error>
```

Write a whole hook at once.

[`ReplaceMode::Replace`] clears the hook first, so only what is
written remains; [`ReplaceMode::Merge`] leaves entries at indices the
write does not name.

Sent as at most two tmux invocations rather than one per index: the
first command alone, then the rest together. That is not atomic: tmux
applies a shared invocation in order and stops at the first refusal,
so a rejected entry leaves the ones before it written.

# Errors

Returns an error when tmux rejects the name or any command.

# Cancel safety

Under [`ReplaceMode::Replace`] the clear and the entries are one
invocation, so a drop leaves the hook as it was or fully written,
never cleared and unwritten; tmux reports one status for that group,
so a refusal there cannot name which command drew it. Under
[`ReplaceMode::Merge`] the first entry is sent on its own, so a drop
after it leaves that entry written and the rest not.

## Example

```rust
use std::collections::BTreeMap;
use libtmux::{IndexedHooks, ReplaceMode, TmuxText};

let guard = libtmux::test::TestServer::new().await?;
let session = guard.server().new_session("hooked").await?;

let mut entries = BTreeMap::new();
entries.insert(0, TmuxText::from(b"display-message first".to_vec()));
entries.insert(3, TmuxText::from(b"display-message fourth".to_vec()));

session
    .set_hooks("alert-bell", &IndexedHooks::from(entries), ReplaceMode::Replace)
    .await?;

let written = session.hook("alert-bell").await?.expect("the hook is set");
assert_eq!(written.len(), 2);
assert!(written.get(1).is_none(), "the gap is kept");

guard.shutdown().await?;
```
