3

I've just started using fish and I'm loving using the vi keybindings it provides.

I wanted though to add a custom keymap that I use in vi. I usually map space+c in visual mode to yank to "* (system clipboard). How can I add such keymap in fish?

I'm using fish 3.3.1 on MacOS 12.

Edit: I've tried this

bind -M visual \x20c '"*y'

And got the following error

fish: Unexpected end of string, quotes are not balanced

Also tried with a modifier instead of space and got the same result.

8
  • This is the right forum for the question, but you might get more traction on stackoverflow. Commented Nov 24, 2021 at 14:13
  • 1
    @glennjackman last time I asked something about fish there, they closed my question saying it should be here ¯_(ツ)_/¯
    – Augusto
    Commented Nov 24, 2021 at 14:28
  • Did you read Custom bindings in the fish docs? Commented Nov 24, 2021 at 14:31
  • I agree it's probably not on-topic on SO, but you might also consider the Unix & Linux Stack. It's definitely on-topic here as well, so I'd just leave it for now. Commented Nov 24, 2021 at 15:44
  • 1
    @Augusto What happens with bind -M visual \20c fish_clipboard_copy? It's not working for me, but I'm on WSL, where the clipboard operates quite differently. If it works, then I'll add it as an answer. If not, we'll keep working on it :-) Commented Nov 24, 2021 at 21:43

3 Answers 3

2

A couple of possibilities:

  • First, if you are currently using "*y to yank to the system clipboard, then you can see via bind in Fish that the default binding is:

    bind -M visual -m default '"*y' 'commandline -s | xsel -p; commandline -f end-selection repaint-mode'
    

    So you should be able to recreate that for SPACEc by:

    bind -M visual -m default \x20c 'commandline -s | xsel -p; commandline -f end-selection repaint-mode'
    
  • There's also the fish_clipboard_copy function listed in the docs for bind, so another possibility might be:

    bind -M visual -m default \20c fish_clipboard_copy
    

    ... but I assume you'll need to use the converse fish_clipboard_paste as well, rather than xsel.

1

The error is because the binding is setting a command that is not valid. You cannot run "*y at the command line (and if you try with fish -c '"*y', you'll get the same error).

The command to bind needs to be just that - a command. That's why using fish_clipboard_copy works fine. There's no way to execute another keybinding, at least in fish 3.3.1 and before.

0

I don't think it's possible to put a binding on two "plain" characters. When the documentation for bind refers to a "sequence of characters", I think it means zero or more modifiers (ctrl, alt, shift) followed by a "regular" character.

I have bindings for !! and !$ to mimic what bash does with those, and it looks like this:

function bind_bang
    switch (commandline -t)[-1]
    case "!"
        commandline -t $history[1]; commandline -f repaint
    case "*"
        commandline -i !
    end
end

function bind_dollar
    switch (commandline -t)[-1]
    case "!"
        commandline -t ""
        commandline -f history-token-search-backward
    case "*"
        commandline -i '$'
    end
end

bind ! bind_bang
bind '$' bind_dollar

Each binding function has to look for the previous character in the command line.

I don't use vi-mode in fish (I started using fish before it had a vi-mode, so my fingers got trained) -- I'm not sure how you're going to have to implement paste.

3
  • I get the same error if I try with a modifier. The issue is with the command
    – Augusto
    Commented Nov 24, 2021 at 19:44
  • 1
    I'd recommend you seek more expert help: try gitter or the mailing list => fishshell.com/docs/current/… Commented Nov 24, 2021 at 19:47
  • You can definitely bind a sequence of characters.
    – Zanchey
    Commented Nov 27, 2021 at 22:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .