fish

From Gentoo Wiki
Jump to:navigation Jump to:search
This page contains changes which are not marked for translation.

fish - the friendly interactive shell - is a smart and user-friendly command line shell for OS X, Linux, and the rest of the family. fish includes features like syntax highlighting, autosuggest-as-you-type, and fancy tab completions that just work, with no configuration required.

See the terminal emulator article for some general usage pointers.

Important
See caveats section for how to use fish as a user's default shell.
Warning
fish is not suitable for linking to /bin/sh, See the eselect sh utility for managing POSIX shells


Installation

Emerge

Install app-shells/fish:

root #emerge --ask app-shells/fish

Configuration

The command fish_config can manage settings on in the shell or a web browser. To use a web browser you also need a $BROWSER env var, an example of setting this would be set -gx BROWSER /usr/bin/librewolf run in the shell, added to the user or system as below, or in the usual bash format in /etc/env.d if you follow the steps in 'Fish as a default shell'

The main files for configuration are /etc/fish/config.fish, which is called by every shell, and the user init file ~/.config/fish/config.fish which does not exist by default. If the user file exists the system file is still used automatically.

Both config/fish and /etc/fish can contain a conf.d directory, this is handled slightly differently in that named files in the user's conf.d will prevent a file of the same name being sourced from /etc/fish/conf.d.

Completions

fish has extensive completions support. Included in the source are many detailed contributed completions, and basic coverage for other things is provided by generation from man pages. For details on file organisation and precedence have a look at https://fishshell.com/docs/current/completions.html#where-to-put-completions

Environment variables

The handbook explains how to set environment variables globally in the Gentoo system, for all users and the default POSIX shell. Anything using PAM will get the environment with pam_env but it's likely that many shells will start without that so extra configuration is needed.

Fish uses the same set command for all types of variables, with -gx (Global scope, eXported) being used for variables loaded from the environment, and has useful options like --prepend and --append.

FILE ~/.conf/fish/config.fishAppend a PATH variable in fish shell
set -gxa PATH "$HOME/.local/bin:"
FILE ~/.conf/fish/config.fishPrepend a PATH variable in fish shell
set -gxp PATH "$HOME/.local/bin:"

Fish as a default shell

Note
See ongoing discussion about setting Fish as default shell with chsh.

In Gentoo a POSIX compatible login shell is needed to configure the environment (see login shell in Gentoo for details), you must only use chsh to change the shell for a user or do it at creation. Linking /bin/sh to Fish will break your system, see eslect sh for a list of POSIX shells that are suitable for this role.

Starting Fish directly

To get partial environment support you can directly source the /etc/profile.env file, which is capable of handling this type of file now.

FILE /etc/fish/config.fish
[...]
status -l ; and test -f /etc/profile.env ; and source /etc/profile.env

This does not cover the scripts in /etc/profile.d, so if you need anything from there you will have to write your own version in Fish and then something like this to the system config so that root also gets the variables.

FILE /etc/fish/config.fish
[...]
status -l ; and for x in (find '/etc/profile.d/' -type f -name '*.fish')
  source $x
end

Starting Fish from Bash

The following workaround allows the use of fish on login or on starting a terminal emulator. This solution uses ~/.bashrc as a wrapper to have fish inherit the environment from the login shell, which is left as a bash.

Add the following to the user's ~/.bashrc, making sure it's placed below the test for an interactive shell, e.g. at the end of the file:

FILE ~/.bashrc
[...]
# Use fish in place of bash
# keep this line at the bottom of ~/.bashrc
[ -x /bin/fish ] && SHELL=/bin/fish exec fish

When bash is started as an interactive shell, this will automatically launch fish for the user, once bash has fully initialized the correct system environment. It will also set the SHELL environment variable to /bin/fish in fish.

Log into a new virtual console to test. Keeping open the current terminal may permit troubleshooting in case of issue with the new configuration.

Note
When set up this way, launching an interactive bash prompt will drop to fish because of the line added to ~/.bashrc. To launch bash, ignore ~/.bashrc (beware that any commands in ~/.bashrc will not be executed):
user $bash --rcfile /etc/profile

This solution was suggested by one of the fish developers, the Arch wiki and Gentoo devs [1].

fish as shell with bass to create and import environment

Once fish is installed, install bass, which will permit fish to source /etc/profile on startup. bass is a utility to execute commands in bash and replay the environment variable changes in fish. The bass site has instructions for different installation methods, such as with the fisher plugin manager or OMF. We will explain here how to install bass manually, as this is the most basic method, however a plugin manager is arguably preferable.

Move into or create a base directory for the bass repository, for example:

user $cd ~/.local/opt

Clone the bass repository (requires git), and move into the repository directory:

user ~/.local/opt $git clone https://github.com/edc/bass; cd bass

Install bass:

user ~/.local/opt/bass $make install

With bass, one can now have fish inherit the system-wide environment variables on startup, from bash.

Add the following line to ~/.config/fish/config.fish, above any other commands that would need the environment to be set up (config.fish may need to be created):

FILE ~/.config/fish/config.fish
bass source /etc/profile

Finally, restart fish if it is already running.

This solution was suggested by one of the fish developers and the Arch wiki.

See also

  • Shell — command-line interpreter that offers a text-based interface to users.

External resources