Show Menu

fish shell Cheat Sheet by

A cheat sheet for using the fish shell.
fish     unix     shell

Nicely Formatted Latex Version

Fish Syntax

Three kinds: universal, global, and local. Universal variables are shared btw. all sessions on the computer per user. Global variables are specific to the current fish session, but they are outside of any block scope. Local variables are specific to a particular block scope and are automa­tically erased.

Set a variable as universal with -U, as global with -g, or local with -l. Scoping rules are as follows:
1. If a variable is explicitly set to either universal, global or local, that setting will be honored. If a variable of the same name exists in a different scope, that variable will not be changed.
2. If a variable is not explicitly set to be either universal, global or local, but has been previously defined, the variable scope is not changed.
3. If a variable is not explicitly set to be either universal, global or local and has never before been defined, the variable will be local to the currently executing function. Note that this is different from using the -l or –local flag. If one of those flags is used, the variable will be local to the most inner currently executing block, while without these the variable will be local to the function. If no function is executing, the variable will be global.

Expo­rting Variab­les
Export a variable with -x.

Store multiple strings in one variable with an array. Access an index:
echo $PATH[3]

for i in $PATH; echo $i is in the path; end

set smurf blue small makes an array called smurf containing "­blu­e" and "­sma­ll".

Delete an element:
set -e smurf[1]

Define a function like so:
function ll
ls -l $argv
Access arguments using $argv, call the function using ll.

When you execute a command, it starts a job. You can put a job in the background by adding the & suffix. You can suspend a currently running job using Ctrl-Z. You can put the suspended job in the background with bg. Finally, you can list all running jobs with jobs.

Chaining Commands
Each command ends in either a newline or a semicolon. Chain commands using command; and command2 or command; or command2. and and or check the previous command's exit status and act accord­ingly.

To define an alias, either make a function:
function ls
command ls --colo­r=auto $argv
...or use use alias NAME DEFINI­TION which does this for you.

IO Redire­ction and Piping

Redirect stdin
N<­SOU­RCE­_FILE (N is optional, default is 0)
Redirect stdout
N>­DES­TIN­ATION (N is optional; default is 1)
Redirect stderr
N^DES­TIN­ATION (N is optional; default is 2)
Redirect with appending
>> or ^^ + DESTI­NAT­ION­_FILE
Close FD
Pipe stdout
command1 | command2
Pipe a different FD
command1 N>| command2


How do I glob for all but one specific file?
find -mindepth 1 -maxdepth 1 -type f -a -name "­*.j­pg"


Quotes and expans­ion
Without quotes, variables are expanded and characters are escaped. In double quotes, variables are expanded, but no characters are escaped (except for \", \$, and \\); in single quotes, everything is literal (except for \' and \\).

Command Expans­ion
Surround the command in parent­heses. If it returns multiple lines, they'll be concat­enated with spaces.

Para­meter Expans­ion
Use find for most globbing. Fish supports ? for any single character (except /), for any string of characters (except /) (including empty string), and * for any string of charac­ters, including the empty string and /.

Files beginning with . are ignored unless a . is the first character of the glob.

Brace Expans­ion
A comma separated list of characters enclosed in curly braces is expanded to each element of the list is a new parameter:
echo input.{­c,­h,txt}
>> input.c input.h input.txt

Variable Expans­ion
A $ followed by a string of characters is expanded to the value of the enviro­nmental variable with that name. Surround the variable with braces to separate it from text.

Process Expans­ion
% followed by a string is expanded into a PID according to the following rules:
1. If the string is self, insert the shell's PID
2. If the string is the ID of a job, insert the process group ID of the job
3. If any child processes match the string, insert their PIDs
4. If any processes owned by the user match the string, insert their PIDs
5. If none of the above matches, then produce an error

Index Range Expans­ion
Select a range of values from an array using ..:
echo (seq 10)[2..5 1..3]
>> 2 3 4 5 1 2 3


array of arguments to a shell function; only defined in a function call or when fish is invoked to run a script
array containing the last commands that were entered
the user's home directory
the current working directory
the exist status of the last foreground job to exit
the current username

Command line editor

Complete current token
Accept autosu­gge­stion
at end of line: End/Ct­rl-­E/R­igh­t/C­trl-F
Move to beginning of line
Move to end of line
Move charac­ter­-wise
Left/­Ctrl-B or Right/­Ctrl-F
Move word-wise
Alt-Left or Alt-Right
Move through directory history
on empty cmd line: Alt-Left or Alt-Right
Search history for prefix in cmd line
Up or Down
Search history for token containing token under cursor
Alt-Up or Alt-Down
Delete charac­terwise
Delet­e/C­trl-D (forwards) or Backspace (backw­ards)
Delete entire line
Move contents from cursor to EOL to killring
Move contents from beginning of line to cursor to killring
Repaint screen
Move preview word to killring
Move next word to killring
Print descri­ption of cmd under cursor
List contents of current directory or directory under cursor
Add '|less;' to end of job under cursor
Capitalize current word
Make current word uppercase
These shortcuts can be changed using the bind built-in command.

Download the fish shell Cheat Sheet

2 Pages

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!



No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets