CLI Magic: Enhancing the shell with fish

556

Author: Bruce Byfield

The Friendly Interactive Shell (fish) is an alternative command line that is designed to be easy to learn and use. fish turns on by default options that are available in shells such as Bash or tcsh and develops them far beyond other shells. The result is a command line that can go a long way toward curing the phobia that many GNU/Linux users nurse from their experience with the DOS command line.

The fish shell has far too many features to discuss all of them in detail. The four that users are most likely to encounter first and appreciate most are the browser help, advanced syntax highlighting, tab completion, and command history.

Fish comes with the usual man pages, as well as help files available with a simple -h switch. However, unlike most shells, fish also comes with an HTML help page. Users can access the top of the help page by typing help. They can enter a bookmark name after the command (for example, help variables) or a fish-specific command to jump to a particular section. This arrangement is somewhat weakened by the fact that, until they read the help for the first time, users who lack shell experience may not know what sections exist. Yet, in general, fish makes good use of browser help by presenting information in easy-to-scan bulleted lists.

Advanced syntax highlighting

By default, fish enables the coloring of different file types in the ls command, but that is only the beginning of fish’s syntax highlighting. As you move about your home directory, fish adds subdirectories to the prompt in green. Move out of your own directory, and paths are added from the root directory of the system. As you enter commands, fish colors each aspect of them, displaying commands in green, paths and files in the default shell color, and regular expressions in blue. Similarly, when the cursor is at one quotation mark or parenthesis, its matching one is also highlighted. These color conventions are handy for newcomers who are still learning, but experienced users who know them may find the highlighting equally useful for keeping track of complex syntax.

fish displays in red a wide variety of errors, ranging from non-existent files and commands to unclosed parentheses and incorrect redirects of output. This display of errors can be useful, but it can also be a distraction; since fish reinterprets your input as you type, it sometimes displays an error simply because you haven’t finished typing yet. The coloring of errors can be turned off in the configuration files, but a more useful option might be to suppress the display of a string as an error until it is complete.

Tab completion and command history

In the default Bash shell that comes with most GNU/Linux distributions, you can type a string and press the Tab key to have a command or file name automatically completed, but only if you type enough of the command that only one completion is available. The feature saves some typing, but is not very useful if you fail to remember the exact spelling of the command.

By contrast, fish boasts what its wiki describes as “tab completion as steroids.” In fish, tab completion can be called at any point in your typing, not just when a single alternative is possible. The results are displayed in a scrollable pager with the text you entered highlighted, and a two- or three-word description of each result. You can then highlight a result and paste it into the cursor.

In addition to saving time and aiding users’ memories, tab completion in fish also serves as a quick search utility. For example, instead of using the find command, you could enter py then press Tab to see what Python-related files are installed on your system, or enter apt on a Debian system to see what apt-based utilities are installed. Although its capacity is more limited than that of search tools, fish’s tab completion has the advantage of requiring less expertise to use effectively.

Like tab completion, command history is streamlined in fish, with all but the most recent of duplicate entries automatically removed from the history. This removal is desirable because fish allows you to search the history. Normally, pressing the Up or Down arrow keys scrolls through the history just as in Bash, but if a string is entered at the prompt first, fish displays only entries in the history that match the string. You can also refine the search even further by pressing Alt-Up or Alt-Down to divide the string into separate elements and search only for the element the cursor is currently on. If a search through the history takes too long, you can stop it by pressing the Esc key.

Advanced features, faults, and assessment

These are only the most obvious features of fish. More experienced users may appreciate its regularized command syntax and English-like scripting, or its specialized environment variables to set such elements as the default browser and new file permissions, or the ability to customize features such as syntax highlighting and tab completion. Some of the commands specific to fish, such as open, which reads the desktop environment’s MIME database to determine what program to open a file in, or fg and bg, which together toggle processes out of and into the foreground, offer further small extensions of standard shell functionality. When you switch to other shells, some of these features are sufficiently different in structure that they might be confusing, but, regarded by themselves, they represent a more rational and powerful shell than many users are likely to have seen.

The fish shell does have some faults. Some of its messages are unnecessarily cryptic; failing to specify a destination with the cp command, for example, results in the unhelpful message “Unexpected end of token” — a phrase far more unhelpful than anything I’ve ever seen in Bash. On my Debian testing installation, fish terminals also showed a tendency to randomly crash.

Yet, despite these shortcomings, fish remains an interesting example of what a shell can be. Anyone who has scanned the man pages of the ls or cp commands knows that the basic commands in a Bash shell are frequently more flexible and powerful than the desktop tools meant to replace them. By adding dozens of small but useful innovations, fish shows that, even after several decades of development, the standard Unix shell still has room for enhancements.

Bruce Byfield is a course designer and instructor, and a computer journalist who writes regularly for NewsForge, Linux.com and IT Manager’s Journal.