[Repost] fish-shell Tutorial

This article is transcribed by SimpRead, original at www.ruanyifeng.com

Command line is an essential skill for programmers. Although graphical interfaces look good, solving problems still depends on the command line.

Command line is an essential skill for programmers. Although graphical interfaces look good, solving problems still depends on the command line.

The command line is provided by the Shell. Various commands pass through the Shell to the operating system kernel. Learning the command line is learning the Shell.

There are several types of Shell, the most commonly used nowadays are Bash and zsh. However, in my view, they are not as user-friendly as Fish Shell.

Five years ago, I tried Fish for the first time and was amazed. I have been using it ever since. This article introduces the main features of Fish, hoping you will try it too.

Thanks to Udacity for sponsoring this article. Their recommended courses are at the end.

1. Introduction

Fish stands for “the friendly interactive shell,” with its biggest feature being easy to use. Many functions that require configuration in other Shells are provided by Fish by default without any configuration.

If you want a convenient and easy-to-use Shell but don’t want to learn a lot of syntax or spend much time configuring, you should definitely try Fish.

2. Installation

Installation method for Ubuntu and Debian.

$ sudo apt-get install fish

Installation method for Mac.

$ brew install fish

For other systems, please refer to the official website.

3. Starting and Help

After installation, you can start Fish.

$ fish

Since Fish’s syntax differs greatly from Bash’s, Bash scripts are generally incompatible. Therefore, I recommend not setting Fish as the default Shell, but starting it manually each time.

During use, if you need help, you can enter the help command. The browser will automatically open with the online documentation displayed.

$ help

4. Colored Display

Once you enter Fish, the first thing you might notice is the default colored display.

# Invalid commands are red
$ mkd

# Valid commands are blue
$ mkdir

Valid paths will have an underline.

$ cat ~/somefi

The above code means there is a path starting with ~/somefi. If there is no underline, you know this path does not exist.

5. Automatic Suggestions

Fish automatically gives suggestions after the cursor, indicating possible options in gray color.

# Command suggestions
$ /bin/hostname

# Parameter suggestions
$ grep --ignore-case

# Path suggestions
$ ls node_modules

If you accept the suggestion, press or Control + F. If you want to accept only part of it, press Alt + →.

6. Autocompletion

When entering commands, Fish will automatically display matching previous history.

$ git commit -m "feat: first commit"

If there is no matching history, Fish guesses possible results and autocompletes various inputs. For example, entering pyt and then pressing Tab will autocomplete to the python command.

If there are multiple possible results, Fish will list them all, along with brief descriptions.

$ vi[press Tab]

vi (Executable link, 2.7MB)
view (Vi IMproved, a programmer's text editor)
viewer.py (Executable, 967B)
viewres  (Graphical class browser for Xt)
...and 12 more rows

At this point, press Tab again to select from these commands.

Besides completing commands, Fish also completes parameters. For example, after typing ls command’s -l parameter and pressing Tab, it will show other parameters that can be combined.

$ ls -l[press Tab]

-l1  (List one file per line)
-lA  (Show hidden except . and ..)  
-la  (Show hidden)
-lB  (Ignore files ending with ~)
...and 16 more rows```

Fish can also autocomplete Git branches.

$ git checkout master

7. Readable Syntax

Fish syntax is very natural and easy to understand at a glance.

if statement.

if grep fish /etc/shells
    echo Found fish
else if grep bash /etc/shells
    echo Found bash
else
    echo Got nothing
end

switch statement.

switch (uname)
case Linux
    echo Hi Tux!
case Darwin
    echo Hi Hexley!
case FreeBSD NetBSD DragonFly
    echo Hi Beastie!
case '*'
    echo Hi, stranger!
end

while loop.

while true
    echo "Loop forever"
end

for loop.

for file in *.txt
    cp $file $file.bak
end

8. Functions

Fish functions are used to encapsulate commands or create aliases for existing commands.

function ll
    ls -lhG $argv
end

The above code defines a function named ll. After running this function in the command line, you can use ll to replace ls -lhG. The variable $argv represents the parameters of the function.

Here is another example.

function ls
    command ls -hG $argv
end

The above code redefines the ls command. Note that inside the function body, the ls must be prefixed with command, otherwise an infinite loop error will occur.

9. Prompt

The fish_prompt function is used to define the command line prompt.

function fish_prompt
    set_color purple
    date "+%m/%d/%y"
    set_color FF0
    echo (pwd) '>'
    set_color normal
end

After executing the above function, your command line prompt will look like this.

02/06/13
/home/tutorial >

10. Configuration

Fish’s configuration file is ~/.config/fish/config.fish. Each time Fish starts, it automatically loads this file.

We can write various custom functions in this file, and they will be auto-loaded. For example, the above fish_prompt function can be written in this file, so every time Fish starts, the custom prompt will appear.

Fish also provides a web interface for configuring this file.

$ fish_config

After entering the above command, the browser will open automatically on localhost port 8000. Users can configure Fish on the webpage, such as selecting the prompt and color theme.

(End of text)