# Lazy loading command setup

This is a pattern one can use to post-pone expensive setup calls, shortening shell startup time:

```
if [ $commands[kubectl] ]; then
  kubectl() {
    unfunction "$0"
    source <(kubectl completion zsh)
    $0 "$@"
  }
fi
```

However, the `zsh` completion system seems sofisticated enough to handle this out of the box, as long as completion scripts are supplied in the correct way. Take a look in `/usr/share/zsh/site-functions`.

A similar pattern can be used to load tools like `nvm` on demand:

```
nvm() {

  unfunction "$0"

  [ -z "$NVM_DIR" ] && export NVM_DIR="$HOME/.nvm"
  source /usr/share/nvm/nvm.sh
  source /usr/share/nvm/install-nvm-exec

  $0 "$@"
}
```

[Source](https://frederic-hemberger.de/notes/shell/speed-up-initial-zsh-startup-with-lazy-loading/)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://notes.eliasnorrby.com/zsh/lazy-load-completion.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
