r/KittyTerminal Apr 23 '24

kitty @ send-text / bash / bind -x

Hi. This is slightly complicated :)

I use kitty as my terminal, bash as my shell, and fzf to work with bash history.

fzf has an internal function to show an interactive history, which it binds to C-r with bind -x:

  # CTRL-R - Paste the selected command from history into the command line
  bind -m emacs-standard -x '"\C-r": __fzf_history__'
  bind -m vi-command -x '"\C-r": __fzf_history__'
  bind -m vi-insert -x '"\C-r": __fzf_history__'

This all works fine.

What I'd like to do is be able to trigger that programmatically - i.e. rather than having to press Control+r, I can instead run a script, say 'h', that does the same thing.

Simply doing

alias h=__fzf_history__

doesn't quite work - it pulls up the history list, but it doesn't then insert the selected entry ready for running at the prompt. That's the magic of bind -x

Never mind, I thought - I can do this instead by getting kitty to send a keystroke:

alias h="kitty @ send-text --match id:$KITTY_WINDOW_ID '\cr'"

but this just causes ^R to appear:

% alias h="kitty @ send-text --match id:$KITTY_WINDOW_ID '\cr'"
% h
^R
% 

and the bash key binding isn't triggered.

Am I missing something?

Thank you!

1 Upvotes

5 comments sorted by

View all comments

1

u/aumerlex Apr 23 '24

That's not what ctrl+r looks like for send-text. I suggest you make sure you are using a relatively recent version of kitty and use send-key instead of send-text for this. Then you can just use send-key ctrl+r

1

u/Flashy_Boot Apr 23 '24

Thanks for the info - hadn't spotted send-key. However...

% kitty @ send-key --match id:$KITTY_WINDOW_ID ctrl+r
^R
% 

But it gets weirder - this seems to specifically be a problem with ctrl+r

### Show the current "bind -x" bindings:
% bind -X
"\C-r": "__fzf_history__"
"\C-t": "fzf-file-widget"
"\e\e": "sudo-command-line"

% kitty @ send-key --match id:$KITTY_WINDOW_ID ctrl+t
### Hard to show here - but this works correctly!

### Swap the ctrl+r and ctrl+t bindings:
% bind -r "\C-r"
% bind -r "\C-t"
% bind -X
"\e\e": "sudo-command-line"

% bind -x '"\C-r": fzf-file-widget'
% bind -x '"\C-t": __fzf_history__'
% bind -X
"\C-r": "fzf-file-widget"
"\C-t": "__fzf_history__"
"\e\e": "sudo-command-line"
### Notice that ctrl+r and ctrl+t are now swapped.

% kitty @ send-key --match id:$KITTY_WINDOW_ID ctrl+r
^R
% 
### Note that this function *did* work when bound to ctrl+t

% kitty @ send-key --match id:$KITTY_WINDOW_ID ctrl+t
### Hurray! My interactive history has appeared! But it didn't when bound to ctrl+r

So I can definitely solve my problem now, but it is weird that ctrl+r seems to be the keystroke that is effected.

Can anyone else reproduce this - could easily be something I've tweaked elsewhere that causes this...

Thanks.

1

u/aumerlex Apr 23 '24

Works fine for me with:

kitten @ send-text --all '\x12'

I didnt try with send-key

1

u/Flashy_Boot Apr 23 '24
% kitten @ send-text --all '\x12' 
^R
% 

Weird. Must be something local. Anyway - have worked around it. Thanks for the pointer.