Playing with a new shell: fish
- Author: Solène
- Date: 05 September 2021
- Tags: openbsd shell
Introduction
Today I'll introduce you to the interactive shell fish. Usually, Linux distributions ships bash (which can be a hidden dash, a limited shell), MacOS is providing zsh and OpenBSD ksh. There are other shells around and fish is one of them.
But fish is not like the others.
What make it special?
Here is a list of biggest changes:
- suggested input based on commands available
- suggested input based on history (even related to the current directory you are in!)
- not POSIX compatible (the usual shell syntax won't work)
- command completion works out of the box (no need for extensions like "ohmyzsh")
- interconnected processes: updating a variable can be done into every opened shells
Making history more powerful with fzf
fzf is a simple utility for searching data among a file (the history file in that case) in fuzzy mode, meaning in not a strict matching, on OpenBSD I use the following configuration file in ~/.config/fish/config.fish to make fzf active.
When pressing ctrl+r with some history available, you can type any words you can think about an old command like "ssh bar" and it should return "ssh foobar" if it exists.
source /usr/local/share/fish/functions/fzf-key-bindings.fish fzf_key_bindings
fzf is absolutely not related to fish, it can certainly be used in some other shells.
Tips
Disable caret character for redirecting to stderr
The defaults works pretty well but as I said before, fish is not POSIX compatible, meaning some habits must be changed. By default, ^ character like in "grep ^foobar" is the equivalent of 2> which is very misleading.
# make typing ^ actually inserting a "^" and not stderr redirect set -U fish_features stderr-nocaret qmark-noglob
Web GUI for customizing your shell
If you want to change behaviors or colors of your shell, just type "fish_config" while in a shell fish, it will run a local web server and open your web browser.
Validating a suggestion
When you type a command and you see more text suggested as you type the command you can press ctrl+e to validate the suggestion. If you don't care about the suggestion, continue typing your command.
Get the return value of latest command
In fish, you want to read $status and not $? , that variable doesn't exist in fish.
Syntax changes
Because it's not always easy to find what changed and how, here is a simple reminder that should cover most of your needs:
- loops (no do keyword, ends with end): for i in 1 2 3 ; echo $i ; end
- condition (no then, ends with end): if something ; echo true ; end
- inline command (no dollar sign): (date +%s)
- export a variable: set -x EDITOR kak
- return value of last command: $status
Conclusion
I love this shell. I've been using the shell that come with my system since forever, and a few months ago I wanted to try something different, it felt weird at first but over time I found it very convenient, especially for git commands or daily tasks, suggesting me exactly the command I wanted to type in that exact directory.
Obviously, as the usual syntax changes, it may not please everyone and it's totally fine.