Skip to content

Conversation

bf
Copy link

@bf bf commented Sep 30, 2025

Add missing ) to docs.

bf added 2 commits September 30, 2025 11:41
Add missing `)` to docs.
Fixing example by adding `[` and `]` to provide list to hook

```
Error: nu::shell::invalid_config

  × Encountered 1 error(s) when updating config

Error: nu::shell::type_mismatch

  × Type mismatch at $env.config.hooks.env_change.PWD
   ╭─[]
 4 │         env_change: {
 5 │ ╭─▶         PWD: {
 6 │ │               code: 'print $"changing directory from ($before) to ($after)"'
 7 │ ├─▶         }
   · ╰──── expected list<any>, but got record<code: string>
 8 │         }
   ╰────
```
@bf
Copy link
Author

bf commented Sep 30, 2025

Working example is now

$env.config = ($env.config | upsert hooks {
    env_change: {
        PWD: [{
            code: 'print $"changing directory from ($before) to ($after)"'
        }]
    }
})

website at https://www.nushell.sh/book/hooks.html#hooks-as-strings currently shows broken example:

$env.config = ($env.config | upsert hooks {
    env_change: {
        PWD: {
            code: 'print $"changing directory from ($before) to ($after)"'
        }
    }
}

@fdncred
Copy link
Contributor

fdncred commented Sep 30, 2025

Are the parentheses even needed at all? This works but it will also overwrite your existing env_change.PWD if it already exists.

$env.config = $env.config | upsert hooks {
    env_change: {
        PWD: [{
            code: 'print $"changing directory from ($before) to ($after)"'
        }]
    }
}

@Bahex
Copy link
Member

Bahex commented Oct 5, 2025

We should probably go further and update these to use the "modern" style:

# Make sure the `PWD` hook list exists
$env.config.hooks.env_change.PWD = $env.config.hooks.env_change.PWD? | default []

# Add hook(s)
$env.config.hooks.env_change.PWD ++= [
    {
        condition: {|_, after| $after == /some/path/to/directory }
        code: 'def foo [] { print "foo" }'
    }
    {
        condition: {|before, _| $before == /some/path/to/directory }
        code: 'hide foo'
    }
]

$env.config.hooks.env_change.PWD ++= [
    {
        code: 'print $"changing directory from ($before) to ($after)"'
    }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants