This page is not available in every language. Missing translations are disabled in the language selector.

Quick reference

Fish Shell Cheatsheet

A comprehensive reference guide for the friendly interactive shell (fish): keyboard shortcuts, variables, loops, conditionals, functions, completions, and builtins.

· Published

fish is a smart, user-friendly command-line shell for macOS, Linux, and Unix-like operating systems.

Keyboard Shortcuts

Shortcut Description
Ctrl + A / Ctrl + E Move to beginning / end of line
Alt + ← / Alt + → Jump to previous / next word
↑ / ↓ History search matching current line prefix
Alt + ↑ / Alt + ↓ Recall previous / next arguments
Ctrl + U Delete from cursor to beginning of line
Ctrl + C Cancel current command buffer
Alt + H Open man page for current command
Alt + W Show short description for command
Alt + . Insert last argument of previous command

Script Structure

#!/usr/bin/env fish

echo 'Hello from Fish!'

Comments

# This is a comment line

Output & Print

echo 'Hello from Fish!'
# or formatted printing
printf '%s\n' 'Hello from Fish!'

Read from Standard Input (stdin)

read my_variable

Reads user input into variable my_variable.

Loops

for file in *.txt
    echo "Processing $file"
end

while test -f /tmp/lock
    sleep 1
end

Variables

Setting & Erasing

set -l local_var "hello"       # Function-local scope
set -g global_var "world"      # Global scope
set -u universal_var "persists"# Universal variable across fish sessions
set -e local_var               # Erase variable

Slicing & Indices (1-based)

set fruits apple orange banana pear
echo $fruits[1]    # apple (1-indexed)
echo $fruits[-1]   # pear
echo $fruits[2..3] # orange banana

Numbers & Arithmetic

Increment & Decrement

set count (math $count + 1)

Arithmetic Operations (math)

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo
^ Exponentiation
math '5 * (3 + 2)'
math 'sqrt(16)'

Strings

Matching (string match)

string match -r '^[0-9]+$' "12345"
Pattern Matches
x? Zero or one occurrence of x
x* Zero or more occurrences of x
x+ One or more occurrences of x
x{n} Exactly n occurrences of x
x{n,m} Between n and m occurrences
x{n,} At least n occurrences
[xy] Either x or y
[^xy] Any character except x and y
\w Word character
\d Digit character
\W Non-word character
\D Non-digit character

String Replacement & Substrings

string replace -a "foo" "bar" "foofoo"  # "barbar"
string sub -s 1 -l 3 "abcdef"           # "abc"
string trim "  hello  "                 # "hello"

Conditionals & Testing

if test $status -eq 0
    echo "Previous command succeeded"
else if test $status -eq 1
    echo "General failure"
else
    echo "Other status code: $status"
end

Comparison Operators

Numeric

Operator Meaning
-lt Less than
-eq Equal
-gt Greater than
-le Less than or equal
-ge Greater than or equal
-ne Not equal

String

Operator Meaning
= String equals
!= String not equal
-z String is empty
-n String is non-empty

Filesystem

Operator Meaning
-f Regular file exists
-d Directory exists
-r File exists and is readable
-w File exists and is writable
-x File exists and is executable

Process Communication

Redirection

command > output.txt    # Overwrite stdout
command >> output.txt   # Append stdout
command 2> errors.txt   # Redirection stderr
command ^ errors.txt    # Fish shortcut for 2>

Piping

cat names.txt | sort | uniq -c

Command Substitution

set current_branch (git branch --show-current)

The output of (...) replaces the expression.

Process Substitution (psub)

diff -u (sort file1.txt | psub) (sort file2.txt | psub)

(... | psub) generates a temporary named pipe/file containing the command output.

Functions

Definition & Erase

function greet --description "Say hello to a user"
    set -l name $argv[1]
    if test -z "$name"
        set name "World"
    end
    echo "Hello, $name!"
end

# Erase function
functions -e greet

Events & Handlers

Emitting Events

emit my_event $data

Listening to Events

function on_my_event --on-event my_event
    echo "Received event with args: $argv"
end

Abbreviations (abbr)

abbr -a gco git checkout
abbr -a gcb git checkout -b
abbr -a gp  git push
abbr -e gco # erase abbreviation

Autocompletions

Defining Completions

complete -c mytool -s h -l help -d "Display help message"
complete -c mytool -s v -l version -d "Show tool version"
complete -c mytool -f -a "start stop status" -d "Service actions"
Option Description
-c, --command Command to apply completion to
-s, --short-option Short flag character
-l, --long-option Long flag string
-a, --arguments Candidate arguments or space-separated values
-f, --no-files Do not suggest filesystem filenames
-F, --force-files Force suggest filesystem files
-n, --condition Shell condition expression for displaying candidate
-d, --description Human-readable completion description

Useful Built-in Functions

Function Description
__fish_seen_argument Check if specific CLI argument was passed
__fish_seen_subcommand_from Check if any of the given subcommands were seen
__fish_use_subcommand Return true if no subcommands have been specified yet
__fish_complete_directories Complete directories matching pattern
__fish_complete_suffix Complete files ending in given suffix
__fish_complete_users Complete system usernames
__fish_complete_groups Complete system group names
__fish_print_hostnames Print known hostnames from SSH config and hosts
__fish_complete_pids Complete running process IDs
__fish_print_filesystems List known mounted filesystem types
__fish_print_interfaces List network interfaces

References

Related topics