bind - handle fish key bindings¶
Synopsis¶
bind [(-M | --mode) MODE] [(-m | --sets-mode) NEW_MODE] [--preset | --user] [-s | --silent] KEYS COMMAND ...
bind [(-M | --mode) MODE] [--preset] [--user] [KEYS]
bind [-a | --all] [--preset] [--user]
bind (-f | --function-names)
bind (-L | --list-modes)
bind (-e | --erase) [(-M | --mode) MODE] [--preset] [--user] [-a | --all] | KEYS ...
Description¶
bind manages key bindings.
If both KEYS and COMMAND are given, bind adds (or replaces) a binding in MODE.
If only KEYS is given, any existing binding in the given MODE will be printed.
KEYS is a comma-separated list of key names.
Modifier keys can be specified by prefixing a key name with a combination of ctrl-, alt- and shift-.
For example, pressing w while holding the Alt modifier is written as alt-w.
Key names are case-sensitive; for example alt-W is the same as alt-shift-w.
ctrl-x,ctrl-e would mean pressing ctrl-x followed by ctrl-e.
Some keys have names, usually because they don’t have an obvious printable character representation. They are:
the arrow keys
up,down,leftandright,backspace,comma(,),delete,end,enter,escape,f1throughf12.home,insert,minus(-),pageup,pagedown,spaceandtab,
These names are case-sensitive.
An empty value ('') for KEYS designates the generic binding that will be used if nothing else matches. For most bind modes, it makes sense to bind this to the self-insert function (i.e. bind '' self-insert). This will insert any keystrokes that have no bindings otherwise. Non-printable characters are ignored by the editor, so this will not result in control sequences being inserted.
To find the name of a key combination you can use fish_key_reader.
COMMAND can be any fish command, but it can also be one of a set of special input functions. These include functions for moving the cursor, operating on the kill-ring, performing tab completion, etc. Use bind --function-names or see below for a list of these input functions.
Note
If a script changes the commandline, it should finish by calling the repaint special input function.
If no KEYS argument is provided, all bindings (in the given MODE) are printed. If KEYS is provided but no COMMAND, just the binding matching that sequence is printed.
Key bindings may use “modes”, which mimics vi’s modal input behavior. The default mode is “default”. Every key binding applies to a single mode; you can specify which one with -M MODE. If the key binding should change the mode, you can specify the new mode with -m NEW_MODE. The mode can be viewed and changed via the $fish_bind_mode variable. If you want to change the mode from inside a fish function, use set fish_bind_mode MODE.
To save custom key bindings, put the bind statements into config.fish. Alternatively, fish also automatically executes a function called fish_user_key_bindings if it exists.
Options¶
The following options are available:
- -f or --function-names
Display a list of available input functions
- -L or --list-modes
Display a list of defined bind modes
- -M MODE or --mode MODE
Specify a bind mode that the bind is used in. Defaults to “default”
- -m NEW_MODE or --sets-mode NEW_MODE
Change the current mode to NEW_MODE after this binding is executed
- -e or --erase
Erase the binding with the given sequence and mode instead of defining a new one. Multiple sequences can be specified with this flag. Specifying -a or --all with -M or --mode erases all binds in the given mode regardless of sequence. Specifying -a or --all without -M or --mode erases all binds in all modes regardless of sequence.
- -a or --all
See --erase
- --preset and --user
Specify if bind should operate on user or preset bindings. User bindings take precedence over preset bindings when fish looks up mappings. By default, all
bindinvocations work on the “user” level except for listing, which will show both levels. All invocations except for inserting new bindings can operate on both levels at the same time (if both --preset and --user are given). --preset should only be used in full binding sets (like when working onfish_vi_key_bindings).- -s or --silent
Silences some of the error messages, including for unknown key names and unbound sequences.
- -h or --help
Displays help about using this command.
Special input functions¶
The following special input functions are available:
andonly execute the next function if the previous succeeded (note: only some functions report success)
accept-autosuggestionaccept the current autosuggestion. Returns false when there was nothing to accept.
backward-charmove one character to the left. If the completion pager is active, select the previous completion instead.
backward-char-passivemove one character to the left, but do not trigger any non-movement-related operations. If the cursor is at the start of the commandline, does nothing. Does not change the selected item in the completion pager UI when shown.
backward-bigwordmove one whitespace-delimited word to the left
backward-tokenmove one argument to the left
backward-delete-chardeletes one character of input to the left of the cursor
backward-kill-bigwordmove the whitespace-delimited word to the left of the cursor to the killring
backward-kill-tokenmove the argument to the left of the cursor to the killring
backward-kill-linemove everything from the beginning of the line to the cursor to the killring
backward-kill-path-componentmove one path component to the left of the cursor to the killring. A path component is everything likely to belong to a path component, i.e. not any of the following: /={,}’":@ |;<>&, plus newlines and tabs.
backward-kill-wordmove the word to the left of the cursor to the killring. The “word” here is everything up to punctuation or whitespace.
backward-wordmove one word to the left
beginning-of-buffermoves to the beginning of the buffer, i.e. the start of the first line
beginning-of-historymove to the beginning of the history
beginning-of-linemove to the beginning of the line
begin-selectionstart selecting text
cancelclose the pager if it is open, or undo the most recent completion if one was just inserted, or otherwise cancel the current commandline and replace it with a new empty one
cancel-commandlinecancel the current commandline and replace it with a new empty one, leaving the old one in place with a marker to show that it was cancelled
capitalize-wordmake the current word begin with a capital letter
clear-commandlineempty the entire commandline
clear-screenclears the screen and redraws the prompt. if the terminal doesn’t support clearing the screen it is the same as
repaint.completeguess the remainder of the current token
complete-and-searchinvoke the searchable pager on completion options (for convenience, this also moves backwards in the completion pager)
delete-chardelete one character to the right of the cursor
delete-or-exitdelete one character to the right of the cursor, or exit the shell if the commandline is empty
down-linemove down one line
downcase-wordmake the current word lowercase
end-of-buffermoves to the end of the buffer, i.e. the end of the first line
end-of-historymove to the end of the history
end-of-linemove to the end of the line
end-selectionend selecting text
expand-abbrexpands any abbreviation currently under the cursor
executerun the current commandline
exitexit the shell
forward-bigwordmove one whitespace-delimited word to the right
forward-tokenmove one argument to the right
forward-charmove one character to the right; or if at the end of the commandline, accept the current autosuggestion. If the completion pager is active, select the next completion instead.
forward-char-passivemove one character to the right, but do not trigger any non-movement-related operations. If the cursor is at the end of the commandline, does not accept the current autosuggestion (if any). Does not change the selected item in the completion pager, if shown.
forward-single-charmove one character to the right; or if at the end of the commandline, accept a single char from the current autosuggestion.
forward-wordmove one word to the right; or if at the end of the commandline, accept one word from the current autosuggestion.
history-pagerinvoke the searchable pager on history (incremental search); or if the history pager is already active, search further backwards in time.
history-pager-deletepermanently delete the current history item, either from the history pager or from an active up-arrow history search
history-search-backwardsearch the history for the previous match
history-search-forwardsearch the history for the next match
history-prefix-search-backwardsearch the history for the previous prefix match
history-prefix-search-forwardsearch the history for the next prefix match
history-token-search-backwardsearch the history for the previous matching argument
history-token-search-forwardsearch the history for the next matching argument
forward-jumpandbackward-jumpread another character and jump to its next occurrence after/before the cursor
forward-jump-tillandbackward-jump-tilljump to right before the next occurrence
repeat-jumpandrepeat-jump-reverseredo the last jump in the same/opposite direction
jump-to-matching-bracketjump to matching bracket if the character under the cursor is bracket; otherwise, jump to the next occurrence of any right bracket after the cursor. The following brackets are considered:
([{}])jump-till-matching-bracketthe same as
jump-to-matching-bracketbut offset cursor to the right for left bracket, and offset cursor to the left for right bracket. The offset is applied for both the position we jump from and position we jump to. In other words, the cursor will continuously jump inside the brackets but won’t reach them by 1 character. The input function is useful to emulateibvi text object. The following brackets are considered:([{}])kill-bigwordmove the next whitespace-delimited word to the killring
kill-tokenmove the next argument to the killring
kill-linemove everything from the cursor to the end of the line to the killring
kill-selectionmove the selected text to the killring
kill-whole-linemove the line (including the following newline) to the killring. If the line is the last line, its preceding newline is also removed
kill-inner-linemove the line (without the following newline) to the killring
kill-wordmove the next word to the killring
nextd-or-forward-wordif the commandline is empty, then move forward in the directory history, otherwise move one word to the right; or if at the end of the commandline, accept one word from the current autosuggestion.
oronly execute the next function if the previous did not succeed (note: only some functions report failure)
pager-toggle-searchtoggles the search field if the completions pager is visible; or if used after
history-pager, search forwards in time.prevd-or-backward-wordif the commandline is empty, then move backward in the directory history, otherwise move one word to the left
repaintreexecutes the prompt functions and redraws the prompt (also
force-repaintfor backwards-compatibility)repaint-modereexecutes the fish_mode_prompt and redraws the prompt. This is useful for vi mode. If no
fish_mode_promptexists or it prints nothing, it acts like a normal repaint.self-insertinserts the matching sequence into the command line
self-insert-notfirstinserts the matching sequence into the command line, unless the cursor is at the beginning
suppress-autosuggestionremove the current autosuggestion. Returns true if there was a suggestion to remove.
swap-selection-start-stopgo to the other end of the highlighted text without changing the selection
transpose-charstranspose two characters to the left of the cursor
transpose-wordstranspose two words to the left of the cursor
togglecase-chartoggle the capitalisation (case) of the character under the cursor
togglecase-selectiontoggle the capitalisation (case) of the selection
insert-line-underadd a new line under the current line
insert-line-overadd a new line over the current line
up-linemove up one line
undoandredorevert or redo the most recent edits on the command line
upcase-wordmake the current word uppercase
yankinsert the latest entry of the killring into the buffer
yank-poprotate to the previous entry of the killring
Additional functions¶
The following functions are included as normal functions, but are particularly useful for input editing:
up-or-searchanddown-or-searchmove the cursor or search the history depending on the cursor position and current mode
edit_command_bufferopen the visual editor (controlled by the
VISUALorEDITORenvironment variables) with the current command-line contentsfish_clipboard_copycopy the current selection to the system clipboard
fish_clipboard_pastepaste the current selection from the system clipboard before the cursor
fish_commandline_appendappend the argument to the command-line. If the command-line already ends with the argument, this removes the suffix instead. Starts with the last command from history if the command-line is empty.
fish_commandline_prependprepend the argument to the command-line. If the command-line already starts with the argument, this removes the prefix instead. Starts with the last command from history if the command-line is empty.
Examples¶
Exit the shell when ctrl-d is pressed:
bind ctrl-d 'exit'
Perform a history search when pageup is pressed:
bind pageup history-search-backward
Turn on vi key bindings and rebind ctrl-c to clear the input line:
set -g fish_key_bindings fish_vi_key_bindings
bind -M insert ctrl-c kill-whole-line repaint
Launch git diff and repaint the commandline afterwards when ctrl-g is pressed:
bind ctrl-g 'git diff' repaint
Terminal Limitations¶
Unix terminals, like the ones fish operates in, are at heart 70s technology. They have some limitations that applications running inside them can’t workaround.
For instance, historically the control key modifies a character by setting the top three bits to 0. This means:
Many characters + control are indistinguishable from other keys: ctrl-i is tab, ctrl-j is newline (
\n).Control and shift don’t work simultaneously - ctrl-X is the same as ctrl-x.
Other keys don’t have a direct encoding, and are sent as escape sequences. For example right (→) usually sends \e\[C.
Some modern terminals support newer encodings for keys, that allow distinguishing more characters and modifiers, and fish enables as many of these as it can, automatically.
When in doubt, run fish_key_reader - explore what characters keyboard keys send. If that tells you that pressing ctrl-i sends tab, your terminal does not support these better encodings, and so fish is limited to what it sends.
Key timeout¶
When you’ve bound a sequence of multiple characters, there is always the possibility that fish has only seen a part of it, and then it needs to disambiguate between the full sequence and part of it.
For example:
bind j,k 'commandline -i foo'
# or `bind jk`
will bind the sequence jk to insert “foo” into the commandline. When you’ve only pressed “j”, fish doesn’t know if it should insert the “j” (because of the default self-insert), or wait for the “k”.
You can enable a timeout for this, by setting the fish_sequence_key_delay_ms variable to the timeout in milliseconds. If the timeout elapses, fish will no longer wait for the sequence to be completed, and do what it can with the characters it already has.
The escape key is a special case, because it can be used standalone as a real key or as part of a longer escape sequence, like function or arrow keys. Holding alt and something else also typically sends escape, for example holding alt+a will send an escape character and then an “a”. So the escape character has its own timeout configured with fish_escape_delay_ms.
See also Key sequences.
