lpaste.de

The Fish shell.

Intro

On most of the Linux distros I have used over the years, the default shell almost always was the Bash (I mostly experimented with Debian-based Linux distros). While I was not unsatisfied with the Bash for several years, I started looking for other shells when a colleague from university showed me his Z-shell setup and some of the cool features it had. It was very impressive to see how fast you can be with a Z shell. So at a spare evening, I simply installed the Z shell on my Ubuntu Laptop but was quickly overwhelmed by all the configurations you have to make to use it. A quick search on the internet brought me to Oh-My-Zsh, a project aiming to help with Z-shell configuration and providing a lot of plugins, themes, etc. for it. I tried it for a few weeks and was happy with all the features it provided but also very annoyed that I constantly found myself tweaking and searching for new plugins to improve my experience. As an effect, I didn't reinstall the Z shell when I upgraded my Ubuntu Laptop. After a few weeks of using the Bash again, I was missing some features of the Z shell but also didn't want to reconfigure it which finally brought me to the Fish shell.

My favorite features of the Fish shell

Most of it works by default

One of the best features of the Fish shell is that most of its features work out of the box without configuring them. It saves a lot of time. Yes, there are still things I tweaked like the color scheme, prompt and starting message but it is easily done by using the built-in web interface.

Autosuggestion

The second most useful feature for me is autosuggestion. Just type a command and it automatically suggests you a command based on what you typed and your command-line history. It works similarly well as the autosuggestion of modern web browsers like Firefox or Google Chrome, except that modern browsers additionally keep internal statistics to suggest you the most likely URL based on your previous behavior. If the suggested command is not what you want, keep typing or use the up/down arrow keys to search your history. To select the suggested command press Ctrl+F (or the right arrow key) and hit Enter. If you just hit Enter, only what you typed is executed.

Adding paths to "PATH" or something similar

Another thing that I like about the Fish shell is how straightforward it is to create or add things to environment variables. Instead of editing files, simply execute the following command to prepend something to the $fish_user_paths variable:

set -U fish_user_paths ~/your/path/ $fish_user_paths

The Fish shell automatically prepends $fish_user_paths to $PATH but the main difference here is not that you add the path of an executable to $fish_user_paths than $PATH. Instead, it is how to set environment variables, in general, using the Fish shell. Other than doing this by adding a line to the .bashrc or .profile which is how it is done when using the Bash, the Fish shell provides a set command that allows to easily set or append things such as paths to variables. The -U in the command is used to set a universal environment variable that is persistent. If shell variables like $fish_user_paths contain several paths, the Fish shell threats them as a list. For example, the following command returns the first path of the $fish_user_paths variable:

echo $fish_user_paths[1]

Notice that list indices start with 1. (I honestly don't know why they did this.) Lists make it very easy to delete single elements from a variable. As an example to delete the first path from the $fish_user_paths variable, simply execute the following command:

set -e fish_user_paths[1]

The -e option tells the set command to erase the shell variable but because we specified only a single element of $fish_user_paths, it deletes a single element instead of the whole variable.

Functions

Fish shell functions are similar to bash's aliases or small scripts. They provide a neat way to bundle one or several commands under a new name and optionally take arguments. The following code snippet is an example of the fish greeting function, which is the function that defines what is printed out when a new Fish shell is started:

function fish_greeting
    echo 'Hello World!'
end

This function prints Hello World! when a new Fish shell is started. Functions can be defined using an editor or by typing them into the Fish shell. The later way is quite cool for smaller functions because the Fish shell automatically takes care of indentation and provides syntax highlighting. After typing the function and pressing enter, the function can be called in the current Fish shell by its name. To save a function, execute the funcsave function followed by the function name.

funcsave fish_greeting

This saves the fish_greeting function so that it is still there after closing the Fish shell and starting a new one.

Outro

If you want to try the Fish shell or learn more about it, go to the Fish shell homepage. However, I hope you enjoyed reading the blog post.

← Home