``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.
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!
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::
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.
``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.
You'll quickly notice that ``fish`` performs syntax highlighting as you type. Invalid commands are colored red by default::
>_ <eror>/bin/mkd</eror>
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::
>_ /bin/mkdir
``fish`` will underline valid file paths as you type them::
>_ cat <u>~/somefi</u>___
This tells you that there exists a file that starts with '``somefi``', which is useful feedback as you type.
These colors, and many more, can be changed by running ``fish_config``, or by modifying variables directly.
Wildcards
---------
``fish`` supports the familiar wildcard ``*``. To list all JPEG files::
To accept the autosuggestion, hit :kbd:`→` (right arrow) or :kbd:`Control+F`. To accept a single word of the autosuggestion, :kbd:`Alt+→` (right arrow). If the autosuggestion is not what you want, just ignore it.
Unlike other shells, ``fish`` has no dedicated syntax for setting variables. Instead it has an ordinary command: ``set``, which takes a variable name, and then its value.
(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::
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.
Exit Status
-----------
Unlike other shells, ``fish`` stores the exit status of the last command in ``$status`` instead of ``$?``.
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``.
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.
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::
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`.
Lists adjacent to other lists or strings are expanded as :ref:`cartesian products <cartesian-product>` unless quoted (see :ref:`Variable expansion <expand-variable>`)::
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::
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::
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``.
For even more complex conditions, use ``begin`` and ``end`` to group parts of them.
There is also a ``switch`` command::
switch (uname)
case Linux
echo Hi Tux!
case Darwin
echo Hi Hexley!
case FreeBSD NetBSD DragonFly
echo Hi Beastie!
case '*'
echo Hi, stranger!
end
Note that ``case`` does not fall through, and can accept multiple arguments or (quoted) wildcards.
Functions
---------
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 ``function`` builtin::
For loops can be used to iterate over a list. For example, a list of files::
>_ for file in *.txt
cp $file $file.bak
end
Iterating over a list of numbers can be done with ``seq``::
>_ for x in (seq 5)
touch file_$x.txt
end
Prompt
------
Unlike other shells, there is no prompt variable like PS1. To display your prompt, ``fish`` executes a function with the name ``fish_prompt``, and its output is used as the prompt.
You can choose among some sample prompts by running ``fish_config prompt``. ``fish`` also supports RPROMPT through ``fish_right_prompt``.
$PATH
-----
``$PATH`` is an environment variable containing the directories in which ``fish`` searches for commands. Unlike other shells, $PATH is a [list](#tut_lists), not a colon-delimited string.
To prepend /usr/local/bin and /usr/sbin to ``$PATH``, you can write::
>_ set PATH /usr/local/bin /usr/sbin $PATH
To remove /usr/local/bin from ``$PATH``, you can write::
>_ set PATH (string match -v /usr/local/bin $PATH)
A faster way is to modify the ``$fish_user_paths`` [universal variable](#tut_universal), which is automatically prepended to ``$PATH``. For example, to permanently add ``/usr/local/bin`` to your ``$PATH``, you could write::
>_ set -U fish_user_paths /usr/local/bin $fish_user_paths
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. (Note: you should NOT add this line to ``config.fish``. If you do, the variable will get longer each time you run fish!)
Startup (Where's .bashrc?)
--------------------------
``fish`` starts by executing commands in ``~/.config/fish/config.fish``. You can create it if it does not exist.
It is possible to directly create functions and variables in ``config.fish`` file, using the commands shown above. For example:
However, it is more common and efficient to use autoloading functions and universal variables.
Autoloading Functions
---------------------
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/``.
For example, if you wanted to have a function ``ll``, you would add a text file ``ll.fish`` to ``~/.config/fish/functions``::
>_ cat ~/.config/fish/functions/ll.fish
function ll
ls -lh $argv
end
This is the preferred way to define your prompt as well::
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``::
>_ set -U EDITOR vim
Now in another shell::
>_ echo $EDITOR
vim
.._switching-to-fish:
Switching to fish?
------------------
If you wish to use fish (or any other shell) as your default shell,
you need to enter your new shell's executable ``/usr/local/bin/fish`` in two places:
- add ``/usr/local/bin/fish`` to ``/etc/shells``
- change your default shell with ``chsh -s /usr/local/bin/fish``
You can use the following commands for this:
Add the fish shell ``/usr/local/bin/fish``
to ``/etc/shells`` with::
>echo /usr/local/bin/fish | sudo tee -a /etc/shells
Change your default shell to fish with::
>chsh -s /usr/local/bin/fish
(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.)
If you want to learn more about fish, there is :ref:`lots of detailed documentation <intro>`, an `official mailing list <https://lists.sourceforge.net/lists/listinfo/fish-users>`__, the IRC channel \#fish on ``irc.oftc.net``, and the `github page <https://github.com/fish-shell/fish-shell/>`__.