Skip to content

Commit 54e3bc2

Browse files
authored
Document pre_execution use of commandline command (#1888)
Resolves #1827
1 parent efa0c9f commit 54e3bc2

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

book/hooks.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,26 @@ $env.SPAM
9898

9999
The hook blocks otherwise follow the general scoping rules, i.e., commands, aliases, etc. defined within the block will be thrown away once the block ends.
100100

101+
## `pre_execution` Hooks
102+
103+
`pre_execution` hooks can inspect the to-be-executed command through the [`commandline` command](/commands/docs/commandline.md).
104+
105+
For example, to print the command being executed:
106+
107+
```nu
108+
$env.config = (
109+
$env.config
110+
| upsert hooks.pre_execution [ {||
111+
$env.repl_commandline = (commandline)
112+
print $"Command: ($env.repl_commandline)"
113+
} ]
114+
)
115+
116+
print (1 + 3)
117+
# => Command: print (1 + 3)
118+
# => 4
119+
```
120+
101121
## Conditional Hooks
102122

103123
One thing you might be tempted to do is to activate an environment whenever you enter a directory:

0 commit comments

Comments
 (0)