0

I'm on Arch Linux (4.13.11) and in my .bashrc, there's a custom function that finds a file using fzf and then opens that file with the default editor:

# 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
}

I've configured my .inputrc, to use Vim-like keybindings for the shell:

set editing-mode vi

Preferably, I could press Ctrl + o when in command mode to call my function fzf_then_open_in_editor.

I tried

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

but that didn't work.

In .inputrc, how can I create keybindings for functions defined in .bashrc?

1

I got what I wanted by adding this to .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"

Where \015 is the keycode for Enter.

This works, but I'm curious if there are more elegant solutions.

1

The form key: function only accepts readline built-in functions. As you found out, to enter text you need what it called a macro which has to be in quotes: key: "my macro\r". I don't see why you need to start with an insert command, as usually you are already in insert mode at the command prompt.

If you want to call a function, use a shell command binding with option -x. I don't know of any way to put this binding in a ~/.inputrc, but you can put the following command in your .bashrc:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.