1
abbr -a gc git commit

After expanding gc via pressing space, I want to undo via pressing ctrl+space in fish shell.

How do i do that?

1 Answer 1

3

To bind a key in fish, use

bind sequence thing-to-bind-to

or

bind -k keyname thing-to-bind-to

To find out the sequence, use fish_key_reader and press whatever you want [0].

So, if we do that, we get:

Press a key: bind -k nul 'do something'

So we bind the keyname "nul" to "undo" [1]:

bind -k nul undo

Note: This is a bad idea because ctrl-space is already bound to insert a space without expanding an abbreviation by default. So now you'd have to expand your abbreviation, undo it, press space again which expands the abbreviation again, ...

You would need to bind another key to insert a space without expanding. E.g. to make ctrl-g do that:

bind \cg 'commandline -i " "'

Instead you can simply use ctrl-z to undo, which is bound by default, and then press ctrl-space to insert your space without expanding the abbreviation.


[0]: If fish_key_reader doesn't print anything, that means your terminal doesn't send anything for your key combination. This can happen e.g. if the terminal uses it for its own bindings.

Also the protocol terminals use to send key combinations by default is fairly horrible and can't express a lot of combinations - e.g. there is no way to distinguish ctrl-i from tab, or ctrl-shift-X from ctrl-X. There are alternatives, but none have widespread buy-in.

[1]: If you use vi-mode, bind without a mode will set it in vi-normal mode. To set something in e.g. insert mode, add -M insert. Or -M visual for visual mode.

4
  • i am on debian fish v3.1.2 ctr+z doesn't undo
    – Mega Bang
    Apr 29 at 8:27
  • 1
    Fish 3.1.2 is 2 years old and missing about 25% of all commits ever made to fish. Undo was introduced in fish 3.2.0 released in march 2021. Update, use the PPA linked on fishshell.com
    – faho
    Apr 29 at 8:56
  • Also a heads-up that Ctrl+2 and Ctrl+` (backtick) return nul as well, so binding any of these keys binds all of them. Apr 29 at 11:37
  • Yes, that's my footnote 0 - the terminal doesn't differentiate these. There is no way to bind ctrl-2 separately from ctrl-space, because they are encoded identically by the terminal. You will have the same problem e.g. in Emacs or vi.
    – faho
    Apr 29 at 11:43

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.