2019-03-31 17:05:09 +08:00
.. _cmd-abbr:
2018-12-17 09:39:33 +08:00
abbr - manage fish abbreviations
2019-01-03 12:10:47 +08:00
================================
2018-12-17 09:39:33 +08:00
2018-12-18 09:58:24 +08:00
Synopsis
--------
2018-12-17 05:08:41 +08:00
docs synopsis: add HTML highlighing and automate manpage markup
Recent synopsis changes move from literal code blocks to
[RST line blocks]. This does not translate well to HTML: it's not
rendered in monospace, so aligment is lost. Additionally, we don't
get syntax highlighting in HTML, which adds differences to our code
samples which are highlighted.
We hard-wrap synopsis lines (like code blocks). To align continuation
lines in manpages we need [backslashes in weird places]. Combined with
the **, *, and `` markup, it's a bit hard to get the alignment right.
Fix these by moving synopsis sources back to code blocks and compute
HTML syntax highlighting and manpage markup with a custom Sphinx
extension.
The new Pygments lexer can tokenize a synopsis and assign the various
highlighting roles, which closely matches fish's syntax highlighing:
- command/keyword (dark blue)
- parameter (light blue)
- operator like and/or/not/&&/|| (cyan)
- grammar metacharacter (black)
For manpage output, we don't project the fish syntax highlighting
but follow the markup convention in GNU's man(1):
bold text type exactly as shown.
italic text replace with appropriate argument.
To make it easy to separate these two automatically, formalize that
(italic) placeholders must be uppercase; while all lowercase text is
interpreted literally (so rendered bold).
This makes manpages more consistent, see string-join(1) and and(1).
Implementation notes:
Since we want manpage formatting but Sphinx's Pygments highlighing
plugin does not support manpage output, add our custom "synopsis"
directive. This directive parses differently when manpage output is
specified. This means that the HTML and manpage build processes must
not share a cache, because the parsed doctrees are cached. Work around
this by using separate cache locations for build targets "sphinx-docs"
(which creates HTML) and "sphinx-manpages". A better solution would
be to only override Sphinx's ManualPageBuilder but that would take a
bit more code (ideally we could override ManualPageWriter but Sphinx
4.3.2 doesn't really support that).
---
Alternative solution: stick with line blocks but use roles like
:command: or :option: (or custom ones). While this would make it
possible to produce HTML that is consistent with code blocks (by adding
a bit of CSS), the source would look uglier and is harder to maintain.
(Let's say we want to add custom formatting to the [|] metacharacters
in HTML. This is much easier with the proposed patch.)
---
[RST line blocks]: https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#line-blocks
[backslashes in weird places]: https://github.com/fish-shell/fish-shell/pull/8626#discussion_r782837750
2022-01-09 22:09:46 +08:00
.. synopsis ::
2022-12-11 06:16:26 +08:00
abbr --add NAME [--position command | anywhere] [-r | --regex PATTERN]
2022-11-28 06:33:35 +08:00
[--set-cursor[=MARKER]]
2022-08-23 02:05:20 +08:00
[-f | --function] EXPANSION
abbr --erase NAME ...
abbr --rename OLD_WORD NEW_WORD
docs synopsis: add HTML highlighing and automate manpage markup
Recent synopsis changes move from literal code blocks to
[RST line blocks]. This does not translate well to HTML: it's not
rendered in monospace, so aligment is lost. Additionally, we don't
get syntax highlighting in HTML, which adds differences to our code
samples which are highlighted.
We hard-wrap synopsis lines (like code blocks). To align continuation
lines in manpages we need [backslashes in weird places]. Combined with
the **, *, and `` markup, it's a bit hard to get the alignment right.
Fix these by moving synopsis sources back to code blocks and compute
HTML syntax highlighting and manpage markup with a custom Sphinx
extension.
The new Pygments lexer can tokenize a synopsis and assign the various
highlighting roles, which closely matches fish's syntax highlighing:
- command/keyword (dark blue)
- parameter (light blue)
- operator like and/or/not/&&/|| (cyan)
- grammar metacharacter (black)
For manpage output, we don't project the fish syntax highlighting
but follow the markup convention in GNU's man(1):
bold text type exactly as shown.
italic text replace with appropriate argument.
To make it easy to separate these two automatically, formalize that
(italic) placeholders must be uppercase; while all lowercase text is
interpreted literally (so rendered bold).
This makes manpages more consistent, see string-join(1) and and(1).
Implementation notes:
Since we want manpage formatting but Sphinx's Pygments highlighing
plugin does not support manpage output, add our custom "synopsis"
directive. This directive parses differently when manpage output is
specified. This means that the HTML and manpage build processes must
not share a cache, because the parsed doctrees are cached. Work around
this by using separate cache locations for build targets "sphinx-docs"
(which creates HTML) and "sphinx-manpages". A better solution would
be to only override Sphinx's ManualPageBuilder but that would take a
bit more code (ideally we could override ManualPageWriter but Sphinx
4.3.2 doesn't really support that).
---
Alternative solution: stick with line blocks but use roles like
:command: or :option: (or custom ones). While this would make it
possible to produce HTML that is consistent with code blocks (by adding
a bit of CSS), the source would look uglier and is harder to maintain.
(Let's say we want to add custom formatting to the [|] metacharacters
in HTML. This is much easier with the proposed patch.)
---
[RST line blocks]: https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#line-blocks
[backslashes in weird places]: https://github.com/fish-shell/fish-shell/pull/8626#discussion_r782837750
2022-01-09 22:09:46 +08:00
abbr --show
abbr --list
2022-08-23 02:05:20 +08:00
abbr --query NAME ...
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
2018-12-20 04:02:45 +08:00
`` abbr `` manages abbreviations - user-defined words that are replaced with longer phrases after they are entered.
2018-12-17 05:08:41 +08:00
2021-12-09 19:33:33 +08:00
For example, a frequently-run command like `` git checkout `` can be abbreviated to `` gco `` .
After entering `` gco `` and pressing :kbd: `Space` or :kbd: `Enter` , the full text `` git checkout `` will appear in the command line.
2018-12-17 05:08:41 +08:00
2022-12-11 04:45:21 +08:00
An abbreviation may match a literal word, or it may match a pattern given by a regular expression. When an abbreviation matches a word, that word is replaced by new text, called its *expansion* . This expansion may be a fixed new phrase, or it can be dynamically created via a fish function. This expansion occurs after pressing space or enter.
2018-12-17 05:08:41 +08:00
2022-08-23 02:05:20 +08:00
Combining these features, it is possible to create custom syntaxes, where a regular expression recognizes matching tokens, and the expansion function interprets them. See the `Examples`_ section.
2018-12-17 05:08:41 +08:00
2022-08-23 02:05:20 +08:00
Abbreviations may be added to :ref: `config.fish <configuration>` . Abbreviations are only expanded for typed-in commands, not in scripts.
2018-12-17 05:08:41 +08:00
2022-08-23 02:05:20 +08:00
"add" subcommand
--------------------
2018-12-17 05:08:41 +08:00
2022-08-23 02:05:20 +08:00
.. synopsis ::
2022-12-11 06:16:26 +08:00
abbr [-a | --add] NAME [--position command | anywhere] [-r | --regex PATTERN]
[--set-cursor[=MARKER]] [-f | --function] EXPANSION
2022-08-23 02:05:20 +08:00
`` abbr --add `` creates a new abbreviation. With no other options, the string **NAME** is replaced by **EXPANSION** .
With **--position command** , the abbreviation will only expand when it is positioned as a command, not as an argument to another command. With **--position anywhere** the abbreviation may expand anywhere in the command line. The default is **command** .
2019-02-25 07:01:16 +08:00
2022-08-23 02:05:20 +08:00
With **--regex** , the abbreviation matches using the regular expression given by **PATTERN** , instead of the literal **NAME** . The pattern is interpreted using PCRE2 syntax and must match the entire token. If multiple abbreviations match the same token, the last abbreviation added is used.
2022-03-12 00:18:41 +08:00
2022-11-28 06:33:35 +08:00
With **--set-cursor=MARKER** , the cursor is moved to the first occurrence of **MARKER** in the expansion. The **MARKER** value is erased. The **MARKER** may be omitted (i.e. simply `` --set-cursor `` ), in which case it defaults to `` % `` .
2018-12-17 05:08:41 +08:00
2022-08-23 02:05:20 +08:00
With **-f** or **--function** , **EXPANSION** is treated as the name of a fish function instead of a literal replacement. When the abbreviation matches, the function will be called with the matching token as an argument. If the function's exit status is 0 (success), the token will be replaced by the function's output; otherwise the token will be left unchanged.
2018-12-17 05:08:41 +08:00
2018-12-19 10:44:30 +08:00
Examples
2022-08-23 02:05:20 +08:00
########
2018-12-17 05:08:41 +08:00
2018-12-19 11:14:04 +08:00
::
2022-08-23 02:05:20 +08:00
abbr --add gco git checkout
2018-12-19 11:14:04 +08:00
2022-04-02 03:05:27 +08:00
Add a new abbreviation where `` gco `` will be replaced with `` git checkout `` .
2018-12-19 11:14:04 +08:00
::
2022-08-23 02:05:20 +08:00
abbr -a --position anywhere -- -C --color
Add a new abbreviation where `` -C `` will be replaced with `` --color `` . The `` -- `` allows `` -C `` to be treated as the name of the abbreviation, instead of an option.
::
2022-11-28 06:33:35 +08:00
abbr -a L --position anywhere --set-cursor "% | less"
2022-08-23 02:05:20 +08:00
Add a new abbreviation where `` L `` will be replaced with `` | less `` , placing the cursor before the pipe.
::
function last_history_item
echo $history[1]
end
2022-11-28 03:29:16 +08:00
abbr -a !! --position anywhere --function last_history_item
2022-08-23 02:05:20 +08:00
2022-11-28 03:29:16 +08:00
This first creates a function `` last_history_item `` which outputs the last entered command. It then adds an abbreviation which replaces `` !! `` with the result of calling this function. Taken together, this is similar to the `` !! `` history expansion feature of bash.
2022-08-23 02:05:20 +08:00
::
function vim_edit
echo vim $argv
end
abbr -a vim_edit_texts --position command --regex ".+\.txt" --function vim_edit
This first creates a function `` vim_edit `` which prepends `` vim `` before its argument. It then adds an abbreviation which matches commands ending in `` .txt `` , and replaces the command with the result of calling this function. This allows text files to be "executed" as a command to open them in vim, similar to the "suffix alias" feature in zsh.
::
2022-12-11 04:45:21 +08:00
abbr 4DIRS --set-cursor ! "$(string join \n -- 'for dir in */' 'cd $dir' '!' 'cd ..' 'end')"
2022-08-23 02:05:20 +08:00
This creates an abbreviation "4DIRS" which expands to a multi-line loop "template." The template enters each directory and then leaves it. The cursor is positioned ready to enter the command to run in each directory, at the location of the `` ! `` , which is itself erased.
Other subcommands
--------------------
::
2022-12-11 06:16:26 +08:00
abbr --rename OLD_NAME NEW_NAME
2018-12-19 11:14:04 +08:00
2022-08-23 02:05:20 +08:00
Renames an abbreviation, from *OLD_NAME* to *NEW_NAME*
2018-12-19 11:14:04 +08:00
::
2022-08-23 02:05:20 +08:00
abbr [-s | --show]
2018-12-19 11:14:04 +08:00
2022-08-23 02:05:20 +08:00
Show all abbreviations in a manner suitable for import and export
2018-12-17 05:08:41 +08:00
2018-12-19 11:14:04 +08:00
::
2022-08-23 02:05:20 +08:00
abbr [-l | --list]
2018-12-19 11:14:04 +08:00
2022-08-23 02:05:20 +08:00
Prints the names of all abbreviation
2018-12-17 05:08:41 +08:00
2018-12-19 11:14:04 +08:00
::
2022-08-23 02:05:20 +08:00
abbr [-e | --erase] NAME
Erases the abbreviation with the given name
::
abbr -q or --query [NAME...]
Return 0 (true) if one of the *NAME* is an abbreviation.
::
2018-12-19 11:14:04 +08:00
2022-08-23 02:05:20 +08:00
abbr -h or --help
2018-12-17 05:08:41 +08:00
2022-08-23 02:05:20 +08:00
Displays help for the `abbr` command.
2018-12-17 05:08:41 +08:00