在 inputrc 中呼叫 bashrc 中的函數

在 inputrc 中呼叫 bashrc 中的函數

我在 Arch Linux (4.13.11) 上,在我的 中.bashrc,有一個自訂函數可以使用以下命令查找文件佛茲夫然後使用預設編輯器開啟該檔案:

# Search a file with fzf inside a Tmux pane and then open the file in an editor
fzf_then_open_in_editor() {
  local file=$(fzf-tmux)
  # Open the file if it exists
  if [ -n "$file" ]; then
    # Use the default editor if it's defined, otherwise Vim
    ${EDITOR:-vim} "$file"
  fi
}

我已經將.inputrc, 配置為在 shell 中使用類似 Vim 的鍵綁定:

set editing-mode vi

最好,我可以在命令模式下按Ctrl+o來呼叫我的函數fzf_then_open_in_editor

我試過

set keymap vi-command
"\C-o": fzf_then_open_in_editor

但這沒有用。

在 中.inputrc,如何為 中定義的函數建立鍵綁定.bashrc

答案1

此表單key: function僅接受 readline 內建函數。正如您所發現的,要輸入文本,您需要所謂的宏,該宏必須用引號括起來:key: "my macro\r"。我不明白為什麼您需要從插入命令開始,因為通常您已經在命令提示字元中處於插入模式。

如果要呼叫函數,請使用帶有 option 的 shell 命令綁定-x。我不知道有什麼方法可以將此綁定放入 a 中~/.inputrc,但您可以將以下命令放入您的 中.bashrc

bind -x '"\C-o": fzf_then_open_in_editor'

答案2

我通過將其添加到以下內容得到了我想要的.inputrc

set keymap vi-command
# Go to insert mode with i, write the function's name, then hit enter
"\C-o": "ifzf_then_open_in_editor\015"

\015的密鑰代碼在哪裡Enter

這可行,但我很好奇是否有更優雅的解決方案。

相關內容