From the Manual
This section contains info that come by default in fish (version 3.5.0 at the time of writing), that I had no idea existed until I started digging around the documentation.
-
Alt-S
prependssudo
to the current commandline. If the commandline is empty,sudo
is prepended to the last commandline. This basically replaces 90% ofsudo !!
in bash for me, and for the remaining 10%Ctrl-P
does the same thing in less keystrokes. -
Alt-E
opens the current commandline in your$EDITOR
. This is extremely useful for long commands, as you get the full text editing capabilities of your editor of choice. Saving and quitting the editor updates the commandline, returning you to the shell. -
Forgot which directory you were in after a long
cd
session? Thedirh
andcdh
commands provide ergonomic ways to navigate previously visited directories in the current shell session!
Functions
Functions in fish can be created and edited in the shell through the funced
command. Upon exiting the editor, the shell will reload and the function will automatically be sourced for interactive use. Running the funcsave
command will then save the function to disk, by default at ~/.config/fish/functions/
. It is also possible to create and define function files manually in the aforementioned directory.
One function I use often lets me change to the n-th parent directory easily. Listed below, the function lets you write $ .. 3
instead of $ cd ../../..
in a regular shell.
function .. -a num -d "Change to the nth parent directory"
if test -n "$num"
set path (string repeat -n "$num" "../")
else
set path "../"
end
cd "$path"
end
Keybindings
Keybindings in fish can be set using the bind command. These keybindings can be placed in config.fish
. However, since fish executes a function called fish_
if it exists, I place my keybindings there.
function fish_user_key_bindings
# Place keybindings here
end
Here are a couple of keybindings I use the most:
-
Unsuspend Jobs With CTRL-Z
bind \cz 'fg 2>/dev/null; commandline -f repaint'
In most shells, CTRL-Z sends the “terminal stop” signal (SIGTSTP) to the job running in the foreground, suspending its execution and returning control to the shell. These jobs can be listed using the
jobs
command, and resumed in the background with thebg
command.A common use case is to suspend a job to temporary gain access to the shell for running other commands. Once done, the job can be brought back to the foreground using the
fg
command. This keybind provides an easier way to run thefg
command. -
Clear the Commandline With CTRL-C
bind \cc 'commandline ""'
In fish, CTRL-C by default ”cancels the entire line”, but it does so by leaving the current commandline as is and moving to a new line (much like pressing the Enter key but without evaluating the command).
On screens with low vertical screen estate, the default behavior may clutter the screen, hiding the output of previous commands. This alternative keybinding wipes the commandline instead, acting as if you held the Backspace key to delete the entire buffer.