設定 Tab 自動完成
自 5.34.0 版起,Rush 支援 Tab 自動完成,因此可以透過按下 TAB 鍵更快地輸入 Shell 命令。以下設定說明是根據文章 適用於 .NET Core CLI 的 Tab 自動完成,其中提供了一些額外提示。
PowerShell
若要為 PowerShell 啟用 Tab 自動完成,請建立或編輯儲存在 $PROFILE
變數中的設定檔。如需詳細資訊,請參閱如何建立您的設定檔和設定檔和執行原則。
將以下程式碼新增至您的設定檔
# PowerShell parameter completion shim for the Rush CLI
Register-ArgumentCompleter -Native -CommandName rush -ScriptBlock {
param($commandName, $commandAst, $cursorPosition)
[string]$value = $commandAst.ToString()
# Handle input like `rush install; rush bui` + Tab
[int]$position = [Math]::Min($cursorPosition, $value.Length)
rush tab-complete --position $position --word "$value" | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
Bash
若要為 Bash 啟用 Tab 自動完成,請將以下程式碼新增至您的 .bashrc 檔案
# bash parameter completion for the Rush CLI
_rush_bash_complete()
{
local word=${COMP_WORDS[COMP_CWORD]}
local completions
completions="$(rush tab-complete --position "${COMP_POINT}" --word "${COMP_LINE}" 2>/dev/null)"
if [ $? -ne 0 ]; then
completions=""
fi
COMPREPLY=( $(compgen -W "$completions" -- "$word") )
}
complete -f -F _rush_bash_complete rush
Fish
若要為 Fish shell 啟用 Tab 自動完成,請將以下程式碼新增至 ~/.config/fish/completions/rush.fish 檔案
# Fish parameter completions for the Rush CLI
complete rush --no-files
function __fish_rush
set -l position (string length (commandline -cp))
set -l word (commandline -opc)
rush tab-complete --word "$word" --position "$position"
end
complete rush -x -a "(__fish_rush)"
Zsh
Zsh 有稍微不同的環境變數,請將以下內容新增至 ~/.zshrc
(( ${+commands[rush]} )) && {
_rush_completion() {
compadd -- $(rush tab-complete --position ${CURSOR} --word "${BUFFER}" 2>>/dev/null)
}
compdef _rush_completion rush
}
它會檢查 rush 是否存在。這需要在 PATH 正確設定後(或在 nvm 初始化後)新增。否則,您將需要移除第一行。