Difference between revisions of "Fish"

From ArchWiki
Jump to: navigation, search
(Undo revision 510231 by Wilriker (talk) Not necessary. .bashrc is only invoked for interactive shells as noted in Bash#Configuration files which is already linked here)
(Make su launch fish: command is the recommended way to run a program)
 
(66 intermediate revisions by 5 users not shown)
Line 6: Line 6:
 
[[zh-hans:Fish]]
 
[[zh-hans:Fish]]
  
[https://fishshell.com fish], the "friendly interactive shell", is a "user friendly [[command-line shell|commandline shell]] intended mostly for interactive use".  
+
[https://fishshell.com fish], the '''''f'''riendly '''i'''nteractive '''sh'''ell'', is a [[command-line shell|commandline shell]] intended to be interactive and user-friendly.
 +
 
 +
''fish'' is intentionally not fully [[Wikipedia:POSIX|POSIX]] compliant, it aims at addressing POSIX inconsistencies (as perceived by the creators) with a simplified or a different syntax. This means that even simple POSIX compliant scripts may require some significant adaptation or even full rewriting to run with fish.
  
 
== Installation ==
 
== Installation ==
  
[[Install]] the {{Pkg|fish}} package. Alternatively install the {{AUR|fish-git}} package for the development version.
+
[[Install]] the {{Pkg|fish}} package. For the development version, install the {{AUR|fish-git}} package.
 
 
To make fish the default shell, see [[Shell#Changing your default shell]]; however, you should consider [[#Not setting fish as default shell]].
 
  
Once installed simply type {{ic|fish}} to drop into the fish shell.
+
Once installed, simply type {{ic|fish}} to drop into the fish shell.
  
 
Documentation can be found by typing {{ic|help}} from fish; it will be opened in a web browser. It is recommended to read at least the "Syntax overview" section, since fish's syntax is different from many other shells.
 
Documentation can be found by typing {{ic|help}} from fish; it will be opened in a web browser. It is recommended to read at least the "Syntax overview" section, since fish's syntax is different from many other shells.
  
== Configuration ==
+
== System integration ==
 
 
User configurations for fish are located at {{ic|~/.config/fish/config.fish}}. Adding commands or functions to the file will execute/define them when opening a terminal, similar to {{ic|.bashrc}}.
 
 
 
=== Web interface ===
 
  
The fish prompt and terminal colors can be set with the interactive web interface:
+
One must decide whether fish is going to be the default user's shell, which means that the user falls directly in fish at login, or whether it is used in interactive terminal mode as a child process of the current default shell, here we will assume the latter is [[Bash]]. To elaborate on these two setups:
  
fish_config
+
* fish used as the '''default shell''': this mode requires some basic understanding of the fish functioning and its scripting language. The user's current initialization scripts and environment variables need to be migrated to the new fish environment. To configure the system in this mode, follow [[#Setting fish as default shell]].
  
Selected settings are written to your personal configuration file. You can also view defined functions and your history.
+
* fish used as an '''interactive shell only''': this is the less disruptive mode, all the Bash initialization scripts are run as usual and fish runs on top of Bash in interactive mode connected to a terminal. To setup fish in this mode, follow [[#Setting fish as interactive shell only]].
  
=== Command completion ===
+
=== Setting fish as default shell ===
 
+
If you decide to set fish as the default user shell, the first step is to set the shell of this particular user to {{ic|/usr/bin/fish}}. This can be done by following the instructions in [[Command-line shell#Changing your default shell]].
fish can generate autocompletions from man pages. Completions are written to {{ic|~/.config/fish/generated_completions/}} and can be generated by calling:
 
 
 
fish_update_completions
 
 
 
You can also define your own completions in {{ic|~/.config/fish/completions/}}. See {{ic|/usr/share/fish/completions/}} for a few examples.
 
 
 
Context-aware completions for Arch Linux-specific commands like ''pacman'', ''pacman-key'', ''makepkg'', ''cower'', ''pbget'', ''pacmatic'' are built into fish, since the policy of the fish development is to include all the existent completions in the upstream tarball. The memory management is clever enough to avoid any negative impact on resources.
 
 
 
==Troubleshooting==
 
===History substitution===
 
Fish does not implement history substitution (e.g. {{ic|sudo !!}}), and the fish developers have said that they [http://fishshell.com/docs/current/faq.html#faq-history do not plan to]. Still, this is an essential piece of many users' workflow. Reddit user, [http://www.reddit.com/u/crossroads1112 crossroads1112], created a function that regains some of the functionality of history substitution and with another syntax. The function is on [https://gist.github.com/crossroads1112/77badb2c3455e23b873b github] and instructions are included as comments in it. There is a [https://gist.github.com/b-/981892a65837ab0a387e forked version] that is closer to the original syntax and allows for {{ic|command !!}} if you specify the command in the helper function.
 
  
Other alternatives to regaining the {{ic|command !!}} syntax can be found on [https://github.com/fish-shell/fish-shell/wiki/Bash-Style-History-Substitution-%28%21%21-and-%21%24%29 Fish' github wiki]. The examples here include e.g. the {{ic|bind_bang}} function which expands {{ic|!!}} to the latest command in the history (this will of course make it impossible to do to bangs in a row as they will expand). Another option is the command given on [https://github.com/fish-shell/fish-shell/issues/288#issuecomment-158704275 this github issue].
+
The next step is to port the current needed actions and configuration performed in the various Bash initialization scripts, namely {{ic|/etc/profile}}, {{ic|~/.bash_profile}}, {{ic|/etc/bash.bashrc}} and {{ic|~/.bashrc}}, into the fish framework.
  
==Tips and tricks==
+
In particular, the content of the {{ic|$PATH}} environment variable, once directly logged under fish, should be checked and adjusted to one's need. In fish, {{ic|$PATH}} is defined as a ''global environment variable'': it has a ''global'' scope across all functions, it is lost upon reboot and it is an ''environment variable'' which means it is exported to child processes.
 +
The recommended way of adding permanently additional locations to the path is by assigning them to the {{ic|fish_user_paths}} universal variable. This variable is automatically added to {{ic|$PATH}} and is preserved across restarts of the shell. For example by setting:
  
=== Not setting fish as default shell ===
+
$ set -U fish_user_paths ''/first/path'' ''/second/path'' ''/third/one''
  
{{Out of date|Fish is very usable as a default shell nowadays, it seems to pick up on all the environment variables and the mentioned issues don't seem to exist anymore.}}
+
These three locations will be permanently prepended to the path. This is an easy way to complement the path without the need to add any instruction in scripts.
  
In Arch, some shell scripts are written for [[Bash]] and are not fully compatible with fish. Not setting fish as system wide or user default allows the Bash scripts to run on startup, ensures the environment variables are set correctly, and generally reduces the issues associated with using a non-Bash compatible terminal like fish. You may see some script errors if your default shell is set as fish. Below are several options for using fish without setting it as your default shell.
+
=== Setting fish as interactive shell only ===
 +
Not setting fish as system wide or user default allows the current Bash scripts to run on startup. It ensures the current user's environment variables are unchanged and are exported to fish which then runs as a Bash child. Below are several ways of running fish in interactive mode without setting it as the default shell.
  
 
==== Modify .bashrc to drop into fish ====
 
==== Modify .bashrc to drop into fish ====
 
+
Keep the default shell as Bash and simply add the line {{ic|exec fish}} to the appropriate [[Bash#Configuration files]], such as {{ic|.bashrc}}. This will allow Bash to properly source {{ic|/etc/profile}} and all files in {{ic|/etc/profile.d}}. Because fish replaces the Bash process, exiting fish will also exit the terminal. Compared to the following options, this is the most universal solution, since it works both on a local machine and on a SSH server.
Keep your default shell as Bash and simply add the line {{ic|exec fish}} to the appropriate [[Bash#Configuration files]], such as {{ic|.bashrc}}. This will allow Bash to properly source {{ic|/etc/profile}} and all files in {{ic|/etc/profile.d}}. Because fish replaces the Bash process, exiting fish will also exit the terminal. Compared to the following options, this is the most universal solution, since it works both on a local machine and on a SSH server.
 
  
 
{{Tip|
 
{{Tip|
 
* In this setup, use {{ic|bash --norc}} to manually enter Bash without executing the commands from {{ic|~/.bashrc}} which would run {{ic|exec fish}} and drop back into fish.
 
* In this setup, use {{ic|bash --norc}} to manually enter Bash without executing the commands from {{ic|~/.bashrc}} which would run {{ic|exec fish}} and drop back into fish.
* To color the hostname in the prompt differently in SSH mode, here in bright red, one can use the line {{ic|if [ -n "$SSH_TTY" ]; then exec fish -C 'set -g fish_color_host brred'; else exec fish; fi}} in the Bash configuration file instead of simply {{ic|exec fish}}. }}
+
* To have commands such as {{ic|bash -c 'echo test'}} run the command in Bash instead of starting fish, you can write {{ic|if [ -z "$BASH_EXECUTION_STRING" ]; then exec fish; fi}} instead.}}
 
 
{{Warning|
 
Some entities stop working, e.g. scp to server with this setup [https://www.complang.tuwien.ac.at/doc/openssh-server/faq.html#2.9 will not work].  
 
}}
 
  
 
==== Use terminal emulator options ====
 
==== Use terminal emulator options ====
Line 72: Line 54:
 
  gnome-terminal -e fish
 
  gnome-terminal -e fish
  
With LilyTerm and other light terminal emulators that do not support setting the shell it would look like this:
+
With terminal emulators that do not support setting the shell, for example {{aur|lilyterm-git}}, it would look like this:
  
 
  SHELL=/usr/bin/fish lilyterm
 
  SHELL=/usr/bin/fish lilyterm
  
You can also set fish as the default shell for the terminal in the terminal's configuration or for a terminal profile if your terminal emulator has a profiles feature.
+
Also, depending on the terminal, you may be able to set fish as the default shell in either the terminal configuration or the terminal profile.
 
 
Whenever you open your terminal emulator, you will be dropped into fish.
 
  
 
==== Use terminal multiplexer options ====
 
==== Use terminal multiplexer options ====
Line 88: Line 68:
 
Whenever you run ''tmux'', you will be dropped into fish.
 
Whenever you run ''tmux'', you will be dropped into fish.
  
=== Setting fish as default shell ===
+
== Configuration ==
{{Out of date|The recommended way of setting the path is by one time setting a universal variable on the command line: {{ic|set -U fish_user_paths <path> $fish_user_paths}} [https://fishshell.com/docs/current/tutorial.html#tut_path]}}
+
 
 +
The configuration file runs at every login and is located at {{ic|~/.config/fish/config.fish}}. Adding commands or functions to the file will execute/define them when opening a terminal, similar to {{ic|.bashrc}}. Note that whenever a variable needs to be preserved, it be set as ''universal'' rather than defined in the aforementioned configuration file.
 +
 
 +
The user's functions are located in the directory {{ic|~/.config/fish/functions}} under the filenames {{ic|''function_name''.fish}}.
 +
 
 +
=== Web interface ===
 +
 
 +
The fish terminal colors, prompt, functions, variables, history, bindings and abbreviations can be set with the interactive web interface:
 +
 
 +
fish_config
 +
 
 +
=== Command completion ===
 +
 
 +
fish can generate autocompletions from man pages. Completions are written to {{ic|~/.config/fish/generated_completions/}} and can be generated by calling:
 +
 
 +
fish_update_completions
 +
 
 +
You can also define your own completions in {{ic|~/.config/fish/completions/}}. See {{ic|/usr/share/fish/completions/}} for a few examples.
 +
 
 +
Context-aware completions for Arch Linux-specific commands like ''pacman'', ''pacman-key'', ''makepkg'', ''cower'', ''pbget'', ''pacmatic'' are built into fish, since the policy of the fish development is to include all the existent completions in the upstream tarball. The memory management is clever enough to avoid any negative impact on resources.
 +
 
 +
==Tips and tricks==
  
If you decide to set fish as your default shell, you may find that you no longer have very much in your path.
+
===Command substitution===
You can add a section to your {{ic|~/.config/fish/config.fish}} file that will set your path correctly on login. This is much like {{ic|.profile}} or {{ic|.bash_profile}} as it is only executed for login shells.
+
''fish'' does not implement Bash style history substitution (e.g. {{ic|sudo !!}}), and the developers recommend in the [http://fishshell.com/docs/current/faq.html#faq-history fish faq] to use the interactive history recall interface instead: the {{ic|Up}} arrow recalls whole past lines and {{ic|Alt+Up}} recalls individual arguments.
  
{{bc|
+
However some workarounds are described in the [https://github.com/fish-shell/fish-shell/wiki/Bash-Style-Command-Substitution-and-Chaining-(!!-!$-&&-%7C%7C) fish wiki]: while not providing complete history substitution, some functions replace {{ic|!!}} with the previous command or {{ic|!$}} with the previous last argument.
if status --is-login
 
        set PATH $PATH /usr/bin /sbin
 
end
 
}}
 
  
{{Note|This route requires you to manually add various other environment variables, such as {{ic|$MOZ_PLUGIN_PATH}}. It is a huge amount of work to get a seamless experience with fish as your default shell using this method. A better idea would be [[#Not setting fish as default shell]].}}
+
===Command chaining===
 +
Command chaining {{ic|&&}} and {{ic|{{!}}{{!}}}} is not implemented and the syntax to achieve similar results in ''fish'' is respectively {{ic|; and}} and {{ic|; or}}.
 +
Some keybindings can be set for automatic substitution as described in the [https://github.com/fish-shell/fish-shell/wiki/Bash-Style-Command-Substitution-and-Chaining-(!!-!$-&&-%7C%7C)#getting--and- fish wiki].
  
 
===Disable greeting===
 
===Disable greeting===
  
By default, fish prints a greeting message at startup. To disable it, run {{ic|set -U fish_greeting}} once.
+
By default, fish prints a greeting message at startup. To disable it, run once:
 +
 
 +
  $ set -U fish_greeting
 +
 
 +
This clears the universal {{ic|fish_greeting}} variable, shared with all fish instances and which is preserved upon restart of the shell.
  
 
=== Make su launch fish ===
 
=== Make su launch fish ===
  
If ''su'' starts with Bash (because Bash is the default shell), define a function in your fish configuration file:
+
If ''su'' starts with Bash because Bash is the target user's (''root'' if no username is provided) default shell, one can define a function to redirect it to fish whatever the user's shell:
 
+
{{hc|~/.config/fish/functions/su.fish|2=
function su
+
function su
    /bin/su --shell=/usr/bin/fish $argv
+
  command su --shell=/usr/bin/fish $argv
end
+
end}}
  
 
=== Start X at login ===
 
=== Start X at login ===
Line 133: Line 136:
 
=== Put git status in prompt ===
 
=== Put git status in prompt ===
  
If you would like fish to display the branch and dirty status when you are in a git directory, you can add the following to your {{ic|~/.config/fish/config.fish}}:
+
If you would like fish to display the branch and dirty status when you are in a git directory, you can define the following {{ic|fish_prompt}} function:
{{bc|<nowiki>
+
{{hc|~/.config/fish/functions/fish_prompt.fish|
# fish git prompt
+
<nowiki>function fish_prompt
set __fish_git_prompt_showdirtystate 'yes'
+
  set -l last_status $status
set __fish_git_prompt_showstashstate 'yes'
 
set __fish_git_prompt_showupstream 'yes'
 
set __fish_git_prompt_color_branch yellow
 
  
# Status Chars
+
  if not set -q __fish_git_prompt_show_informative_status
set __fish_git_prompt_char_dirtystate '⚡ '
+
    set -g __fish_git_prompt_show_informative_status 1
set __fish_git_prompt_char_stagedstate '→ '
+
  end
set __fish_git_prompt_char_stashstate '↩ '
+
  if not set -q __fish_git_prompt_color_branch
set __fish_git_prompt_char_upstream_ahead '↑ '
+
    set -g __fish_git_prompt_color_branch brmagenta
set __fish_git_prompt_char_upstream_behind '↓ '
+
  end
+
  if not set -q __fish_git_prompt_showupstream
function fish_prompt
+
    set -g __fish_git_prompt_showupstream "informative"
     printf '%s@%s %s%s%s%s> ' (whoami) (hostname) (set_color $fish_color_cwd) (prompt_pwd) (set_color normal) (__fish_git_prompt)
+
  end
 +
  if not set -q __fish_git_prompt_showdirtystate
 +
    set -g __fish_git_prompt_showdirtystate "yes"
 +
  end
 +
  if not set -q __fish_git_prompt_color_stagedstate
 +
    set -g __fish_git_prompt_color_stagedstate yellow
 +
  end
 +
  if not set -q __fish_git_prompt_color_invalidstate
 +
    set -g __fish_git_prompt_color_invalidstate red
 +
  end
 +
  if not set -q __fish_git_prompt_color_cleanstate
 +
     set -g __fish_git_prompt_color_cleanstate brgreen
 +
  end
 +
 
 +
  printf '%s%s %s%s%s%s ' (set_color $fish_color_host) (prompt_hostname) (set_color $fish_color_cwd) (prompt_pwd) (set_color normal) (__fish_git_prompt)
 +
 
 +
  if not test $last_status -eq 0
 +
    set_color $fish_color_error
 +
  end
 +
  echo -n '$ '
 +
  set_color normal
 
end
 
end
 
</nowiki>}}
 
</nowiki>}}
 
More explanations about the parameters can be found in the [https://github.com/fish-shell/fish-shell/blob/master/share/functions/__fish_git_prompt.fish fish-shell git].
 
More explanations about the parameters can be found in the [https://github.com/fish-shell/fish-shell/blob/master/share/functions/__fish_git_prompt.fish fish-shell git].
 +
 +
===Color the hostname in the prompt in SSH===
 +
To color the hostname in the prompt dynamically whenever connected through SSH, add the following lines in either the {{ic|fish_prompt}} function or the fish configuration file, here using the red color:
 +
 +
{{hc|~/.config/fish/functions/fish_prompt.fish|
 +
...
 +
if set -q SSH_TTY
 +
  set -g fish_color_host brred
 +
end
 +
...}}
  
 
=== Evaluate ssh-agent ===
 
=== Evaluate ssh-agent ===
Line 173: Line 203:
 
This means firefox will not be closed when the fish process is closed. See {{man|1|disown|url=}} in ''fish'' for more details.
 
This means firefox will not be closed when the fish process is closed. See {{man|1|disown|url=}} in ''fish'' for more details.
  
=== Quickly set a persistent alias ===
+
=== Set a persistent alias ===
 +
 
 +
To quickly make a persistent alias, one can simply use the method showed in this example:
 +
$ alias lsl "ls -l"
 +
$ funcsave lsl
 +
 
 +
This will create the function:
 +
function lsl
 +
    ls -l $argv
 +
end
  
To quickly make a persistent alias, one can simply open up fish shell and use the following method:
+
and will set the alias as a persistent shell function. To see all functions and/or edit them, one can simply use {{ic|fish_config}} and look into the ''Function'' tab in the web configuration page.
{{bc|1=<nowiki>
+
 
$ alias FooAliasName "foo"
+
For more detailed information, refer to [https://fishshell.com/docs/current/commands.html#alias fish - alias].
$ funcsave FooAliasName
 
</nowiki>}}
 
This will set you alias as a persistent fish shell function. if you wish to see all functions and/or edit them, one can simply use
 
{{ic|fish_config}} to view or edit all functions under the '''Function''' tab in the resulting web configuration page.
 
  
 
== See also ==
 
== See also ==
Line 188: Line 223:
 
* http://fishshell.com/docs/current/index.html - Documentation
 
* http://fishshell.com/docs/current/index.html - Documentation
 
* http://hyperpolyglot.org/unix-shells - Shell grammar correspondence table
 
* http://hyperpolyglot.org/unix-shells - Shell grammar correspondence table
 +
* https://github.com/fish-shell/fish-shell - fish on GitHub

Latest revision as of 06:25, 5 December 2018

fish, the friendly interactive shell, is a commandline shell intended to be interactive and user-friendly.

fish is intentionally not fully POSIX compliant, it aims at addressing POSIX inconsistencies (as perceived by the creators) with a simplified or a different syntax. This means that even simple POSIX compliant scripts may require some significant adaptation or even full rewriting to run with fish.

Installation

Install the fish package. For the development version, install the fish-gitAUR package.

Once installed, simply type fish to drop into the fish shell.

Documentation can be found by typing help from fish; it will be opened in a web browser. It is recommended to read at least the "Syntax overview" section, since fish's syntax is different from many other shells.

System integration

One must decide whether fish is going to be the default user's shell, which means that the user falls directly in fish at login, or whether it is used in interactive terminal mode as a child process of the current default shell, here we will assume the latter is Bash. To elaborate on these two setups:

  • fish used as the default shell: this mode requires some basic understanding of the fish functioning and its scripting language. The user's current initialization scripts and environment variables need to be migrated to the new fish environment. To configure the system in this mode, follow #Setting fish as default shell.
  • fish used as an interactive shell only: this is the less disruptive mode, all the Bash initialization scripts are run as usual and fish runs on top of Bash in interactive mode connected to a terminal. To setup fish in this mode, follow #Setting fish as interactive shell only.

Setting fish as default shell

If you decide to set fish as the default user shell, the first step is to set the shell of this particular user to /usr/bin/fish. This can be done by following the instructions in Command-line shell#Changing your default shell.

The next step is to port the current needed actions and configuration performed in the various Bash initialization scripts, namely /etc/profile, ~/.bash_profile, /etc/bash.bashrc and ~/.bashrc, into the fish framework.

In particular, the content of the $PATH environment variable, once directly logged under fish, should be checked and adjusted to one's need. In fish, $PATH is defined as a global environment variable: it has a global scope across all functions, it is lost upon reboot and it is an environment variable which means it is exported to child processes. The recommended way of adding permanently additional locations to the path is by assigning them to the fish_user_paths universal variable. This variable is automatically added to $PATH and is preserved across restarts of the shell. For example by setting:

$ set -U fish_user_paths /first/path /second/path /third/one

These three locations will be permanently prepended to the path. This is an easy way to complement the path without the need to add any instruction in scripts.

Setting fish as interactive shell only

Not setting fish as system wide or user default allows the current Bash scripts to run on startup. It ensures the current user's environment variables are unchanged and are exported to fish which then runs as a Bash child. Below are several ways of running fish in interactive mode without setting it as the default shell.

Modify .bashrc to drop into fish

Keep the default shell as Bash and simply add the line exec fish to the appropriate Bash#Configuration files, such as .bashrc. This will allow Bash to properly source /etc/profile and all files in /etc/profile.d. Because fish replaces the Bash process, exiting fish will also exit the terminal. Compared to the following options, this is the most universal solution, since it works both on a local machine and on a SSH server.

Tip:
  • In this setup, use bash --norc to manually enter Bash without executing the commands from ~/.bashrc which would run exec fish and drop back into fish.
  • To have commands such as bash -c 'echo test' run the command in Bash instead of starting fish, you can write if [ -z "$BASH_EXECUTION_STRING" ]; then exec fish; fi instead.

Use terminal emulator options

Another option is to open your terminal emulator with a command line option that executes fish. For most terminals this is the -e switch, so for example, to open gnome-terminal using fish, change your shortcut to use:

gnome-terminal -e fish

With terminal emulators that do not support setting the shell, for example lilyterm-gitAUR, it would look like this:

SHELL=/usr/bin/fish lilyterm

Also, depending on the terminal, you may be able to set fish as the default shell in either the terminal configuration or the terminal profile.

Use terminal multiplexer options

To set fish as the shell started in tmux, put this into your ~/.tmux.conf:

set-option -g default-shell "/usr/bin/fish"

Whenever you run tmux, you will be dropped into fish.

Configuration

The configuration file runs at every login and is located at ~/.config/fish/config.fish. Adding commands or functions to the file will execute/define them when opening a terminal, similar to .bashrc. Note that whenever a variable needs to be preserved, it be set as universal rather than defined in the aforementioned configuration file.

The user's functions are located in the directory ~/.config/fish/functions under the filenames function_name.fish.

Web interface

The fish terminal colors, prompt, functions, variables, history, bindings and abbreviations can be set with the interactive web interface:

fish_config

Command completion

fish can generate autocompletions from man pages. Completions are written to ~/.config/fish/generated_completions/ and can be generated by calling:

fish_update_completions

You can also define your own completions in ~/.config/fish/completions/. See /usr/share/fish/completions/ for a few examples.

Context-aware completions for Arch Linux-specific commands like pacman, pacman-key, makepkg, cower, pbget, pacmatic are built into fish, since the policy of the fish development is to include all the existent completions in the upstream tarball. The memory management is clever enough to avoid any negative impact on resources.

Tips and tricks

Command substitution

fish does not implement Bash style history substitution (e.g. sudo !!), and the developers recommend in the fish faq to use the interactive history recall interface instead: the Up arrow recalls whole past lines and Alt+Up recalls individual arguments.

However some workarounds are described in the fish wiki: while not providing complete history substitution, some functions replace !! with the previous command or !$ with the previous last argument.

Command chaining

Command chaining && and || is not implemented and the syntax to achieve similar results in fish is respectively ; and and ; or. Some keybindings can be set for automatic substitution as described in the fish wiki.

Disable greeting

By default, fish prints a greeting message at startup. To disable it, run once:

 $ set -U fish_greeting

This clears the universal fish_greeting variable, shared with all fish instances and which is preserved upon restart of the shell.

Make su launch fish

If su starts with Bash because Bash is the target user's (root if no username is provided) default shell, one can define a function to redirect it to fish whatever the user's shell:

~/.config/fish/functions/su.fish
function su
   command su --shell=/usr/bin/fish $argv
end

Start X at login

Add the following to the bottom of your ~/.config/fish/config.fish.

# Start X at login
if status is-login
    if test -z "$DISPLAY" -a $XDG_VTNR = 1
        exec startx -- -keeptty
    end
end

Use liquidprompt

Liquidprompt is a popular "full-featured & carefully designed adaptive prompt for Bash & Zsh" and has no plans to make it compatible with fish [1]. This project implements it for fish.

Put git status in prompt

If you would like fish to display the branch and dirty status when you are in a git directory, you can define the following fish_prompt function:

~/.config/fish/functions/fish_prompt.fish
function fish_prompt
  set -l last_status $status

  if not set -q __fish_git_prompt_show_informative_status
    set -g __fish_git_prompt_show_informative_status 1
  end
  if not set -q __fish_git_prompt_color_branch
    set -g __fish_git_prompt_color_branch brmagenta
  end
  if not set -q __fish_git_prompt_showupstream
    set -g __fish_git_prompt_showupstream "informative"
  end
  if not set -q __fish_git_prompt_showdirtystate
    set -g __fish_git_prompt_showdirtystate "yes"
  end
  if not set -q __fish_git_prompt_color_stagedstate
    set -g __fish_git_prompt_color_stagedstate yellow
  end
  if not set -q __fish_git_prompt_color_invalidstate
    set -g __fish_git_prompt_color_invalidstate red
  end
  if not set -q __fish_git_prompt_color_cleanstate
    set -g __fish_git_prompt_color_cleanstate brgreen
  end

  printf '%s%s %s%s%s%s ' (set_color $fish_color_host) (prompt_hostname) (set_color $fish_color_cwd) (prompt_pwd) (set_color normal) (__fish_git_prompt)

  if not test $last_status -eq 0
    set_color $fish_color_error
  end
  echo -n '$ '
  set_color normal
end

More explanations about the parameters can be found in the fish-shell git.

Color the hostname in the prompt in SSH

To color the hostname in the prompt dynamically whenever connected through SSH, add the following lines in either the fish_prompt function or the fish configuration file, here using the red color:

~/.config/fish/functions/fish_prompt.fish
...
if set -q SSH_TTY
  set -g fish_color_host brred
end
...

Evaluate ssh-agent

In fish, eval (ssh-agent) generate errors due to how variables are set. To work around this, use the csh-style option -c:

 $ eval (ssh-agent -c)

The "command not found" hook

pkgfile includes a "command not found" hook that will automatically search the official repositories, when entering an unrecognized command. This hook will be run by default if pkgfile is installed.

Remove a process from the list of jobs

fish terminates any jobs put into the background when fish terminates. To keep a job running after fish terminates, first use the disown builtin. For example, the following starts firefox in the background and then disowns it:

 $ firefox &
 $ disown

This means firefox will not be closed when the fish process is closed. See disown(1) in fish for more details.

Set a persistent alias

To quickly make a persistent alias, one can simply use the method showed in this example:

$ alias lsl "ls -l"
$ funcsave lsl

This will create the function:

function lsl
    ls -l $argv
end

and will set the alias as a persistent shell function. To see all functions and/or edit them, one can simply use fish_config and look into the Function tab in the web configuration page.

For more detailed information, refer to fish - alias.

See also