}
{
condition: {|before, _| $before == /some/path/to/directory }
code: 'hide foo'
}
]
}
})
```
When defining a hook as a string, the `$before` and `$after` variables are set to the previous and current environment variable value, respectively, similarly to the previous examples:
```nu
$env.config = ($env.config | upsert hooks {
env_change: {
PWD: {
code: 'print $"changing directory from ($before) to ($after)"'
}
}
}
```
## Examples
### Adding a Single Hook to Existing Config
An example for PWD env change hook:
```nu
$env.config = ($env.config | upsert hooks.env_change.PWD {|config|
let val = ($config | get -i hooks.env_change.PWD)
if $val == null {
$val | append {|before, after| print $"changing directory from ($before) to ($after)" }
} else {
[
{|before, after| print $"changing directory from ($before) to ($after)" }
]
}
})
```
### Automatically Activating an Environment when Entering a Directory
This one looks for `test-env.nu` in a directory
```nu
$env.config = ($env.config | upsert hooks.env_change.PWD {
[
{
condition: {|_, after|
($after == '/path/to/target/dir'
and ($after | path join test-env.nu | path exists))
}
code: "overlay use test-env.nu"
}
{
condition: {|before, after|
('/path/to/target/dir' not-in $after
and '/path/to/target/dir' in ($before | default "")
and 'test-env' in (overlay list))
}
code: "overlay hide test-env --keep-env [ PWD ]"
}
]
})
```
### Filtering or Diverting Command Output
You can use the `display_output` hook to redirect the output of commands.
You should define a block that works on all value types.
The output of external commands is not filtered through `display_output`.
This hook can display the output in a separate window,
perhaps as rich HTML text. Here is the basic idea of how to do that:
```nu
$env.config = ($env.config | upsert hooks {
display_output: { to html --partial --no-color | save --raw /tmp/nu-output.html }
})
```
You can view the result by opening `file:///tmp/nu-output.html` in
a web browser.
Of course this isn't very convenient unless you use
a browser that automatically reloads when the file changes.
Instead of the [`save`](/commands/docs/save.md) command, you would normally customize this