Fish shell
A command line shell that does not think that the problem is you
March 30, 2019 — November 22, 2024
Not the aquatic creatures, but rather the command-line doohickey, which is not as shit as the other ones. I’m gradually transitioning to fish
, after accidentally losing a lot of precious data due to a quirk in bash
syntax. Long boring story. It’s time for new, exciting, different stupid errors.
fish
has a strong fanbase and an opinionated design. If you dislike those design opinions, at least you might appreciate it has a healthy degree of sarcasm with which said opinions are expressed, which sarcasm is sorely absent from the drearily earnest nerdview of your typical gnu.org project. You might also hold that having any kind of principled opinion is better than the design-by-accumulation-of-tradition-cruft which structures command-line shells.
1 Installing
- Ubuntu users can get an updated fish PPA.
- or, macOS/Linux: install using homebrew
gararine reports how to make fish
the default shell with homebrew:
# check the fish path with `which fish`. In the examples below it was located at: `/opt/homebrew/bin/fish`.
# Add fish to the known shells
sudo sh -c `which fish` >> /etc/shells
# Set fish as the default shell
chsh -s `which fish`
# Add brew binaries in fish path
fish_add_path /opt/homebrew/bin
#To collect command completions for all commands run:
fish_update_completions
2 Configuration
2.1 Modifying the PATH
This is the most confusing thing in fish for me. It is worth reading tutorials, e.g.
tl;dr:
To add a path, use the utility command fish_add_path
To remove a path
There is an alternative method where we just update a path variable like any other variable, which relates in a confusing way: Adding a path? Say it’s /usr/local/bin
. Put
in ~/.config/fish/config.fish
, OR (“universal” style)
Removing a path?
🚧TODO🚧 clarify the difference between $PATH
and $fish_user_path
which will depend upon me understanding how the content of $PATH
magically replenishes itself and the difference between “universal” and ”global” variables.
2.2 Modifying any settings with GUI
2.3 Traditional config
Put commands in ~/.config/fish/config.fish
.
2.4 Extremely traditional config
Aelius notes a hack to unify config:
one of the things I like about fish is how there are sane defaults and I don’t need to have any config. Which works for me, because I have no interest in learning fish syntax. I just want a helpful shell, I don’t want to have to know yet another language, and I deeply resent fish every time it doesn’t process the line of posix sh I paste into it from a wiki…
After jumping between several different shells and rewriting my
.profile
a number of different times for a number of different shells, I came up with a way to decouple my environment config from the shell I use. My environment always works, I don’t have to learn fish or any other syntax.I set
/bin/dash
as my login shell. the first line of my~/.profile
isENV=$HOME/.shinit; export ENV.
In any interactive shell, dash executes~/.shinit
, which contains one line:exec /usr/bin/fish
.Every config item I need from my shell goes into
~/.profile
, written in easy, conventional posix sh— and I still get to use fish as my interactive shell, without having to go through the trouble of adopting its config to my system.
2.5 ssh-agent
Optiligence notes that this minor alteration should work.
Alternatively, see danhper/fish-ssh-agent or ivakyb/fish_ssh_agent.
Installation:
wget https://gitlab.com/kyb/fish_ssh_agent/raw/master/functions/fish_ssh_agent.fish -P ~/.config/fish/functions/
Append next line to ~/.config/fish/config.fish
You really need to verify that https://gitlab.com/kyb/fish_ssh_agent/raw/master/functions/fish_ssh_agent.fish
is not anything malicious; this is high security code.
To be more secure, we can get a known-good (IMO) version thusly:
3 Plugins
You can hack fish. Popular plugin management systems exist also. AFAICT a passable default is oh my fish
fisher seems to be around too?
It also handles omf
plugins, apparently.
The omf
manual is brusque. See a helpful blogpost. I am currently running omf, but since it intrusively changed my prompt I am grumpy at it. However, I got a better prompt, spacefish.
It does need the wacky powerline fonts.
There are various useful plugins that are not purely cosmetic; For example fzf adds fuzzy history search. z does recency/frequency-based directory navigation.
4 homebrew compatibility on Ubuntu
Since I use fish
shell as my default but ubuntu automatically executes the bash
startup script .profile
on login, I ran into the following errors when it tried to run the fish
init in a bash
process when I used homebrew:
bash: set: -g: invalid option
set: usage: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
bash: set: -g: invalid option
...
This is maybe related to an intermittently reported bug in homebrew. The fix that worked for me was to change the automatically-added line in .profile
to be
and to add
to ~/.config/fish/config.fish
.
5 Python environments
5.1 virtualenv
If I used virtualenv
on python I would need virtualfish to replace python’s virtualenvwrapper.sh
. Or switch to native python3 venv
, which is more or less the same thing but works better and doesn’t support python 2. But if you need to support python 2 at this stage it’s because you are in some weird enterprise environment with horrid legacy software, so hopefully you can farm this problem out to the tech support team? Either that or you are barred from using fish
by policy and this is not a problem.
5.2 Using anaconda python
You need to do some extra setup to use conda with fish.
Or, for older versions,
into ~/.config/fish/config.fish
.
(Replace ~/miniconda3/
with the output of conda info --root
if you used a non-standard install location)
6 Vars, expansions, extensions, suffixes
Wildcards are minimal, just *
, **
, and brace expansion, mv a.{txt,html
.
For more sophisticated string processing, one defines custom functions (which I never actually do) or use classic subcommands which I do all the time. Usually I want to rename files:
or expansion via string
, which is harder to remember
Or use another fancy utility like rename.
7 For loops
8 Test exit status
To simply execute the second command if the first succeeded the command you want is and, which is hard to google for:
9 Temporary variable setting uses env
10 VS code
VS code requires you launch it from the command line if you use fish on macos.
11 Aliases, custom commands
12 Writing functions
13 Python environments
In lazy mode, python environments works by manipulating paths and environment variables. This needs extra configuration in fish
shell. pyenv
needs
set -x PYENV_ROOT $HOME/.pyenv
set -x PATH $PYENV_ROOT/bin $PATH
status --is-interactive; and pyenv init --path | source
status --is-interactive; and pyenv init - | source
See How to use nvm, rbenv, pyenv, goenv… with the fish shell.
14 Incoming
Deleting history interactively:
NB that only allocates 5 jobs at a time. Nifty.