Back to Cheatsheets

Fish shell

CLI2018-01-31

Keys

ShortcutDescription
^A ←/^E →Move to the line beginning/end
Alt ←/Alt →Jump to the previous/next word
/Switch to the previous/next command
Alt ↑/Alt ↓Switch to the previous/next arguments
------
^UDelete to the beginning
^CCancel the line
------
Alt HShow the command man page description
Alt WShow the short command description
------
Alt .Repeat last argument

Sample program

#!/usr/bin/env fish

echo 'Hello from Fish!'

Comments

# my comment

Printing text

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

Print the string with a trailing \n.

Reading from stdin

read my_variable

Reads the string to a variable my_variable.

Loops

for i in (seq 1 10)
  ...
end

Variables

Defining and erasing

# Declare the global/local variable:
set my_variable 'Hello from Fish!'

i# Remove the variable:
set --erase my_variable

Slicing

echo $my_variable[1..10]
echo $my_variable[2..]
echo $my_variable[..-2]

Numbers

Incrementing and decrementing

set my_variable (math $my_variable + 1)
set my_variable (math $my_variable - 1)

Arithmetic

echo (math 1 + 2)
OperatorPerforms
+Addition
-Subtraction
*Multiplication
/Division
%Modulo
^Exponentiation

Strings

Matching

Match the string against a regular expression:

string match --regex --entire 'Fish' 'Hello from Fish!'
PatternMatches
x?Zero or one x chars
x*Any count x chars
x+One or more x chars
x{n}n times x chars
x{n,m}n to m times x chars
x{n,}n or more times x chars
[xy]x or y char
[^xy]not x or y char
------
\wWord character
\dDigit character
\WNot word character
\DNot digit character

Perl compatible regular expressions are described here.

Replacing

# Replaces the first match
string replace --regex 'Fish' 'fish' 'Hello from Fish!'

# Replaces all matches
string replace --regex --all 'Fish' 'fish' 'Hello from Fish!'

Conditionals

If/else

if test $my_variable -lt $another_variable
  ···
else if test $my_variable -eq $another_variable
  ···
else
  ···
end

Comparisons

Numbers

Number operatorMeaning
-lt[L]ess [t]han
-eq[Eq]ual
-gt[G]reater [t]han
-le[L]ess than or [e]qual to
-ge[G]reater than or [e]qual to
-ne[N]ot [E]qual

Strings

String operatorMeaning
=[Eq]ual
!=[N]ot [E]qual

Files

File operatorMeaning
-f[F]ile exists
-d[D]irectory exists
-rFile or directory exists and [r]eadable
-wFile or directory exists and [w]ritable
-xFile or directory exists and e[x]ecutable

Process communication

Writing to files

# Overwrite file
echo 'Hello from Fish!' > my_file

# Append to file
echo 'Hello from Fish!' >> my_file

Piping

my_command | another_command

Passes the first command stdout output as an input to a second command.

Command substitution

echo (math $my_variable + 1)

The (...) expression is substituted with the output of the command inside it.

Process substitution

echo (math $my_variable + 1 | psub)

The (... | psub) expression is substituted with a temporary file with the command's output.

Functions

Defining and erasing

# Declare the function
function my_function --description 'My description'
  ···
end

# Remove the function
functions --erase my_function

Events

Emitting

emit my_event

Emits an event that can be picked up by other functions.

Event handling

function my_hook --on-event my_event
  ···
end

Reacts to the my_event event.

Abbreviations

Defining and erasing

# Declare the abbreviation
abbr --add grh "git reset --hard HEAD"
# Remove the abbreviation
abbr --erase grh

Completions

Defining completions

complete --command mycommand --arguments 'install uninstall'
complete --command mycommand --short-option 'h' --long-option 'help' --description 'Display help'
OptionDescription
--argumentsArguments to the command itself or option
--short-optionShort option
--long-optionLong option
--no-filesDon't suggest files
--force-filesSuggest files
--conditionDisplay the hint only when a given condition is true
--descriptionDescription

Declares the completion for a command.

Removing completions

complete --command mycommand --erase

Useful built-in functions

FunctionDescription
__fish_seen_argumentCheck whether the specified argument is used
__fish_seen_subcommand_fromCheck whether the specified subcommand is used
__fish_use_subcommandCheck whether any subcommand is used
------
__fish_complete_directoriesComplete directories with the specified letters in their name
__fish_complete_suffixComplete files with the specified suffix
------
__fish_complete_usersList all users
__fish_complete_groupsList all user groups
__fish_print_hostnamesList all host names
__fish_complete_pidsList all PIDs
__fish_print_filesystemsList all known filesystems
__fish_print_interfacesList all network interfaces