設定 bash/zsh 完成的環境變量

設定 bash/zsh 完成的環境變量

我正在使用 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按。)

相關內容