#Command-line completion
Hardhat has a companion npm package that acts as a shorthand for npx hardhat
, and at the same time, it enables command-line completions in your terminal.
This package, hardhat-shorthand
, installs a globally accessible binary called hh
that runs your locally installed hardhat
.
# Installation
To use the Hardhat shorthand you need to install it globally:
npm install --global hardhat-shorthand
After doing this running hh
will be equivalent to running npx hardhat
. For example, instead of running npx hardhat compile
you can run hh compile
.
#Installing the command-line completions
To enable autocomplete support you'll also need to install the shell completion script using hardhat-completion
, which comes with hardhat-shorthand
. Run hardhat-completion install
and follow the instructions to install the completion script:
$ hardhat-completion install
✔ Which Shell do you use ? · zsh
✔ We will install completion to ~/.zshrc, is it ok ? (y/N) · true
=> Added tabtab source line in "~/.zshrc" file
=> Added tabtab source line in "~/.config/tabtab/zsh/__tabtab.zsh" file
=> Wrote completion script to /home/fvictorio/.config/tabtab/zsh/hh.zsh file
=> Tabtab source line added to ~/.zshrc for hh package.
Make sure to reload your SHELL.
To try it out, open a new terminal, go to the directory of your Hardhat project, and try typing hh
followed by tab:
# Context
Out of best practice, Hardhat projects use a local installation of the npm package hardhat
to make sure everyone working on the project is using the same version. This is why you need to use npx
or npm scripts to run Hardhat.
This approach has the downside of there being no way to provide autocomplete suggestions directly for the hardhat
command, as well as making the CLI commands longer. These are the two issues that hh
solves.
# Troubleshooting
#"Autocompletion is not working"
First, make sure you installed the autocompletion script with hardhat-completion install
, then either reload your shell or open a new terminal to try again.
If you still have problems, make sure that your Hardhat config doesn't have any issues. You can do this by just running hh
. If the command prints the help message, then your config is fine. If not, you'll see what the problem is.
If you are using zsh, these are some other things you can try:
- Run
declare -f _hh_completion
. If you don't get any output, then the completion script is not being loaded. - Check that your
.zshrc
has a line that loads a__tabtab.zsh
file. This is the file that in turn should load thehh
completion. - Check that this
__tabtab.zsh
exists, and that there is ahh.zsh
file in that same directory. - Make sure that your
.zshrc
is autoloadingcompinit
. This means that you should have something likeautoload -U compinit && compinit
before the__tabtab.zsh
line.
If you are using bash, try this:
- Run
complete -p hh
. You should getcomplete -o default -F _hh_completion hh
as the output. - Check that your
.bashrc
has a line that loads a__tabtab.bash
file. This is the file that in turn should load thehh
completion. - Check that this
__tabtab.bash
exists, and that there is ahh.bash
file in that same directory.
#Windows user
- The
hardhat-completion install
is not supported on Windows. - On Windows, the default
hh
command is associated with the HTML Help executable program (Hh.exe). To usehardhat-completion
, you can simply runnpx hh
instead ofhh
. - If you want to set the
hh
command for hardhat-completion as the default on Windows, you can follow these steps:- Run the
npm config get prefix
to get the npm global packages installation location. - Add the npm location to the
Path
environment variable. For more information, refer to the Microsoft document - You need to set the system environment variable
Path
because theHh.exe
is in%SystemRoot%
environment variable. Add the npm location to thePath
before%SystemRoot%
- Run the