2019-03-15 04:52:25 +08:00
.. highlight :: fish-docs-samples
2019-02-25 09:13:01 +08:00
2019-12-11 01:47:14 +08:00
.. _tutorial:
2019-02-25 09:13:01 +08:00
Tutorial
========
Why fish?
---------
2020-09-21 02:12:50 +08:00
Fish is a fully-equipped command line shell (like bash or zsh) that is smart and user-friendly. Fish supports powerful features like syntax highlighting, autosuggestions, and tab completions that just work, with nothing to learn or configure.
2019-02-25 09:13:01 +08:00
2020-09-21 02:12:50 +08:00
If you want to make your command line more productive, more useful, and more fun, without learning a bunch of arcane syntax and configuration options, then fish might be just what you're looking for!
2019-02-25 09:13:01 +08:00
Getting started
---------------
Once installed, just type in `` fish `` into your current shell to try it out!
You will be greeted by the standard fish prompt,
which means you are all set up and can start using fish::
> fish
2019-04-30 18:19:04 +08:00
Welcome to fish, the friendly interactive shell
2019-04-30 18:53:15 +08:00
Type help for instructions on how to use fish
2019-11-05 03:08:45 +08:00
you@hostname ~>
2019-02-25 09:13:01 +08:00
2020-09-21 02:12:50 +08:00
This prompt that you see above is the fish default prompt: it shows your username, hostname, and working directory.
2020-02-19 22:12:20 +08:00
- to change this prompt see `how to change your prompt <#prompt> `_
2019-02-25 09:13:01 +08:00
- to switch to fish permanently see `switch your default shell to fish <#switching-to-fish> `_ .
2020-04-14 04:54:33 +08:00
From now on, we'll pretend your prompt is just a `` > `` to save space.
2019-02-25 09:13:01 +08:00
Learning fish
-------------
2020-09-21 02:12:50 +08:00
This tutorial assumes a basic understanding of command line shells and Unix commands, and that you have a working copy of fish.
2019-02-25 09:13:01 +08:00
2020-09-21 02:12:50 +08:00
If you have a strong understanding of other shells, and want to know what fish does differently, search for the magic phrase *unlike other shells* , which is used to call out important differences.
2019-02-25 09:13:01 +08:00
Running Commands
----------------
2020-09-21 02:12:50 +08:00
Fish runs commands like other shells: you type a command, followed by its arguments. Spaces are separators::
2019-02-25 09:13:01 +08:00
2019-11-05 03:08:45 +08:00
> echo hello world
2019-04-30 18:19:04 +08:00
hello world
2019-02-25 09:13:01 +08:00
2020-10-27 02:19:05 +08:00
This runs the command `` echo `` with the arguments `` hello `` and `` world `` .
2019-06-07 01:38:41 +08:00
2019-02-25 09:13:01 +08:00
You can include a literal space in an argument with a backslash, or by using single or double quotes::
2019-11-05 03:08:45 +08:00
> mkdir My\ Files
> cp ~/Some\ File 'My Files'
> ls "My Files"
2019-04-30 18:19:04 +08:00
Some File
2019-02-25 09:13:01 +08:00
Commands can be chained with semicolons.
Getting Help
------------
2020-09-21 02:12:50 +08:00
Fish has excellent help and man pages. Run `` help `` to open help in a web browser, and `` man `` to open it in a man page. You can also ask for help with a specific command, for example, `` help set `` to open in a web browser, or `` man set `` to see it in the terminal.
2019-02-25 09:13:01 +08:00
::
2019-11-05 03:08:45 +08:00
> man set
2019-04-30 18:19:04 +08:00
set - handle shell variables
Synopsis...
2019-02-25 09:13:01 +08:00
Syntax Highlighting
-------------------
2020-02-23 01:01:05 +08:00
.. role :: red
.. role :: gray
2020-08-03 00:01:34 +08:00
.. role :: prompt
.. role :: command
.. role :: param
.. role :: param-valid-path
2019-02-25 09:13:01 +08:00
2020-08-03 00:01:34 +08:00
You'll quickly notice that fish performs syntax highlighting as you type. Invalid commands are colored red by default:
2019-02-25 09:13:01 +08:00
2020-08-03 00:01:34 +08:00
.. parsed-literal ::
2020-09-02 11:48:59 +08:00
:class: highlight
2019-02-25 09:13:01 +08:00
2020-08-03 00:01:34 +08:00
:prompt: `>` :red: `/bin/mkd`
2020-02-23 01:01:05 +08:00
2019-02-25 09:13:01 +08:00
A command may be invalid because it does not exist, or refers to a file that you cannot execute. When the command becomes valid, it is shown in a different color::
2019-11-05 03:08:45 +08:00
> /bin/mkdir
2019-02-25 09:13:01 +08:00
2020-08-03 00:01:34 +08:00
Valid file paths are underlined as you type them:
2019-02-25 09:13:01 +08:00
2020-08-03 00:01:34 +08:00
.. parsed-literal ::
2020-09-02 11:48:59 +08:00
:class: highlight
2020-08-03 00:01:34 +08:00
:prompt: `>` :command: `cat` :param-valid-path: `~/somefi`
2019-02-25 09:13:01 +08:00
2020-04-14 04:54:33 +08:00
This tells you that there exists a file that starts with `` somefi `` , which is useful feedback as you type.
2019-02-25 09:13:01 +08:00
2020-08-03 00:01:34 +08:00
These colors, and many more, can be changed by running `` fish_config `` , or by modifying :ref: `color variables <variables-color>` directly.
2019-02-25 09:13:01 +08:00
Wildcards
---------
2020-09-21 02:12:50 +08:00
Fish supports the familiar wildcard `` * `` . To list all JPEG files::
2019-02-25 09:13:01 +08:00
2019-11-05 03:08:45 +08:00
> ls *.jpg
2019-04-30 18:19:04 +08:00
lena.jpg
meena.jpg
santa maria.jpg
2019-02-25 09:13:01 +08:00
You can include multiple wildcards::
2019-11-05 03:08:45 +08:00
> ls l*.p*
2019-04-30 18:19:04 +08:00
lena.png
lesson.pdf
2019-02-25 09:13:01 +08:00
Especially powerful is the recursive wildcard ** which searches directories recursively::
2019-11-05 03:08:45 +08:00
> ls /var/**.log
2019-04-30 18:19:04 +08:00
/var/log/system.log
/var/run/sntp.log
2019-02-25 09:13:01 +08:00
2020-03-20 13:47:22 +08:00
If that directory traversal is taking a long time, you can :kbd: `Control` \ +\ :kbd: `C` out of it.
2019-02-25 09:13:01 +08:00
Pipes and Redirections
----------------------
You can pipe between commands with the usual vertical bar::
2019-11-05 03:08:45 +08:00
> echo hello world | wc
1 2 12
2019-02-25 09:13:01 +08:00
2020-10-27 02:19:05 +08:00
stdin and stdout can be redirected via the familiar `` < `` and `` > `` . stderr is redirected with a `` 2> `` .
2019-02-25 09:13:01 +08:00
::
2019-11-05 03:08:45 +08:00
> grep fish < /etc/shells > ~/output.txt 2> ~/errors.txt
2019-02-25 09:13:01 +08:00
2020-01-09 23:18:26 +08:00
To redirect stdout and stderr into one file, you need to first redirect stdout, and then stderr into stdout::
> make > make_output.txt 2>&1
2019-02-25 09:13:01 +08:00
Autosuggestions
---------------
2020-08-03 00:01:34 +08:00
As you type fish will suggest commands to the right of the cursor, in gray. For example:
.. parsed-literal ::
2020-09-02 11:48:59 +08:00
:class: highlight
2020-08-03 00:01:34 +08:00
:prompt: `>` :red: `/bin/h` :gray: `ostname`
2019-02-25 09:13:01 +08:00
2020-08-03 00:01:34 +08:00
It knows about paths and options:
2019-02-25 09:13:01 +08:00
2020-08-03 00:01:34 +08:00
.. parsed-literal ::
2020-09-02 11:48:59 +08:00
:class: highlight
2019-02-25 09:13:01 +08:00
2020-08-03 00:01:34 +08:00
:prompt: `>` :command: `grep` :param: `--i` :gray: `gnore-case`
2019-02-25 09:13:01 +08:00
2020-08-03 00:01:34 +08:00
And history too. Type a command once, and you can re-summon it by just typing a few letters:
2019-02-25 09:13:01 +08:00
2020-08-03 00:01:34 +08:00
.. parsed-literal ::
2020-09-02 11:48:59 +08:00
:class: highlight
2020-08-03 00:01:34 +08:00
:prompt: `>` :red: `r` :gray: `sync -avze ssh . myname@somelonghost.com:/some/long/path/doo/dee/doo/dee/doo`
2019-02-25 09:13:01 +08:00
2020-03-20 13:47:22 +08:00
To accept the autosuggestion, hit :kbd: `→` (right arrow) or :kbd: `Control` \ +\ :kbd: `F` . To accept a single word of the autosuggestion, :kbd: `Alt` \ +\ :kbd: `→` (right arrow). If the autosuggestion is not what you want, just ignore it.
2019-02-25 09:13:01 +08:00
Tab Completions
---------------
2020-08-03 00:01:34 +08:00
A rich set of tab completions work "out of the box".
Press :kbd: `Tab` and fish will attempt to complete the command, argument, or path:
2019-02-25 09:13:01 +08:00
2020-08-03 00:01:34 +08:00
.. parsed-literal ::
2020-09-02 11:48:59 +08:00
:class: highlight
2019-02-25 09:13:01 +08:00
2020-08-03 00:01:34 +08:00
:prompt: `>` :red: `/pri` :kbd: `Tab` => :command: `/private/`
2019-02-25 09:13:01 +08:00
2020-08-03 00:01:34 +08:00
If there's more than one possibility, it will list them:
2019-02-25 09:13:01 +08:00
2020-08-03 00:01:34 +08:00
.. parsed-literal ::
2020-09-02 11:48:59 +08:00
:class: highlight
2020-08-03 00:01:34 +08:00
:prompt: `>` :red: `~/stuff/s` :kbd: `Tab`
~/stuff/script.sh (Executable, 4.8kB) ~/stuff/sources/ (Directory)
2019-02-25 09:13:01 +08:00
Hit tab again to cycle through the possibilities.
2020-08-03 00:01:34 +08:00
fish can also complete many commands, like git branches:
.. parsed-literal ::
2020-09-02 11:48:59 +08:00
:class: highlight
2019-02-25 09:13:01 +08:00
2020-08-03 00:01:34 +08:00
:prompt: `>` :command: `git` :param: `merge pr` :kbd: `Tab` => :command: `git` :param: `merge prompt_designer`
:prompt: `>` :command: `git` :param: `checkout b` :kbd: `Tab`
2019-11-05 03:08:45 +08:00
builtin_list_io_merge (Branch) builtin_set_color (Branch) busted_events (Tag)
2019-02-25 09:13:01 +08:00
2020-08-03 00:01:34 +08:00
Try hitting tab and see what fish can do!
2019-02-25 09:13:01 +08:00
Variables
---------
Like other shells, a dollar sign performs variable substitution::
2019-11-05 03:08:45 +08:00
> echo My home directory is $HOME
2019-04-30 18:19:04 +08:00
My home directory is /home/tutorial
2019-02-25 09:13:01 +08:00
2019-06-07 01:38:41 +08:00
Variable substitution also happens in double quotes, but not single quotes::
2019-02-25 09:13:01 +08:00
2019-11-05 03:08:45 +08:00
> echo "My current directory is $PWD"
2019-04-30 18:19:04 +08:00
My current directory is /home/tutorial
2019-11-05 03:08:45 +08:00
> echo 'My current directory is $PWD'
2019-04-30 18:19:04 +08:00
My current directory is $PWD
2019-02-25 09:13:01 +08:00
2020-10-27 02:19:05 +08:00
Unlike other shells, fish has no dedicated `` VARIABLE=VALUE `` syntax for setting variables. Instead it has an ordinary command: `` set `` , which takes a variable name, and then its value.
2019-02-25 09:13:01 +08:00
::
2019-11-05 03:08:45 +08:00
> set name 'Mister Noodle'
> echo $name
2019-04-30 18:19:04 +08:00
Mister Noodle
2019-02-25 09:13:01 +08:00
(Notice the quotes: without them, `` Mister `` and `` Noodle `` would have been separate arguments, and `` $name `` would have been made into a list of two elements.)
Unlike other shells, variables are not further split after substitution::
2019-11-05 03:08:45 +08:00
> mkdir $name
> ls
2019-04-30 18:19:04 +08:00
Mister Noodle
2019-02-25 09:13:01 +08:00
2020-09-21 02:12:50 +08:00
In bash, this would have created two directories "Mister" and "Noodle". In fish, it created only one: the variable had the value "Mister Noodle", so that is the argument that was passed to `` mkdir `` , spaces and all. Other shells use the term "arrays", rather than lists.
2019-02-25 09:13:01 +08:00
2019-06-07 01:26:54 +08:00
You can erase (or "delete") a variable with `` -e `` or `` --erase ``
2019-02-25 09:13:01 +08:00
::
2019-11-05 03:08:45 +08:00
> set -e MyVariable
> env | grep MyVariable
2019-06-07 01:26:54 +08:00
(no output)
2019-02-25 09:13:01 +08:00
2019-06-07 01:26:54 +08:00
Exports (Shell Variables)
-------------------------
2019-02-25 09:13:01 +08:00
2020-10-27 02:19:05 +08:00
Sometimes you need to have a variable available to an external command, often as a setting. For example many programs like `` git `` or `` man `` read the `` $PAGER `` variable to figure out your preferred pager (the program that lets you scroll text). Other variables used like this include `` $BROWSER `` , `` $LANG `` (to configure your language) and `` $PATH `` . You'll note these are written in ALLCAPS, but that's just a convention.
2019-02-25 09:13:01 +08:00
2020-09-21 02:12:50 +08:00
To give a variable to an external command, it needs to be "exported". Unlike other shells, fish does not have an export command. Instead, a variable is exported via an option to `` set `` , either `` --export `` or just `` -x `` .
2019-02-25 09:13:01 +08:00
::
2019-11-05 03:08:45 +08:00
> set -x MyVariable SomeValue
> env | grep MyVariable
2019-06-07 01:26:54 +08:00
MyVariable=SomeValue
2019-02-25 09:13:01 +08:00
2019-06-07 01:26:54 +08:00
It can also be unexported with `` --unexport `` or `` -u `` .
2019-02-25 09:13:01 +08:00
2020-08-01 03:06:40 +08:00
.. _tut-lists:
2019-06-07 04:52:14 +08:00
2019-02-25 09:13:01 +08:00
Lists
-----
2020-09-21 02:12:50 +08:00
The `` set `` command above used quotes to ensure that `` Mister Noodle `` was one argument. If it had been two arguments, then `` name `` would have been a list of length 2. In fact, all variables in fish are really lists, that can contain any number of values, or none at all.
2019-02-25 09:13:01 +08:00
Some variables, like `` $PWD `` , only have one value. By convention, we talk about that variable's value, but we really mean its first (and only) value.
Other variables, like `` $PATH `` , really do have multiple values. During variable expansion, the variable expands to become multiple arguments::
2019-11-05 03:08:45 +08:00
> echo $PATH
2019-04-30 18:19:04 +08:00
/usr/bin /bin /usr/sbin /sbin /usr/local/bin
2019-02-25 09:13:01 +08:00
2019-04-30 19:18:56 +08:00
Variables whose name ends in "PATH" are automatically split on colons to become lists. They are joined using colons when exported to subcommands. This is for compatibility with other tools, which expect $PATH to use colons. You can also explicitly add this quirk to a variable with `set --path` , or remove it with `set --unpath` .
2019-02-25 09:13:01 +08:00
Lists cannot contain other lists: there is no recursion. A variable is a list of strings, full stop.
Get the length of a list with `` count `` ::
2019-11-05 03:08:45 +08:00
> count $PATH
2019-04-30 18:19:04 +08:00
5
2019-02-25 09:13:01 +08:00
You can append (or prepend) to a list by setting the list to itself, with some additional arguments. Here we append /usr/local/bin to $PATH::
2019-11-05 03:08:45 +08:00
> set PATH $PATH /usr/local/bin
2019-02-25 09:13:01 +08:00
You can access individual elements with square brackets. Indexing starts at 1 from the beginning, and -1 from the end::
2019-11-05 03:08:45 +08:00
> echo $PATH
2019-04-30 18:19:04 +08:00
/usr/bin /bin /usr/sbin /sbin /usr/local/bin
2019-11-05 03:08:45 +08:00
> echo $PATH[1]
2019-04-30 18:19:04 +08:00
/usr/bin
2019-11-05 03:08:45 +08:00
> echo $PATH[-1]
2019-04-30 18:19:04 +08:00
/usr/local/bin
2019-02-25 09:13:01 +08:00
2020-10-11 03:22:34 +08:00
You can also access ranges of elements, known as "slices":
2019-02-25 09:13:01 +08:00
::
2019-11-05 03:08:45 +08:00
> echo $PATH[1..2]
2019-04-30 18:19:04 +08:00
/usr/bin /bin
2019-11-05 03:08:45 +08:00
> echo $PATH[-1..2]
2019-04-30 18:19:04 +08:00
/usr/local/bin /sbin /usr/sbin /bin
2019-02-25 09:13:01 +08:00
You can iterate over a list (or a slice) with a for loop::
2019-11-05 03:08:45 +08:00
> for val in $PATH
2019-02-25 09:13:01 +08:00
echo "entry: $val"
end
2019-04-30 18:19:04 +08:00
entry: /usr/bin/
entry: /bin
entry: /usr/sbin
entry: /sbin
entry: /usr/local/bin
2019-02-25 09:13:01 +08:00
2019-04-09 20:06:54 +08:00
Lists adjacent to other lists or strings are expanded as :ref: `cartesian products <cartesian-product>` unless quoted (see :ref: `Variable expansion <expand-variable>` )::
2019-02-25 09:13:01 +08:00
2019-11-05 03:08:45 +08:00
> set a 1 2 3
> set 1 a b c
> echo $a$1
2019-04-30 18:19:04 +08:00
1a 2a 3a 1b 2b 3b 1c 2c 3c
2019-11-05 03:08:45 +08:00
> echo $a" banana"
2019-04-30 18:19:04 +08:00
1 banana 2 banana 3 banana
2019-11-05 03:08:45 +08:00
> echo "$a banana"
2019-04-30 18:19:04 +08:00
1 2 3 banana
2019-02-25 09:13:01 +08:00
2019-03-31 03:27:30 +08:00
This is similar to `Brace expansion <index#expand-brace> `__ .
2019-02-25 09:13:01 +08:00
Command Substitutions
---------------------
2020-09-21 02:12:50 +08:00
Command substitutions use the output of one command as an argument to another. Unlike other shells, fish does not use backticks `` for command substitutions. Instead, it uses parentheses::
2019-02-25 09:13:01 +08:00
2019-11-05 03:08:45 +08:00
> echo In (pwd), running (uname)
2019-04-30 18:19:04 +08:00
In /home/tutorial, running FreeBSD
2019-02-25 09:13:01 +08:00
A common idiom is to capture the output of a command in a variable::
2019-11-05 03:08:45 +08:00
> set os (uname)
> echo $os
2019-04-30 18:19:04 +08:00
Linux
2019-02-25 09:13:01 +08:00
Command substitutions are not expanded within quotes. Instead, you can temporarily close the quotes, add the command substitution, and reopen them, all in the same argument::
2019-11-05 03:08:45 +08:00
> touch "testing_"(date +%s)".txt"
> ls *.txt
2019-04-30 18:19:04 +08:00
testing_1360099791.txt
2019-02-25 09:13:01 +08:00
Unlike other shells, fish does not split command substitutions on any whitespace (like spaces or tabs), only newlines. This can be an issue with commands like `` pkg-config `` that print what is meant to be multiple arguments on a single line. To split it on spaces too, use `` string split `` .
::
2019-11-05 03:08:45 +08:00
> printf '%s\n' (pkg-config --libs gio-2.0)
2019-04-30 18:19:04 +08:00
-lgio-2.0 -lgobject-2.0 -lglib-2.0
2020-03-31 01:17:51 +08:00
> printf '%s\n' (pkg-config --libs gio-2.0 | string split -n " ")
2019-04-30 18:19:04 +08:00
-lgio-2.0
2019-02-25 09:13:01 +08:00
-lgobject-2.0
2019-04-30 18:19:04 +08:00
-lglib-2.0
2019-02-25 09:13:01 +08:00
2020-08-01 03:06:40 +08:00
.. _tut-semicolon:
2020-07-18 02:22:57 +08:00
2019-02-25 09:13:01 +08:00
Separating Commands (Semicolon)
-------------------------------
Like other shells, fish allows multiple commands either on separate lines or the same line.
To write them on the same line, use the semicolon (";"). That means the following two examples are equivalent::
echo fish; echo chips
# or
echo fish
echo chips
2019-06-07 01:17:09 +08:00
Exit Status
-----------
2020-06-30 02:26:53 +08:00
When a command exits, it returns a status code as a non-negative integer.
2019-06-07 01:38:19 +08:00
2020-09-21 02:12:50 +08:00
Unlike other shells, fish stores the exit status of the last command in `` $status `` instead of `` $? `` .
2019-06-07 01:17:09 +08:00
::
2019-11-05 03:08:45 +08:00
> false
> echo $status
2019-06-07 01:17:09 +08:00
1
2020-06-12 00:40:05 +08:00
This indicates how the command fared - 0 usually means success, while the others signify kinds of failure. For instance fish's `` set --query `` returns the number of variables it queried that weren't set - `` set --query PATH `` usually returns 0, `` set --query arglbargl boogagoogoo `` usually returns 2.
2019-06-07 01:38:19 +08:00
There is also a `` $pipestatus `` list variable for the exit statuses [#]_ of processes in a pipe.
2019-06-07 01:17:09 +08:00
2019-06-07 01:38:19 +08:00
.. [#] or "stati" if you prefer, or "statūs" if you've time-travelled from ancient Rome or work as a latin teacher
2019-06-07 01:17:09 +08:00
2020-08-01 02:53:26 +08:00
.. _tut-combiners:
2020-07-18 02:22:57 +08:00
2019-02-25 09:13:01 +08:00
Combiners (And, Or, Not)
------------------------
2019-04-07 05:33:07 +08:00
fish supports the familiar `` && `` and `` || `` to combine commands, and `` ! `` to negate them::
2019-02-25 09:13:01 +08:00
2019-11-05 03:08:45 +08:00
> ./configure && make && sudo make install
2019-02-25 09:13:01 +08:00
2020-07-18 02:22:57 +08:00
Here, `` make `` is only executed if `` ./configure `` succeeds (returns 0), and `` sudo make install `` is only executed if both `` ./configure `` and `` make `` succeed.
2019-02-25 09:13:01 +08:00
2020-07-18 02:22:57 +08:00
fish also supports :ref: `and <cmd-and>` , :ref: `or <cmd-or>` , and :ref: `not <cmd-not>` . The first two are job modifiers and have lower precedence. Example usage::
2019-02-25 09:13:01 +08:00
2019-11-05 03:08:45 +08:00
> cp file1.txt file1_bak.txt && cp file2.txt file2_bak.txt ; and echo "Backup successful"; or echo "Backup failed"
2019-04-30 18:19:04 +08:00
Backup failed
2019-02-25 09:13:01 +08:00
2020-08-01 03:06:40 +08:00
As mentioned in :ref: `the section on the semicolon <tut-semicolon>` , this can also be written in multiple lines, like so::
2019-02-25 09:13:01 +08:00
cp file1.txt file1_bak.txt && cp file2.txt file2_bak.txt
and echo "Backup successful"
or echo "Backup failed"
2020-07-24 23:17:39 +08:00
.. _tut-conditionals:
2019-02-25 09:13:01 +08:00
Conditionals (If, Else, Switch)
-------------------------------
2020-07-18 02:22:57 +08:00
Use :ref: `if <cmd-if>` and :ref: `else <cmd-else>` to conditionally execute code, based on the exit status of a command.
2019-02-25 09:13:01 +08:00
::
if grep fish /etc/shells
echo Found fish
else if grep bash /etc/shells
echo Found bash
else
echo Got nothing
end
2019-03-31 17:07:59 +08:00
To compare strings or numbers or check file properties (whether a file exists or is writeable and such), use :ref: `test <cmd-test>` , like
2019-02-25 09:13:01 +08:00
::
if test "$fish" = "flounder"
echo FLOUNDER
end
# or
if test "$number" -gt 5
echo $number is greater than five
else
echo $number is five or less
end
2019-06-07 02:00:11 +08:00
# or
if test -e /etc/hosts # is true if the path /etc/hosts exists - it could be a file or directory or symlink (or possibly something else).
echo We most likely have a hosts file
else
echo We do not have a hosts file
end
2019-02-25 09:13:01 +08:00
2020-08-01 02:53:26 +08:00
:ref: `Combiners <tut-combiners>` can also be used to make more complex conditions, like
2019-02-25 09:13:01 +08:00
::
if grep fish /etc/shells; and command -sq fish
echo fish is installed and configured
end
2020-07-18 02:22:57 +08:00
For even more complex conditions, use :ref: `begin <cmd-begin>` and :ref: `end <cmd-end>` to group parts of them.
2019-02-25 09:13:01 +08:00
2020-07-18 02:22:57 +08:00
There is also a :ref: `switch <cmd-switch>` command::
2019-02-25 09:13:01 +08:00
switch (uname)
case Linux
echo Hi Tux!
case Darwin
echo Hi Hexley!
case FreeBSD NetBSD DragonFly
echo Hi Beastie!
case '*'
echo Hi, stranger!
end
2020-07-18 02:22:57 +08:00
Note that :ref: `case <cmd-case>` does not fall through, and can accept multiple arguments or (quoted) wildcards.
2019-02-25 09:13:01 +08:00
Functions
---------
2020-07-18 02:22:57 +08:00
A fish function is a list of commands, which may optionally take arguments. Unlike other shells, arguments are not passed in "numbered variables" like `` $1 `` , but instead in a single list `` $argv `` . To create a function, use the :ref: `function <cmd-function>` builtin::
2019-02-25 09:13:01 +08:00
2019-11-05 03:08:45 +08:00
> function say_hello
2019-02-25 09:13:01 +08:00
echo Hello $argv
end
2019-11-05 03:08:45 +08:00
> say_hello
2019-04-30 18:19:04 +08:00
Hello
2019-11-05 03:08:45 +08:00
> say_hello everybody!
2019-04-30 18:19:04 +08:00
Hello everybody!
2019-02-25 09:13:01 +08:00
2020-07-18 02:22:57 +08:00
Unlike other shells, fish does not have aliases or special prompt syntax. Functions take their place.
2019-02-25 09:13:01 +08:00
2020-07-18 02:22:57 +08:00
You can list the names of all functions with the :ref: `functions <cmd-functions>` builtin (note the plural!). fish starts out with a number of functions::
2019-02-25 09:13:01 +08:00
2019-11-05 03:08:45 +08:00
> functions
2020-07-18 02:22:57 +08:00
N_, abbr, alias, bg, cd, cdh, contains_seq, delete-or-exit, dirh, dirs, disown, down-or-search, edit_command_buffer, export, fg, fish_add_path, fish_breakpoint_prompt, fish_clipboard_copy, fish_clipboard_paste, fish_config, fish_default_key_bindings, fish_default_mode_prompt, fish_git_prompt, fish_hg_prompt, fish_hybrid_key_bindings, fish_indent, fish_is_root_user, fish_job_summary, fish_key_reader, fish_md5, fish_mode_prompt, fish_npm_helper, fish_opt, fish_print_git_action, fish_print_hg_root, fish_prompt, fish_sigtrap_handler, fish_svn_prompt, fish_title, fish_update_completions, fish_vcs_prompt, fish_vi_cursor, fish_vi_key_bindings, funced, funcsave, grep, help, history, hostname, isatty, kill, la, ll, ls, man, nextd, nextd-or-forward-word, open, popd, prevd, prevd-or-backward-word, prompt_hostname, prompt_pwd, psub, pushd, realpath, seq, setenv, suspend, trap, type, umask, up-or-search, vared, wait
2019-02-25 09:13:01 +08:00
You can see the source for any function by passing its name to `` functions `` ::
2019-11-05 03:08:45 +08:00
> functions ls
2019-02-25 09:13:01 +08:00
function ls --description 'List contents of directory'
command ls -G $argv
end
Loops
-----
While loops::
2019-11-05 03:08:45 +08:00
> while true
2019-04-30 18:53:15 +08:00
echo "Loop forever"
2019-02-25 09:13:01 +08:00
end
2019-04-30 18:19:04 +08:00
Loop forever
Loop forever
Loop forever
2019-06-07 02:00:11 +08:00
... # yes, this really will loop forever. Unless you abort it with ctrl-c.
2019-02-25 09:13:01 +08:00
For loops can be used to iterate over a list. For example, a list of files::
2019-11-05 03:08:45 +08:00
> for file in *.txt
2019-02-25 09:13:01 +08:00
cp $file $file.bak
end
Iterating over a list of numbers can be done with `` seq `` ::
2019-11-05 03:08:45 +08:00
> for x in (seq 5)
2019-02-25 09:13:01 +08:00
touch file_$x.txt
end
Prompt
------
2020-08-03 00:01:34 +08:00
.. role :: purple
2019-02-25 09:13:01 +08:00
2020-08-03 00:01:34 +08:00
Unlike other shells, there is no prompt variable like `` PS1 `` . To display your prompt, fish executes the :ref: `fish_prompt <cmd-fish_prompt>` function and uses its output as the prompt. And if it exists, fish also executes the :ref: `fish_right_prompt <cmd-fish_right_prompt>` function and uses its output as the right prompt.
2019-02-25 09:13:01 +08:00
2020-08-03 00:01:34 +08:00
You can define your own prompt from the command line:
2019-02-25 09:13:01 +08:00
2020-08-03 00:01:34 +08:00
.. parsed-literal ::
2020-09-02 11:48:59 +08:00
:class: highlight
2019-02-25 09:13:01 +08:00
2020-08-03 00:01:34 +08:00
> function fish_prompt; echo "New Prompt % "; end
New Prompt % _
2019-05-25 17:47:44 +08:00
2019-02-25 09:13:01 +08:00
2020-08-03 00:01:34 +08:00
Then, if you are happy with it, you can save it to disk by typing `` funcsave fish_prompt `` . This saves the prompt in `` ~/.config/fish/functions/fish_prompt.fish `` . (Or, if you want, you can create that file manually from the start.)
Multiple lines are OK. Colors can be set via :ref: `set_color <cmd-set_color>` , passing it named ANSI colors, or hex RGB values::
function fish_prompt
set_color purple
date "+%m/%d/%y"
set_color F00
echo (pwd) '>' (set_color normal)
end
This prompt would look like:
.. parsed-literal ::
2020-09-02 11:48:59 +08:00
:class: highlight
2019-02-25 09:13:01 +08:00
2020-09-02 11:48:59 +08:00
:purple: `02/06/13`
:red: `/home/tutorial >` _
2019-05-25 17:47:44 +08:00
2019-02-25 09:13:01 +08:00
2020-08-03 00:01:34 +08:00
You can choose among some sample prompts by running `` fish_config prompt `` .
2019-02-25 09:13:01 +08:00
$PATH
-----
2020-09-21 02:12:50 +08:00
`` $PATH `` is an environment variable containing the directories that fish searches for commands. Unlike other shells, $PATH is a :ref: `list <tut-lists>` , not a colon-delimited string.
2019-02-25 09:13:01 +08:00
To prepend /usr/local/bin and /usr/sbin to `` $PATH `` , you can write::
2019-11-05 03:08:45 +08:00
> set PATH /usr/local/bin /usr/sbin $PATH
2019-02-25 09:13:01 +08:00
To remove /usr/local/bin from `` $PATH `` , you can write::
2019-11-05 03:08:45 +08:00
> set PATH (string match -v /usr/local/bin $PATH)
2019-02-25 09:13:01 +08:00
2020-10-11 03:22:34 +08:00
For compatibility with other shells and external commands, $PATH is a :ref: `path variable<variables-path>` , and so will be joined with colons (not spaces) when you quote it::
2019-06-07 02:00:11 +08:00
2019-11-05 03:08:45 +08:00
> echo "$PATH"
2019-06-07 02:00:11 +08:00
/usr/local/sbin:/usr/local/bin:/usr/bin
and it will be exported like that, and when fish starts it splits the $PATH it receives into a list on colon.
2019-02-25 09:13:01 +08:00
2019-03-31 17:50:28 +08:00
You can do so directly in `` config.fish `` , like you might do in other shells with `` .profile `` . See :ref: `this example <path_example>` .
2019-02-25 09:13:01 +08:00
2020-05-21 23:38:46 +08:00
A faster way is to use the :ref: `fish_add_path <cmd-fish_add_path>` function, which adds given directories to the path if they aren't already included. It does this by modifying the `` $fish_user_paths `` :ref: `universal variable <tut-universal>` , which is automatically prepended to `` $PATH `` . For example, to permanently add `` /usr/local/bin `` to your `` $PATH `` , you could write::
2019-02-25 09:13:01 +08:00
2020-05-21 23:38:46 +08:00
> fish_add_path /usr/local/bin
2019-02-25 09:13:01 +08:00
2020-05-21 23:38:46 +08:00
The advantage is that you don't have to go mucking around in files: just run this once at the command line, and it will affect the current session and all future instances too. You can also add this line to :ref: `config.fish <tut-config>` , as it only adds the component if necessary.
Or you can modify $fish_user_paths yourself, but you should be careful *not* to append to it unconditionally in config.fish, or it will grow longer and longer.
.. _tut-config:
2019-02-25 09:13:01 +08:00
Startup (Where's .bashrc?)
--------------------------
2020-09-21 02:12:50 +08:00
Fish starts by executing commands in `` ~/.config/fish/config.fish `` . You can create it if it does not exist.
2019-02-25 09:13:01 +08:00
It is possible to directly create functions and variables in `` config.fish `` file, using the commands shown above. For example:
2019-03-31 17:50:28 +08:00
.. _path_example:
2019-02-25 09:13:01 +08:00
::
2019-11-05 03:08:45 +08:00
> cat ~/.config/fish/config.fish
2019-02-25 09:13:01 +08:00
set -x PATH $PATH /sbin/
function ll
ls -lh $argv
end
However, it is more common and efficient to use autoloading functions and universal variables.
2019-06-07 02:00:11 +08:00
If you want to organize your configuration, fish also reads commands in .fish files in `` ~/.config/fish/conf.d/ `` . See :ref: `initialization <initialization>` for the details.
2019-02-25 09:13:01 +08:00
Autoloading Functions
---------------------
2020-09-21 02:12:50 +08:00
When fish encounters a command, it attempts to autoload a function for that command, by looking for a file with the name of that command in `` ~/.config/fish/functions/ `` .
2019-02-25 09:13:01 +08:00
For example, if you wanted to have a function `` ll `` , you would add a text file `` ll.fish `` to `` ~/.config/fish/functions `` ::
2019-11-05 03:08:45 +08:00
> cat ~/.config/fish/functions/ll.fish
2019-02-25 09:13:01 +08:00
function ll
ls -lh $argv
end
This is the preferred way to define your prompt as well::
2019-11-05 03:08:45 +08:00
> cat ~/.config/fish/functions/fish_prompt.fish
2019-02-25 09:13:01 +08:00
function fish_prompt
echo (pwd) "> "
end
2019-10-03 21:03:49 +08:00
See the documentation for :ref: `funced <cmd-funced>` and :ref: `funcsave <cmd-funcsave>` for ways to create these files automatically, and :ref: `$fish_function_path <syntax-function-autoloading>` to control their location.
2019-02-25 09:13:01 +08:00
2019-09-30 22:02:06 +08:00
.. _tut-universal:
2019-02-25 09:13:01 +08:00
Universal Variables
-------------------
2020-09-21 02:12:50 +08:00
A universal variable is a variable whose value is shared across all instances of fish, now and in the future – even after a reboot. You can make a variable universal with `` set -U `` ::
2019-02-25 09:13:01 +08:00
2019-11-05 03:08:45 +08:00
> set -U EDITOR vim
2019-02-25 09:13:01 +08:00
Now in another shell::
2019-11-05 03:08:45 +08:00
> echo $EDITOR
2019-02-25 09:13:01 +08:00
vim
.. _switching-to-fish:
Switching to fish?
------------------
If you wish to use fish (or any other shell) as your default shell,
2020-07-15 13:58:57 +08:00
you need to enter your new shell's executable in two places.
2019-02-25 09:13:01 +08:00
2020-07-15 13:58:57 +08:00
Add the shell to `` /etc/shells `` with::
2019-02-25 09:13:01 +08:00
2019-11-05 03:08:45 +08:00
> echo /usr/local/bin/fish | sudo tee -a /etc/shells
2019-02-25 09:13:01 +08:00
2020-07-15 13:58:57 +08:00
Change your default shell with::
2019-02-25 09:13:01 +08:00
2019-11-05 03:08:45 +08:00
> chsh -s /usr/local/bin/fish
2019-02-25 09:13:01 +08:00
(To change it back to another shell, just substitute `` /usr/local/bin/fish ``
with `` /bin/bash `` , `` /bin/tcsh `` or `` /bin/zsh `` as appropriate in the steps above.)
Ready for more?
---------------
2019-11-16 23:03:14 +08:00
If you want to learn more about fish, there is :ref: `lots of detailed documentation <intro>` , the `official gitter channel <https://gitter.im/fish-shell/fish-shell> `__ , an `official mailing list <https://lists.sourceforge.net/lists/listinfo/fish-users> `__ , and the `github page <https://github.com/fish-shell/fish-shell/> `__ .