![設定 bash/zsh 完成的環境變量](https://rvso.com/image/1628432/%E8%A8%AD%E5%AE%9A%20bash%2Fzsh%20%E5%AE%8C%E6%88%90%E7%9A%84%E7%92%B0%E5%A2%83%E8%AE%8A%E9%87%8F.png)
我正在使用 gnu pass 和 bash/zsh 補全。這意味著當我輸入時,pass a[TAB]
shell 會建議我可以使用哪些以 開頭的密碼a
。
我有兩個 gnu-pass 目錄,當我想使用非預設目錄時,我需要設定PASSWORD_STORE_DIR
.為了獲得 shell 完成,我需要export PASSWORD_STORE_DIR=~/.password-store-non-default
然後像平常一樣使用 pass 。
這很麻煩,因為我經常需要在兩個目錄之間進行更改。
有沒有辦法建立一個別名或 shell 命令來設定該變量,提供完成,然後將該變數更改回其原始值?
例如
alias otherpass=...
otherpass a[TAB]
將提供完成而無需設定不同的PASSWORD_STORE_DIR
.
答案1
在 Zsh 中,您可以簡單地將鍵盤快速鍵綁定到它,如下所示:
# Bind the command to Ctrl-P.
bindkey '^P' complete-password
# Create a completion widget for the command.
zle -C complete-password complete-word complete-password
# Create a function to implement the widget.
complete-password() {
export PASSWORD_STORE_DIR=~/.password-store-non-default
_main_complete $@
unset PASSWORD_STORE_DIR
}
然後,按CtrlP將完成非預設密碼儲存。 (無需Tab按。)