2019-03-31 17:05:09 +08:00
.. _cmd-bind:
2018-12-17 09:39:33 +08:00
bind - handle fish key bindings
2019-01-03 12:10:47 +08:00
===============================
2018-12-18 09:58:24 +08:00
Synopsis
--------
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
| `` bind `` [(**-M** | **--mode** ) *MODE* ] [(*-m* | *--sets-mode* ) **NEW_MODE** ] [*--preset* | *--user* ] [*-s* | *--silent* ] [**-k** | **--key** ] *SEQUENCE* *COMMAND* ...
2022-01-09 16:01:51 +08:00
| `` bind `` [(**-M** | **--mode** ) *MODE* ] [**-k** | **--key** ] [**--preset** ] [**--user** ] *SEQUENCE*
| `` bind `` (**-K** | **--key-names** ) [**-a** | **--all** ] [**--preset** ] [**--user** ]
| `` bind `` (**-f** | **--function-names** )
| `` bind `` (**-L** | **--list-modes** )
| `` bind `` (**-e** | **--erase** ) [(**-M** | **--mode** ) *MODE* ] [**--preset** ] [**--user** ] [**-a** | **--all** ] | [**-k** | **--key** ] *SEQUENCE* ...
2018-12-17 05:08:41 +08:00
2018-12-19 10:44:30 +08:00
Description
2019-01-03 12:10:47 +08:00
-----------
2018-12-17 05:08:41 +08:00
2020-05-19 02:48:27 +08:00
`` bind `` manages bindings.
2018-12-17 05:08:41 +08:00
2020-05-19 02:48:27 +08:00
It can add bindings if given a SEQUENCE of characters to bind to. These should be written as :ref: `fish escape sequences <escapes>` . The most important of these are `` \c `` for the control key, and `` \e `` for escape, and because of historical reasons also the Alt key (sometimes also called "Meta").
2018-12-17 05:08:41 +08:00
2020-05-19 02:48:27 +08:00
For example, :kbd: `Alt` \ +\ :kbd: `W` can be written as `` \ew `` , and :kbd: `Control` \ +\ :kbd: `X` (^X) can be written as `` \cx `` . Note that Alt-based key bindings are case sensitive and Control-based key bindings are not. This is a constraint of text-based terminals, not `` fish `` .
2018-12-17 05:08:41 +08:00
2020-05-19 02:48:27 +08:00
The generic key binding that matches if no other binding does can be set by specifying a `` SEQUENCE `` of the empty string (that is, `` '' `` ). For most key bindings, it makes sense to bind this to the `` self-insert `` function (i.e. `` bind '' self-insert `` ). This will insert any keystrokes not specifically bound to into the editor. Non-printable characters are ignored by the editor, so this will not result in control sequences being inserted.
If the `` -k `` switch is used, the name of a key (such as 'down', 'up' or 'backspace') is used instead of a sequence. The names used are the same as the corresponding curses variables, but without the 'key\_' prefix. (See `` terminfo(5) `` for more information, or use `` bind --key-names `` for a list of all available named keys). Normally this will print an error if the current `` $TERM `` entry doesn't have a given key, unless the `` -s `` switch is given.
2018-12-17 05:08:41 +08:00
2021-01-09 20:13:48 +08:00
To find out what sequence a key combination sends, you can use :ref: `fish_key_reader <cmd-fish_key_reader>` .
2018-12-20 04:02:45 +08:00
`` 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 `` for a complete list of these input functions.
2018-12-17 05:08:41 +08:00
2021-02-06 03:02:22 +08:00
When `` COMMAND `` is a shellscript command, it is a good practice to put the actual code into a :ref: `function <syntax-function>` and simply bind to the function name. This way it becomes significantly easier to test the function while editing, and the result is usually more readable as well.
2018-12-17 05:08:41 +08:00
2018-12-20 04:02:45 +08:00
If a script produces output, it should finish by calling `` commandline -f repaint `` to tell fish that a repaint is in order.
2018-12-17 05:08:41 +08:00
2020-05-19 02:48:27 +08:00
Note that special input functions cannot be combined with ordinary shell script commands. The commands must be entirely a sequence of special input functions (from `` bind -f `` ) or all shell script commands (i.e., valid fish script).
2018-12-17 05:08:41 +08:00
2020-05-19 02:48:27 +08:00
If no `` SEQUENCE `` is provided, all bindings (or just the bindings in the given `` MODE `` ) are printed. If `` SEQUENCE `` is provided but no `` COMMAND `` , just the binding matching that sequence is printed.
2018-12-17 05:08:41 +08:00
2021-05-28 23:21:49 +08:00
To save custom keybindings, put the `` bind `` statements into :ref: `config.fish <configuration>` . Alternatively, fish also automatically executes a function called `` fish_user_key_bindings `` if it exists.
2018-12-17 05:08:41 +08:00
2018-12-20 04:02:45 +08:00
Key bindings may use "modes", which mimics Vi's modal input behavior. The default mode is "default", and every bind applies to a single mode. The mode can be viewed/changed with the `` $fish_bind_mode `` variable.
2018-12-17 05:08:41 +08:00
2020-05-19 02:48:27 +08:00
Options
-------
The following options are available:
2018-12-17 05:08:41 +08:00
2021-12-09 19:53:13 +08:00
**-k** or **--key**
2021-12-09 19:33:33 +08:00
Specify a key name, such as 'left' or 'backspace' instead of a character sequence
2018-12-17 05:08:41 +08:00
2021-12-09 19:53:13 +08:00
**-K** or **--key-names**
Display a list of available key names. Specifying **-a** or **--all** includes keys that don't have a known mapping
2018-12-17 05:08:41 +08:00
2021-12-09 19:53:13 +08:00
**-f** or **--function-names**
2021-12-09 19:33:33 +08:00
Display a list of available input functions
2018-12-17 05:08:41 +08:00
2021-12-09 19:53:13 +08:00
**-L** or **--list-modes**
2021-12-09 19:33:33 +08:00
Display a list of defined bind modes
2018-12-17 05:08:41 +08:00
2021-12-09 19:53:13 +08:00
**-M MODE** or **--mode** *MODE*
2021-12-09 19:33:33 +08:00
Specify a bind mode that the bind is used in. Defaults to "default"
2018-12-17 05:08:41 +08:00
2021-12-09 19:53:13 +08:00
**-m NEW_MODE** or **--sets-mode** *NEW_MODE*
Change the current mode to *NEW_MODE* after this binding is executed
2018-12-17 05:08:41 +08:00
2021-12-09 19:53:13 +08:00
**-e** or **--erase**
2021-12-09 19:33:33 +08:00
Erase the binding with the given sequence and mode instead of defining a new one.
Multiple sequences can be specified with this flag.
2021-12-09 19:53:13 +08:00
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.
2018-12-17 05:08:41 +08:00
2021-12-09 19:53:13 +08:00
**-a** or **--all**
See **--erase** and **--key-names**
2018-12-17 05:08:41 +08:00
2021-12-09 19:53:13 +08:00
**--preset** and **--user**
2021-12-09 19:33:33 +08:00
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 `` bind `` invocations work on the "user" level except for listing, which will show both levels.
2021-12-09 19:53:13 +08:00
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 on `` fish_vi_key_bindings `` ).
2018-12-17 05:08:41 +08:00
2018-12-19 10:44:30 +08:00
Special input functions
2019-01-03 12:10:47 +08:00
-----------------------
2018-12-17 05:08:41 +08:00
The following special input functions are available:
2021-12-09 19:33:33 +08:00
`` and ``
only execute the next function if the previous succeeded (note: only some functions report success)
2019-12-25 17:46:46 +08:00
2021-12-09 19:33:33 +08:00
`` accept-autosuggestion ``
accept the current autosuggestion completely
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` backward-char ``
moves one character to the left
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` backward-bigword ``
move one whitespace-delimited word to the left
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` backward-delete-char ``
deletes one character of input to the left of the cursor
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` backward-kill-bigword ``
move the whitespace-delimited word to the left of the cursor to the killring
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` backward-kill-line ``
move everything from the beginning of the line to the cursor to the killring
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` backward-kill-path-component ``
move 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.
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` backward-kill-word ``
move the word to the left of the cursor to the killring. The "word" here is everything up to punctuation or whitespace.
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` backward-word ``
move one word to the left
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` beginning-of-buffer ``
moves to the beginning of the buffer, i.e. the start of the first line
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` beginning-of-history ``
move to the beginning of the history
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` beginning-of-line ``
move to the beginning of the line
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` begin-selection ``
start selecting text
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` cancel ``
cancel the current commandline and replace it with a new empty one
2019-12-25 17:26:03 +08:00
2021-12-09 19:33:33 +08:00
`` cancel-commandline ``
cancel 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
2020-07-02 02:54:13 +08:00
2021-12-09 19:33:33 +08:00
`` capitalize-word ``
make the current word begin with a capital letter
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` complete ``
guess the remainder of the current token
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` complete-and-search ``
invoke the searchable pager on completion options (for convenience, this also moves backwards in the completion pager)
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` delete-char ``
delete one character to the right of the cursor
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` delete-or-exit ``
2021-12-09 00:29:52 +08:00
delete one character to the right of the cursor, or exit the shell if the commandline is empty
2019-07-03 17:44:08 +08:00
2021-12-09 19:33:33 +08:00
`` down-line ``
move down one line
2019-10-09 00:01:32 +08:00
2021-12-09 19:33:33 +08:00
`` downcase-word ``
make the current word lowercase
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` end-of-buffer ``
moves to the end of the buffer, i.e. the end of the first line
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` end-of-history ``
move to the end of the history
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` end-of-line ``
move to the end of the line
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` end-selection ``
end selecting text
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` expand-abbr ``
expands any abbreviation currently under the cursor
2019-04-01 21:59:15 +08:00
2021-12-09 19:33:33 +08:00
`` execute ``
run the current commandline
2019-12-25 17:26:03 +08:00
2021-12-09 19:33:33 +08:00
`` exit ``
exit the shell
2021-01-04 16:45:34 +08:00
2021-12-09 19:33:33 +08:00
`` forward-bigword ``
move one whitespace-delimited word to the right
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` forward-char ``
move one character to the right
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` forward-single-char ``
move one character to the right; if an autosuggestion is available, only take a single char from it
2020-07-21 21:08:38 +08:00
2021-12-09 19:33:33 +08:00
`` forward-word ``
move one word to the right
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` history-search-backward ``
search the history for the previous match
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` history-search-forward ``
search the history for the next match
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` history-prefix-search-backward ``
search the history for the previous prefix match
2019-10-06 04:50:20 +08:00
2021-12-09 19:33:33 +08:00
`` history-prefix-search-forward ``
search the history for the next prefix match
2019-10-06 04:50:20 +08:00
2021-12-09 19:33:33 +08:00
`` history-token-search-backward ``
search the history for the previous matching argument
2019-07-28 16:33:54 +08:00
2021-12-09 19:33:33 +08:00
`` history-token-search-forward ``
search the history for the next matching argument
2019-07-28 16:33:54 +08:00
2021-12-09 19:33:33 +08:00
`` forward-jump `` and `` backward-jump ``
read another character and jump to its next occurence after/before the cursor
2019-12-25 17:26:03 +08:00
2021-12-09 19:33:33 +08:00
`` forward-jump-till `` and `` backward-jump-till ``
jump to right *before* the next occurence
2019-12-25 17:26:03 +08:00
2021-12-09 19:33:33 +08:00
`` repeat-jump `` and `` repeat-jump-reverse ``
redo the last jump in the same/opposite direction
2019-12-25 17:26:03 +08:00
2021-12-09 19:33:33 +08:00
`` kill-bigword ``
move the next whitespace-delimited word to the killring
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` kill-line ``
move everything from the cursor to the end of the line to the killring
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` kill-selection ``
move the selected text to the killring
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` kill-whole-line ``
move the line to the killring
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` kill-word ``
move the next word to the killring
2018-12-17 05:08:41 +08:00
2021-12-09 00:34:38 +08:00
`` nextd-or-forward-word ``
if the commandline is empty, then move forward in the directory history, otherwise move one word to the right
2021-12-09 19:33:33 +08:00
`` or ``
only execute the next function if the previous succeeded (note: only some functions report success)
2020-07-23 01:18:24 +08:00
2021-12-09 19:33:33 +08:00
`` pager-toggle-search ``
toggles the search field if the completions pager is visible.
2018-12-17 05:08:41 +08:00
2021-12-09 00:34:38 +08:00
`` prevd-or-backward-word ``
if the commandline is empty, then move backward in the directory history, otherwise move one word to the left
2021-12-09 19:33:33 +08:00
`` repaint ``
reexecutes the prompt functions and redraws the prompt (also `` force-repaint `` for backwards-compatibility)
2019-04-01 21:52:21 +08:00
2021-12-09 19:33:33 +08:00
`` repaint-mode ``
reexecutes the :ref: `fish_mode_prompt <cmd-fish_mode_prompt>` and redraws the prompt. This is useful for vi-mode. If no `` fish_mode_prompt `` exists or it prints nothing, it acts like a normal repaint.
2019-04-01 21:52:21 +08:00
2021-12-09 19:33:33 +08:00
`` self-insert ``
inserts the matching sequence into the command line
2020-03-05 06:10:24 +08:00
2021-12-09 19:33:33 +08:00
`` self-insert-notfirst ``
inserts the matching sequence into the command line, unless the cursor is at the beginning
2020-03-05 06:10:24 +08:00
2021-12-09 19:33:33 +08:00
`` suppress-autosuggestion ``
remove the current autosuggestion. Returns true if there was a suggestion to remove.
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` swap-selection-start-stop ``
go to the other end of the highlighted text without changing the selection
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` transpose-chars ``
transpose two characters to the left of the cursor
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` transpose-words ``
transpose two words to the left of the cursor
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` togglecase-char ``
toggle the capitalisation (case) of the character under the cursor
2021-03-31 10:41:10 +08:00
2021-12-09 19:33:33 +08:00
`` togglecase-selection ``
toggle the capitalisation (case) of the selection
2021-03-31 10:41:10 +08:00
2021-12-09 19:33:33 +08:00
`` insert-line-under ``
add a new line under the current line
2020-12-20 06:32:17 +08:00
2021-12-09 19:33:33 +08:00
`` insert-line-over ``
add a new line over the current line
2020-12-20 06:32:17 +08:00
2021-12-09 19:33:33 +08:00
`` up-line ``
move up one line
2019-10-09 00:01:32 +08:00
2021-12-09 19:33:33 +08:00
`` undo `` and `` redo ``
revert or redo the most recent edits on the command line
2020-02-04 19:47:44 +08:00
2021-12-09 19:33:33 +08:00
`` upcase-word ``
make the current word uppercase
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` yank ``
insert the latest entry of the killring into the buffer
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
`` yank-pop ``
rotate to the previous entry of the killring
2018-12-17 05:08:41 +08:00
2021-03-31 10:41:10 +08:00
Additional functions
--------------------
The following functions are included as normal functions, but are particularly useful for input editing:
2021-12-09 19:33:33 +08:00
`` up-or-search `` and `` down-or-search ``
move the cursor or search the history depending on the cursor position and current mode
2021-03-31 10:41:10 +08:00
2021-12-09 19:33:33 +08:00
`` edit_command_buffer ``
2021-12-09 19:53:13 +08:00
open the visual editor (controlled by the :envvar: `VISUAL` or :envvar: `EDITOR` environment variables) with the current command-line contents
2021-03-31 10:41:10 +08:00
2021-12-09 19:33:33 +08:00
`` fish_clipboard_copy ``
copy the current selection to the system clipboard
2021-03-31 10:41:10 +08:00
2021-12-09 19:33:33 +08:00
`` fish_clipboard_paste ``
paste the current selection from the system clipboard before the cursor
2021-03-31 10:41:10 +08:00
2021-12-09 19:33:33 +08:00
`` fish_commandline_append ``
append 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.
2021-06-23 14:55:33 +08:00
2021-12-09 19:33:33 +08:00
`` fish_commandline_prepend ``
prepend 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.
2021-06-23 14:55:33 +08:00
2018-12-19 10:44:30 +08:00
Examples
2019-01-03 12:10:47 +08:00
--------
2018-12-17 05:08:41 +08:00
2020-10-11 03:22:34 +08:00
Exit the shell when :kbd: `Control` \ +\ :kbd: `D` is pressed::
2018-12-19 11:14:04 +08:00
2019-04-27 01:23:08 +08:00
bind \cd 'exit'
2018-12-19 11:14:04 +08:00
2020-10-11 03:22:34 +08:00
Perform a history search when :kbd: `Page Up` is pressed::
2018-12-19 11:14:04 +08:00
bind -k ppage history-search-backward
2021-04-14 21:43:14 +08:00
Turn on :ref: `Vi key bindings <vi-mode>` and rebind :kbd: `Control` \ +\ :kbd: `C` to clear the input line::
2018-12-19 11:14:04 +08:00
set -g fish_key_bindings fish_vi_key_bindings
2020-08-07 14:19:50 +08:00
bind -M insert \cc kill-whole-line repaint
2018-12-19 11:14:04 +08:00
2020-10-11 03:22:34 +08:00
Launch `` git diff `` and repaint the commandline afterwards when :kbd: `Control` \ +\ :kbd: `G` is pressed::
2018-12-17 05:08:41 +08:00
2020-05-19 02:48:27 +08:00
bind \cg 'git diff; commandline -f repaint'
.. _cmd-bind-termlimits:
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, the control key modifies a character by setting the top three bits to 0. This means:
2021-11-04 22:21:28 +08:00
- Many characters + control are indistinguishable from other keys. :kbd: `Control` \ +\ :kbd: `I` *is* tab, :kbd: `Control` \ +\ :kbd: `J` *is* newline (`` \n `` ).
2020-05-19 02:48:27 +08:00
- Control and shift don't work simultaneously
2020-10-27 02:19:05 +08:00
Other keys don't have a direct encoding, and are sent as escape sequences. For example :kbd: `→` (Right) often sends `` \e\[C `` . These can differ from terminal to terminal, and the mapping is typically available in `terminfo(5)` . Sometimes however a terminal identifies as e.g. `` xterm-256color `` for compatibility, but then implements xterm's sequences incorrectly.
2020-05-19 02:48:27 +08:00
2019-10-31 21:12:57 +08:00
.. _cmd-bind-escape:
2018-12-17 05:08:41 +08:00
2020-05-19 02:48:27 +08:00
Special Case: The Escape Character
2019-01-03 12:10:47 +08:00
----------------------------------
2018-12-17 05:08:41 +08:00
2020-02-22 19:36:30 +08:00
The escape key can be used standalone, for example, to switch from insertion mode to normal mode when using Vi keybindings. Escape can also be used as a "meta" key, to indicate the start of an escape sequence, like for function or arrow keys. Custom bindings can also be defined that begin with an escape character.
2018-12-17 05:08:41 +08:00
2020-02-22 19:36:30 +08:00
Holding alt and something else also typically sends escape, for example holding alt+a will send an escape character and then an "a".
2018-12-17 05:08:41 +08:00
2020-02-22 19:36:30 +08:00
fish waits for a period after receiving the escape character, to determine whether it is standalone or part of an escape sequence. While waiting, additional key presses make the escape key behave as a meta key. If no other key presses come in, it is handled as a standalone escape. The waiting period is set to 30 milliseconds (0.03 seconds). It can be configured by setting the `` fish_escape_delay_ms `` variable to a value between 10 and 5000 ms. This can be a universal variable that you set once from an interactive session.