From 6da4b682db11454cb0412675bb76d5bc5ad7bc55 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Tue, 10 Dec 2019 21:05:21 +0100 Subject: [PATCH] docs: Separate scripting/interactive sections [ci skip] --- sphinx_doc_src/index.rst | 352 ++++++++++++++++++++------------------- 1 file changed, 179 insertions(+), 173 deletions(-) diff --git a/sphinx_doc_src/index.rst b/sphinx_doc_src/index.rst index f73aef058..d856384ac 100644 --- a/sphinx_doc_src/index.rst +++ b/sphinx_doc_src/index.rst @@ -410,181 +410,10 @@ The ``switch`` command is used to execute one of possibly many blocks of command The other conditionals use the `exit status <#variables-status>`_ of a command to decide if a command or a block of commands should be executed. See their documentation for more information. -Help -==== - -``fish`` has an extensive help system. Use the :ref:`help ` command to obtain help on a specific subject or command. For instance, writing ``help syntax`` displays the `syntax section <#syntax>`_ of this documentation. - -``fish`` also has man pages for its commands. For example, ``man set`` will show the documentation for ``set`` as a man page. - -Help on a specific builtin can also be obtained with the ``-h`` parameter. For instance, to obtain help on the ``fg`` builtin, either type ``fg -h`` or ``help fg``. - -Autosuggestions ---------------- - -fish suggests commands as you type, based on `command history <#history-search>`_, completions, and valid file paths. As you type commands, you will see a suggestion offered after the cursor, in a muted gray color (which can be changed with the ``fish_color_autosuggestion`` variable). - -To accept the autosuggestion (replacing the command line contents), press right arrow or :kbd:`Control+F`. To accept the first suggested word, press :kbd:`Alt+→,Right` or :kbd:`Alt+F`. If the autosuggestion is not what you want, just ignore it: it won't execute unless you accept it. - -Autosuggestions are a powerful way to quickly summon frequently entered commands, by typing the first few characters. They are also an efficient technique for navigating through directory hierarchies. - - -Tab Completion --------------- - -Tab completion is one of the most time saving features of any modern shell. By tapping the tab key, the user asks ``fish`` to guess the rest of the command or parameter that the user is currently typing. If ``fish`` can only find one possible completion, ``fish`` will write it out. If there is more than one completion, ``fish`` will write out the longest prefix that all completions have in common. If the completions differ on the first character, a list of all possible completions is printed. The list features descriptions of the completions and if the list doesn't fit the screen, it is scrollable by using the arrow keys, the page up/page down keys, the tab key or the space bar. - -If the list is visible, pressing control-S (or the ``pager-toggle-search`` binding) will allow filtering the list. Shift-tab (or the ``complete-and-search`` binding) will trigger completion with the search field immediately visible. -These are the general purpose tab completions that ``fish`` provides: - -- Completion of commands (builtins, functions and regular programs). - -- Completion of shell variable names. - -- Completion of usernames for tilde expansion. - -- Completion of filenames, even on strings with wildcards such as '``*``' and '``**``'. - -``fish`` provides a large number of program specific completions. Most of these completions are simple options like the ``-l`` option for ``ls``, but some are more advanced. The latter include: - -- The programs ``man`` and ``whatis`` show all installed manual pages as completions. - -- The ``make`` program uses all targets in the Makefile in the current directory as completions. - -- The ``mount`` command uses all mount points specified in fstab as completions. - -- The ``ssh`` command uses all hosts that are stored in the known_hosts file as completions. (See the ssh documentation for more information) - -- The ``su`` command uses all users on the system as completions. - -- The ``apt-get``, ``rpm`` and ``yum`` commands use all installed packages as completions. - - -.. _completion-own: - -Writing your own completions ----------------------------- - -To specify a completion, use the ``complete`` command. ``complete`` takes as a parameter the name of the command to specify a completion for. For example, to add a completion for the program ``myprog``, one would start the completion command with ``complete -c myprog ...`` - -To provide a list of possible completions for myprog, use the ``-a`` switch. If ``myprog`` accepts the arguments start and stop, this can be specified as ``complete -c myprog -a 'start stop'``. The argument to the ``-a`` switch is always a single string. At completion time, it will be tokenized on spaces and tabs, and variable expansion, command substitution and other forms of parameter expansion will take place. - -``fish`` has a special syntax to support specifying switches accepted by a command. The switches ``-s``, ``-l`` and ``-o`` are used to specify a short switch (single character, such as ``-l``), a gnu style long switch (such as '``--color``') and an old-style long switch (like '``-shuffle``'), respectively. If the command 'myprog' has an option '-o' which can also be written as '``--output``', and which can take an additional value of either 'yes' or 'no', this can be specified by writing:: - - complete -c myprog -s o -l output -a "yes no" - - -There are also special switches for specifying that a switch requires an argument, to disable filename completion, to create completions that are only available in some combinations, etc.. For a complete description of the various switches accepted by the ``complete`` command, see the documentation for the :ref:`complete ` builtin, or write ``complete --help`` inside the ``fish`` shell. - -As a more comprehensive example, here's a commented excerpt of the completions for systemd's ``timedatectl``:: - - # All subcommands that timedatectl knows - this is useful for later. - set -l commands status set-time set-timezone list-timezones set-local-rtc set-ntp - - # Disable file completions for the entire command - # because it does not take files anywhere - # Note that this can be undone by using "-F". - # - # File completions also need to be disabled - # if you want to have more control over what files are offered (e.g. just directories, or just files ending in ".mp3"). - complete -c timedatectl -f - - # This line offers the subcommands - # -"status", - # -"set-timezone", - # -"set-time" - # -"list-timezones" - # if no subcommand has been given so far. - # - # The `-n`/`--condition` option takes script as a string, which it executes. - # If it returns true, the completion is offered. - # Here the condition is the `__fish_seen_subcommands_from` helper function. - # If returns true if any of the given commands is used on the commandline, - # as determined by a simple heuristic. - # For more complex uses, you can write your own function. - # See e.g. the git completions for an example. - # - complete -c timedatectl -n "not __fish_seen_subcommand_from $commands" -a "status set-time set-timezone list-timezones" - - # If the "set-timezone" subcommand is used, - # offer the output of `timedatectl list-timezones` as completions. - # Each line of output is used as a separate candidate, - # and anything after a tab is taken as the description. - # It's often useful to transform command output with `string` into that form. - complete -c timedatectl -n "__fish_seen_subcommand_from set-timezone" -a "(timedatectl list-timezones)" - - # Completion candidates can also be described via `-d`, - # which is useful if the description is constant. - # Try to keep these short, because that means the user gets to see more at once. - complete -c timedatectl -n "not __fish_seen_subcommand_from $commands" -a "set-local-rtc" -d "Maintain RTC in local time" - - # We can also limit options to certain subcommands by using conditions. - complete -c timedatectl -n "__fish_seen_subcommand_from set-local-rtc" -l adjust-system-clock -d 'Synchronize system clock from the RTC' - - # These are simple options that can be used everywhere. - complete -c timedatectl -s h -l help -d 'Print a short help text and exit' - complete -c timedatectl -l version -d 'Print a short version string and exit' - complete -c timedatectl -l no-pager -d 'Do not pipe output into a pager' - -For examples of how to write your own complex completions, study the completions in ``/usr/share/fish/completions``. (The exact path depends on your chosen installation prefix and may be slightly different) - -.. _completion-func: - -Useful functions for writing completions ----------------------------------------- - -``fish`` ships with several functions that are very useful when writing command specific completions. Most of these functions name begins with the string '``__fish_``'. Such functions are internal to ``fish`` and their name and interface may change in future fish versions. Still, some of them may be very useful when writing completions. A few of these functions are described here. Be aware that they may be removed or changed in future versions of fish. - -Functions beginning with the string ``__fish_print_`` print a newline separated list of strings. For example, ``__fish_print_filesystems`` prints a list of all known file systems. Functions beginning with ``__fish_complete_`` print out a newline separated list of completions with descriptions. The description is separated from the completion by a tab character. - -- ``__fish_complete_directories STRING DESCRIPTION`` performs path completion on STRING, allowing only directories, and giving them the description DESCRIPTION. - -- ``__fish_complete_path STRING DESCRIPTION`` performs path completion on STRING, giving them the description DESCRIPTION. - -- ``__fish_complete_groups`` prints a list of all user groups with the groups members as description. - -- ``__fish_complete_pids`` prints a list of all processes IDs with the command name as description. - -- ``__fish_complete_suffix SUFFIX`` performs file completion allowing only files ending in SUFFIX, with an optional description. - -- ``__fish_complete_users`` prints a list of all users with their full name as description. - -- ``__fish_print_filesystems`` prints a list of all known file systems. Currently, this is a static list, and not dependent on what file systems the host operating system actually understands. - -- ``__fish_print_hostnames`` prints a list of all known hostnames. This functions searches the fstab for nfs servers, ssh for known hosts and checks the ``/etc/hosts`` file. - -- ``__fish_print_interfaces`` prints a list of all known network interfaces. - -- ``__fish_print_packages`` prints a list of all installed packages. This function currently handles Debian, rpm and Gentoo packages. - -.. _completion-path: - -Where to put completions ------------------------- - -Completions can be defined on the commandline or in a configuration file, but they can also be automatically loaded. Fish automatically searches through any directories in the list variable ``$fish_complete_path``, and any completions defined are automatically loaded when needed. A completion file must have a filename consisting of the name of the command to complete and the suffix '``.fish``'. - -By default, Fish searches the following for completions, using the first available file that it finds: - -- A directory for end-users to keep their own completions, usually ``~/.config/fish/completions`` (controlled by the ``XDG_CONFIG_HOME`` environment variable); -- A directory for systems administrators to install completions for all users on the system, usually ``/etc/fish/completions``; -- A directory for third-party software vendors to ship their own completions for their software, usually ``/usr/share/fish/vendor_completions.d``; -- The completions shipped with fish, usually installed in ``/usr/share/fish/completions``; and -- Completions automatically generated from the operating system's manual, usually stored in ``~/.local/share/fish/generated_completions``. - -These paths are controlled by parameters set at build, install, or run time, and may vary from the defaults listed above. - -This wide search may be confusing. If you are unsure, your completions probably belong in ``~/.config/fish/completions``. - -If you have written new completions for a common Unix command, please consider sharing your work by submitting it via the instructions in `Further help and development <#more-help>`_. - -If you are developing another program and would like to ship completions with your program, install them to the "vendor" completions directory. As this path may vary from system to system, the ``pkgconfig`` framework should be used to discover this path with the output of ``pkg-config --variable completionsdir fish``. - - .. _expand: Parameter expansion (Globbing) -============================== +------------------------------ When an argument for a program is given on the commandline, it undergoes the process of parameter expansion before it is sent on to the command. Parameter expansion is a powerful mechanism that allows you to expand the parameter in various ways, including performing wildcard matching on files, inserting the value of a shell variable into the parameter or even using the output of another command as a parameter list. @@ -1317,12 +1146,189 @@ The most common way to set the locale to use a command like 'set -x LANG en_GB.u .. _builtin-overview: Builtin commands -================ +---------------- Many other shells have a large library of builtin commands. Most of these commands are also available as standalone commands, but have been implemented in the shell anyway. To avoid code duplication, and to avoid the confusion of subtly differing versions of the same command, ``fish`` generally only implements builtins for actions which cannot be performed by a regular command. For a list of all builtins, functions and commands shipped with fish, see the :ref:`list of commands `. The documentation is also available by using the ``--help`` switch of the command. +Interactive use +=============== + +Fish prides itself on being really nice to use interactively. That's down to a few features we'll explain in the next few sections. + + +Help +---- + +``fish`` has an extensive help system. Use the :ref:`help ` command to obtain help on a specific subject or command. For instance, writing ``help syntax`` displays the `syntax section <#syntax>`_ of this documentation. + +``fish`` also has man pages for its commands. For example, ``man set`` will show the documentation for ``set`` as a man page. + +Help on a specific builtin can also be obtained with the ``-h`` parameter. For instance, to obtain help on the ``fg`` builtin, either type ``fg -h`` or ``help fg``. + +Autosuggestions +--------------- + +fish suggests commands as you type, based on `command history <#history-search>`_, completions, and valid file paths. As you type commands, you will see a suggestion offered after the cursor, in a muted gray color (which can be changed with the ``fish_color_autosuggestion`` variable). + +To accept the autosuggestion (replacing the command line contents), press right arrow or :kbd:`Control+F`. To accept the first suggested word, press :kbd:`Alt+→,Right` or :kbd:`Alt+F`. If the autosuggestion is not what you want, just ignore it: it won't execute unless you accept it. + +Autosuggestions are a powerful way to quickly summon frequently entered commands, by typing the first few characters. They are also an efficient technique for navigating through directory hierarchies. + + +Tab Completion +-------------- + +Tab completion is one of the most time saving features of any modern shell. By tapping the tab key, the user asks ``fish`` to guess the rest of the command or parameter that the user is currently typing. If ``fish`` can only find one possible completion, ``fish`` will write it out. If there is more than one completion, ``fish`` will write out the longest prefix that all completions have in common. If the completions differ on the first character, a list of all possible completions is printed. The list features descriptions of the completions and if the list doesn't fit the screen, it is scrollable by using the arrow keys, the page up/page down keys, the tab key or the space bar. + +If the list is visible, pressing control-S (or the ``pager-toggle-search`` binding) will allow filtering the list. Shift-tab (or the ``complete-and-search`` binding) will trigger completion with the search field immediately visible. +These are the general purpose tab completions that ``fish`` provides: + +- Completion of commands (builtins, functions and regular programs). + +- Completion of shell variable names. + +- Completion of usernames for tilde expansion. + +- Completion of filenames, even on strings with wildcards such as '``*``' and '``**``'. + +``fish`` provides a large number of program specific completions. Most of these completions are simple options like the ``-l`` option for ``ls``, but some are more advanced. The latter include: + +- The programs ``man`` and ``whatis`` show all installed manual pages as completions. + +- The ``make`` program uses all targets in the Makefile in the current directory as completions. + +- The ``mount`` command uses all mount points specified in fstab as completions. + +- The ``ssh`` command uses all hosts that are stored in the known_hosts file as completions. (See the ssh documentation for more information) + +- The ``su`` command uses all users on the system as completions. + +- The ``apt-get``, ``rpm`` and ``yum`` commands use all installed packages as completions. + + +.. _completion-own: + +Writing your own completions +---------------------------- + +To specify a completion, use the ``complete`` command. ``complete`` takes as a parameter the name of the command to specify a completion for. For example, to add a completion for the program ``myprog``, one would start the completion command with ``complete -c myprog ...`` + +To provide a list of possible completions for myprog, use the ``-a`` switch. If ``myprog`` accepts the arguments start and stop, this can be specified as ``complete -c myprog -a 'start stop'``. The argument to the ``-a`` switch is always a single string. At completion time, it will be tokenized on spaces and tabs, and variable expansion, command substitution and other forms of parameter expansion will take place. + +``fish`` has a special syntax to support specifying switches accepted by a command. The switches ``-s``, ``-l`` and ``-o`` are used to specify a short switch (single character, such as ``-l``), a gnu style long switch (such as '``--color``') and an old-style long switch (like '``-shuffle``'), respectively. If the command 'myprog' has an option '-o' which can also be written as '``--output``', and which can take an additional value of either 'yes' or 'no', this can be specified by writing:: + + complete -c myprog -s o -l output -a "yes no" + + +There are also special switches for specifying that a switch requires an argument, to disable filename completion, to create completions that are only available in some combinations, etc.. For a complete description of the various switches accepted by the ``complete`` command, see the documentation for the :ref:`complete ` builtin, or write ``complete --help`` inside the ``fish`` shell. + +As a more comprehensive example, here's a commented excerpt of the completions for systemd's ``timedatectl``:: + + # All subcommands that timedatectl knows - this is useful for later. + set -l commands status set-time set-timezone list-timezones set-local-rtc set-ntp + + # Disable file completions for the entire command + # because it does not take files anywhere + # Note that this can be undone by using "-F". + # + # File completions also need to be disabled + # if you want to have more control over what files are offered (e.g. just directories, or just files ending in ".mp3"). + complete -c timedatectl -f + + # This line offers the subcommands + # -"status", + # -"set-timezone", + # -"set-time" + # -"list-timezones" + # if no subcommand has been given so far. + # + # The `-n`/`--condition` option takes script as a string, which it executes. + # If it returns true, the completion is offered. + # Here the condition is the `__fish_seen_subcommands_from` helper function. + # If returns true if any of the given commands is used on the commandline, + # as determined by a simple heuristic. + # For more complex uses, you can write your own function. + # See e.g. the git completions for an example. + # + complete -c timedatectl -n "not __fish_seen_subcommand_from $commands" -a "status set-time set-timezone list-timezones" + + # If the "set-timezone" subcommand is used, + # offer the output of `timedatectl list-timezones` as completions. + # Each line of output is used as a separate candidate, + # and anything after a tab is taken as the description. + # It's often useful to transform command output with `string` into that form. + complete -c timedatectl -n "__fish_seen_subcommand_from set-timezone" -a "(timedatectl list-timezones)" + + # Completion candidates can also be described via `-d`, + # which is useful if the description is constant. + # Try to keep these short, because that means the user gets to see more at once. + complete -c timedatectl -n "not __fish_seen_subcommand_from $commands" -a "set-local-rtc" -d "Maintain RTC in local time" + + # We can also limit options to certain subcommands by using conditions. + complete -c timedatectl -n "__fish_seen_subcommand_from set-local-rtc" -l adjust-system-clock -d 'Synchronize system clock from the RTC' + + # These are simple options that can be used everywhere. + complete -c timedatectl -s h -l help -d 'Print a short help text and exit' + complete -c timedatectl -l version -d 'Print a short version string and exit' + complete -c timedatectl -l no-pager -d 'Do not pipe output into a pager' + +For examples of how to write your own complex completions, study the completions in ``/usr/share/fish/completions``. (The exact path depends on your chosen installation prefix and may be slightly different) + +.. _completion-func: + +Useful functions for writing completions +---------------------------------------- + +``fish`` ships with several functions that are very useful when writing command specific completions. Most of these functions name begins with the string '``__fish_``'. Such functions are internal to ``fish`` and their name and interface may change in future fish versions. Still, some of them may be very useful when writing completions. A few of these functions are described here. Be aware that they may be removed or changed in future versions of fish. + +Functions beginning with the string ``__fish_print_`` print a newline separated list of strings. For example, ``__fish_print_filesystems`` prints a list of all known file systems. Functions beginning with ``__fish_complete_`` print out a newline separated list of completions with descriptions. The description is separated from the completion by a tab character. + +- ``__fish_complete_directories STRING DESCRIPTION`` performs path completion on STRING, allowing only directories, and giving them the description DESCRIPTION. + +- ``__fish_complete_path STRING DESCRIPTION`` performs path completion on STRING, giving them the description DESCRIPTION. + +- ``__fish_complete_groups`` prints a list of all user groups with the groups members as description. + +- ``__fish_complete_pids`` prints a list of all processes IDs with the command name as description. + +- ``__fish_complete_suffix SUFFIX`` performs file completion allowing only files ending in SUFFIX, with an optional description. + +- ``__fish_complete_users`` prints a list of all users with their full name as description. + +- ``__fish_print_filesystems`` prints a list of all known file systems. Currently, this is a static list, and not dependent on what file systems the host operating system actually understands. + +- ``__fish_print_hostnames`` prints a list of all known hostnames. This functions searches the fstab for nfs servers, ssh for known hosts and checks the ``/etc/hosts`` file. + +- ``__fish_print_interfaces`` prints a list of all known network interfaces. + +- ``__fish_print_packages`` prints a list of all installed packages. This function currently handles Debian, rpm and Gentoo packages. + +.. _completion-path: + +Where to put completions +------------------------ + +Completions can be defined on the commandline or in a configuration file, but they can also be automatically loaded. Fish automatically searches through any directories in the list variable ``$fish_complete_path``, and any completions defined are automatically loaded when needed. A completion file must have a filename consisting of the name of the command to complete and the suffix '``.fish``'. + +By default, Fish searches the following for completions, using the first available file that it finds: + +- A directory for end-users to keep their own completions, usually ``~/.config/fish/completions`` (controlled by the ``XDG_CONFIG_HOME`` environment variable); +- A directory for systems administrators to install completions for all users on the system, usually ``/etc/fish/completions``; +- A directory for third-party software vendors to ship their own completions for their software, usually ``/usr/share/fish/vendor_completions.d``; +- The completions shipped with fish, usually installed in ``/usr/share/fish/completions``; and +- Completions automatically generated from the operating system's manual, usually stored in ``~/.local/share/fish/generated_completions``. + +These paths are controlled by parameters set at build, install, or run time, and may vary from the defaults listed above. + +This wide search may be confusing. If you are unsure, your completions probably belong in ``~/.config/fish/completions``. + +If you have written new completions for a common Unix command, please consider sharing your work by submitting it via the instructions in `Further help and development <#more-help>`_. + +If you are developing another program and would like to ship completions with your program, install them to the "vendor" completions directory. As this path may vary from system to system, the ``pkgconfig`` framework should be used to discover this path with the output of ``pkg-config --variable completionsdir fish``. + + .. _editor: Command line editor