A smart and friendly shell

Teach a Man to Fish…

Article from Issue 162/2014
Author(s):

The fish shell provides many features that rival the well-known Bash. We examine some highlights.

Bruce Byfield

Bruce Byfield is a computer journalist and a freelance writer and editor specializing in free and open source software. In addition to his writing projects, he also teaches live and e-learning courses. In his spare time, Bruce writes about Northwest coast art. You can read more of his work at http://brucebyfield.wordpress.com

The home page for the Friendly Interactive SHell (fish) [1] still proclaims it "the command line shell for the 90s." However, with version 2.1.0 released in October 2013, fish remains in active development and includes dozens of small innovations that together make the command line a friendlier place.

Fish is available in the package repositories of many major distributions [2]. If yours does not include it, you can download the code by cloning the GitHub repository with

git clone git://github.com/fish-shell/fish-shell.git

and build it by following the instructions at the bottom of the GitHub page.

Once fish is installed, you can start it by entering the command fish at a command prompt. If you plan to use fish regularly, consider creating a profile in your virtual terminal that defaults to fish rather than Bash. Another option is to set fish as your default shell with the command ch -s fish.

Fish uses most of the same commands as Bash [3] and, in some cases, the same syntax as well. However, fish also includes significant differences, so first-timers are well-advised to begin with the online tutorial [4] and the documentation [5], which is opened in your web browser when you run the help command. All of fish's documentation is refreshingly clear, concise, and complete (Figure 1).

Figure 1: Fish's error messages not only explain the error but also suggest where you can learn more.

Basic Features – With Extras

The first feature you are likely to notice is syntax highlighting. This feature is far more extensive than the highlighting with the command ls --color, which displays different file types in different colors. Fish's highlighting usually begins with red, which identifies an invalid command. When your typing results in a valid command, the color changes to blue, with paths to source or target directories in light blue and underlined if they are valid.

Another feature that you will notice almost immediately is auto-suggestion. As you type, suggestions for completion appear in gray. These completions can be commands, directory and file paths, programs, and even man pages, options, and variables. You can either ignore the suggestions and continue typing or press Ctrl+F or the Right Arrow key to accept the suggestion.

These auto-suggestions are in addition to the extensive tab completions. As in many shells, you can press the Tab key to have fish list possible completions, and continue pressing Tab until you reach the completion you want. However, fish's tab completions also include brief descriptions of each item on the list, and they include not only commands but any part of the syntax. In other words, you can use tab completion at any point in your typing, not just for the command (Figure 2).

Figure 2: Tab completion is available for every part of the syntax, including options and directory paths.

As you continue to use fish, you will discover other advanced features as well. For example, history works just as it does in most shells, by pressing the Up and Down arrows, whereas the unsupplemented command history lists the commands you have entered.

As with other basic shell features, fish adds one or two extras. By default, fish deletes duplicate entries. Additionally, you can search the history and delete items either with the web-based configuration tool or with the history command, using history -search STRING to list commands that include the string and history -delete STRING to remove with confirmations all commands that include the string (Figure 3).

Figure 3: You can edit the command history with either the history command or the on-screen configuration tool.

Configuring Fish

Fish uses the Nano text editor for editing configuration files. This choice of editor has the advantage of building on-screen help into the interface and generally requiring less knowledge to begin using, compared with Vim and Emacs.

The editor includes a kill ring (a shell's clipboard) that can store multiple items. Copying to the kill ring depends on the cursor position: Ctrl+W moves the word before the cursor to the kill ring, and Alt-D moves the next word. Similarly, Ctrl+K moves the contents from the cursor to the end of the line to the kill ring, and Ctrl+U moves the contents from the cursor to the start of the line. Ctrl+Y pastes the latest entry in the kill ring into the editor, and you can select an earlier item with meta key+Y.

The standard way to configure fish is to open the files in ~/.config/fish in the text editor or to create an environment variable using the set command. Key bindings, as well as functions and variables for scripting, can all be created these ways. With the latest release, you can set several basic features by entering the command fish_config for the online configuration tool. For example, you can open fish's web-based configuration tool and assign colors, underlining, and a background to up to 17 different parts of the syntax. However, making extensive use of the syntax highlighting generally requires a dark background to avoid making at least some of the choices invisible (Figure 4).

Figure 4: Configuring syntax highlighting with fish's web-based configuration tool.

The fish_config tool includes a tab containing sample prompts (Figure 5). Each prompt is given a name, with the code to create the prompt manually beside it and the output at the top of the tab. The code is entered in ~/.config/fish/functions/fish_prompt, although it can be created manually at the command prompt; this usually takes longer to set up, but the resulting prompt may be more exactly what you want.

Figure 5: Sample prompts.

An option not included in fish_config is the ability to add your own completions for commands. Instead, you can use the complete command. Each custom completion must be associated with a command and can include a short option of a single letter and a long option of two or more characters. The custom completion can also be associated with a command option and include a description of what the command does. All completions are saved to ~/.config/fish/completions.

Fish vs. Bash

I've described only some of the highlights of fish – mainly, those most likely to interest everyday users. In particular, fish's scripting features deserve an article by themselves, especially because variables and functions are organized and used differently than in Bash. Most of these differences strike me as more rational than Bash's equivalents, but they do take some getting use to.

In fact, if fish has a flaw, it is that the shell spoils you for Bash and other shells. That is fine if you switch entirely to fish, but if you switch back and forth, fish's differences are elaborate enough and numerous enough that confusion is easy. I would be exaggerating if I said that fish is to Bash what Bash is to the Windows shell; however, especially in configuration, the analogy comes close to being true.

For this or other reasons, you may decide – perhaps reluctantly – that fish is not for you. If you do, use chsh to change your default shell back to Bash, or you will probably have to edit the current account as root before you can log in to it again. If fish is not installed from a repository, the documentation suggests running these commands to remove it completely:

rm -Rf /usr/local/etc/fish /usr/local/share/fish ~/.config/fish
rm /usr/local/share/man/man1/fish*.1
cd /usr/local/bin
rm -f fish mimedb fish_pager fishd fish_indent

However, even if you never use fish as your default shell, you might just want to keep it for use in an alternative profile, so you can occasionally enjoy the rich feature set. These days, use of the command line seems to be declining, but fish is an example of how much a shell can still do, given a bit of rethinking and determination.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Charly's Column: Fish

    Columnist Charly serves up Fish as the "shellfish" of the day. His conclusion: tasty, but not something you would want every day.

  • Fish Shell

    The Fish shell offers some user-friendly features for command line beginners.

  • Bash Alternatives

    Don't let your familiarity with the Bash shell stop you from exploring other options. We take a look at a pair of alternatives that are easy to install and easy to use: Zsh and fish.

  • Tool Tips

    Six Linux tools reviewed: FISH 2.0.0, BAD 0.0.2, BLINK, FPING 3.5, LNAV 0.5.0, TOMB 1.4

  • Bash Builtins

    Even beginners can benefit from a greater understanding of the Bash shell’s many builtin commands.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News