Difference between revisions of "Fish"

From ArchWiki
Jump to: navigation, search
m (style aderence)
(When the XDG_VTNR variable is empty, the test command is failing, this way instead it should always behave correctly)
 
(100 intermediate revisions by 25 users not shown)
Line 1: Line 1:
 +
{{lowercase title}}
 
[[Category:Command shells]]
 
[[Category:Command shells]]
 
[[de:Fish]]
 
[[de:Fish]]
'''Fish''' is a user friendly commandline shell intended mostly for interactive use.
+
[[ja:Fish]]
 +
[[ru:Fish]]
  
== Installation instructions ==
+
'''fish''' (the '''friendly interactive shell''') is a user friendly command line shell intended mostly for interactive use.
[[pacman|Install]] {{Pkg|fish}} from the [[official repositories]].
+
  
To verify that it has been installed you can run:
+
== Installation ==
$ less /etc/shells
+
  
If you wanted to make Fish your default shell run:
+
[[Install]] the {{Pkg|fish}} package. Alternatively install the {{AUR|fish-git}} package for the development version.
$ chsh -s /usr/bin/fish
+
  
== Pacman and Fish ==
+
To make fish the default shell, see [[Shell#Changing your default shell]]; however, you should consider [[#Not setting fish as default shell]].
Context-aware completions for pacman and makepkg 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 ==
+
Once installed simply type {{ic|fish}} to drop into the fish shell.
In arch, there are a lot of shell scripts written for Bash, and these have not been translated to Fish. It is advisable not to set Fish as your default shell because of this. The best option is to open your terminal emulator (gnome-terminal, konsole, sakura, etc...) 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:
+
 
 +
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 ==
 +
 
 +
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:
 +
 
 +
fish_config
 +
 
 +
Selected settings are written to your personal configuration file. You can also view defined functions and your history.
 +
 
 +
=== 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.
 +
 
 +
==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.
 +
 
 +
=== Not setting fish as default shell ===
 +
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.
 +
 
 +
==== Modify .bashrc to drop into fish ====
 +
 
 +
Keep your default shell as Bash and simply add the line {{ic|fish && exit}} 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}}. When you exit ''fish'' you will seamlessly exit ''bash'' as well. Compared to the following options, this is the most universal solution, since it works both on a local machine and on an SSH server.
 +
 
 +
{{Tip|Use {{ic|bash --norc}} to enter bash manually without dropping to fish after setting up as above.}}
 +
 
 +
==== 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 {{ic|-e}} switch, so for example, to open gnome-terminal using fish, change your shortcut to use:
  
 
  gnome-terminal -e fish
 
  gnome-terminal -e fish
  
With LilyTerm and other light terminal emulators that don't support setting the shell it would look like this:
+
With LilyTerm and other light terminal emulators that do not support setting the shell it would look like this:
  
 
  SHELL=/usr/bin/fish lilyterm
 
  SHELL=/usr/bin/fish lilyterm
  
Another option is to 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. This is contrast to changing the default shell for the user which would cause the above mentioned problem.
+
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.
  
To set Fish as the shell started in tmux, put this into your {{ic|.tmux.conf}}:
+
Whenever you open your terminal emulator, you will be dropped into fish.
 +
 
 +
==== Use terminal multiplexer options ====
 +
 
 +
To set fish as the shell started in tmux, put this into your {{ic|~/.tmux.conf}}:
  
 
  set-option -g default-shell "/usr/bin/fish"
 
  set-option -g default-shell "/usr/bin/fish"
  
Not setting Fish as system wide default allows the arch scripts to run on startup, ensure the environment variables are set correctly, and generally reduces the issues associated with using a non-Bash compatible terminal like Fish.
+
Whenever you run ''tmux'', you will be dropped into fish.
  
If you decide to set Fish as your default shell, you may find that you no longer have very much in your path.
+
==== Working with fish as default shell ====
 +
 
 +
If you decide to set fish as your default shell, you may find that you no longer have very much in your path.
 
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.
 
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.
  
if status --is-login
+
{{bc|
    set PATH $PATH /usr/bin /sbin
+
if status --is-login
end
+
        set PATH $PATH /usr/bin /sbin
 +
end
 +
}}
  
Note that you will need to manually add various other environment variables, such as $MOZ_PLUGIN_PATH. It is a huge amount of work to get a seamless experience with Fish as your default shell.
+
Note that 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 to [[#.2Fetc.2Fprofile_and_.7E.2F.profile_compatibility|source /etc/profile and ~/.profile]].
  
== Configuration Suggestions ==
+
=== /etc/profile and ~/.profile compatibility ===
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}}:
+
  
set fish_git_dirty_color red
+
Since standard POSIX {{ic|sh}} syntax is not compatible with fish, fish will not be able to source {{ic|/etc/profile}} and thus all {{ic|*.sh}} in {{ic|/etc/profile.d}}) and {{ic|~/.profile}}. If you have fish as your default shell, you can work around this by doing the following:
function parse_git_dirty
+
 
          git diff --quiet HEAD ^&-
+
Install {{Pkg|dash}} and add this line to your {{ic|config.fish}}:
          if test $status = 1
+
  env -i HOME=$HOME dash -l -c printenv | sed -e '/PWD/d; /PATH/s/:/ /g;s/=/ /;s/^/set -x /' | source
            echo (set_color $fish_git_dirty_color)"Δ"(set_color normal)
+
 
          end
+
an alternative variant will save you one executable invocation by using a builtin command:
end
+
  env -i HOME=$HOME dash -l -c 'export -p' | sed -e "/PWD/d; /PATH/s/'//g;/PATH/s/:/ /g;s/=/ /;s/^export/set -x/" | source
  function parse_git_branch
+
 
          # git branch outputs lines, the current branch is prefixed with a *
+
Also consider [[#Not setting fish as default shell]].
          set -l branch (git branch --color ^&- | awk '/*/ {print $2}')
+
 
          echo $branch (parse_git_dirty)   
+
==Tips and tricks==
end
+
===Disable greeting===
   
+
By default, fish prints a greeting message at startup. To disable it, add {{ic|set fish_greeting}} to your fish configuration file.
function fish_prompt
+
 
          if test -z (git branch --quiet 2>| awk '/fatal:/ {print "no git"}')
+
=== Make su launch fish ===
            printf '%s@%s %s%s%s (%s) $ ' (whoami) (hostname|cut -d . -f 1) (set_color $fish_color_cwd) (prompt_pwd) (set_color normal) (parse_git_branch)           
+
 
          else
+
If ''su'' starts with Bash (because Bash is the default shell), define a function in your fish configuration file:
            printf '%s@%s %s%s%s $ ' (whoami) (hostname|cut -d . -f 1) (set_color $fish_color_cwd) (prompt_pwd) (set_color normal)
+
function su
          end
+
    /bin/su --shell=/usr/bin/fish $argv
 
  end
 
  end
  
If '''su''' starts with Bash (because Bash is the default shell), define a function in Fish:
+
=== Start X at login ===
~> funced su
+
 
su> function su
+
Add the following to the bottom of your {{ic|~/.config/fish/config.fish}}.
         /bin/su --shell=/usr/bin/fish $argv
+
 
    end
+
{{bc|1=<nowiki>
~> funcsave su
+
# start X at login
 +
if status --is-login
 +
    if test -z "$DISPLAY" -a "$XDG_VTNR" -eq "1"
 +
         exec startx -- -keeptty
 +
    end
 +
end
 +
</nowiki>}}
 +
 
 +
=== Use liquidprompt ===
 +
 
 +
[https://github.com/nojhan/liquidprompt Liquidprompt] is a popular "full-featured & carefully designed adaptive prompt for Bash & Zsh" and has no plans to make it compatible with fish [https://github.com/nojhan/liquidprompt/pull/230]. [https://github.com/wesbarnett/fish-lp This project] implements it for fish.
 +
 
 +
{{Note|See [https://github.com/fish-shell/fish-shell/issues/1772 this issue] for reasons why {{ic|startx}} requires the {{ic|-keeptty}} flag when using 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 add the following to your {{ic|~/.config/fish/config.fish}}:
 +
{{bc|<nowiki>
 +
# fish git prompt
 +
set __fish_git_prompt_showdirtystate 'yes'
 +
set __fish_git_prompt_showstashstate 'yes'
 +
set __fish_git_prompt_showupstream 'yes'
 +
set __fish_git_prompt_color_branch yellow
 +
 
 +
# Status Chars
 +
set __fish_git_prompt_char_dirtystate '⚡'
 +
set __fish_git_prompt_char_stagedstate '→'
 +
set __fish_git_prompt_char_stashstate '↩'
 +
set __fish_git_prompt_char_upstream_ahead '↑'
 +
set __fish_git_prompt_char_upstream_behind '↓'
 +
 +
function fish_prompt
 +
        set last_status $status
 +
        set_color $fish_color_cwd
 +
        printf '%s' (prompt_pwd)
 +
        set_color normal
 +
        printf '%s ' (__fish_git_prompt)
 +
      set_color normal
 +
end
 +
</nowiki>}}
  
== Current state of Fish development ==
+
=== Evaluate ssh-agent ===
The original developer, [https://github.com/liljencrantz Axel Liljencrantz] has [http://www.mail-archive.com/fish-users@lists.sourceforge.net/msg02893.html abandoned the project]. The rest of his team slowly took over and transferred the codebase to [http://gitorious.org/fish-shell gitorius].
+
  
So far several bug fixes are available from the git repository. Also, there is an AUR package for the git master branch, which is considered stable for everyday use: {{AUR|fish-git}}.
+
In fish, {{ic|eval (ssh-agent)}} generate errors due to how variables are set. To work around this, use the csh-style option {{ic|-c}}:
  
On May 30, 2012 [http://ridiculousfish.com/shell/ ridiculous_fish] has [http://www.mailinglistarchive.com/html/fish-users@lists.sourceforge.net/2012-05/msg00004.html announced] a new fork of Fish which [http://www.mailinglistarchive.com/html/fish-users@lists.sourceforge.net/2012-05/msg00023.html has been adopted] as mainstream later, and development is now relocated to [https://github.com/fish-shell/fish-shell github]. The AUR package {{AUR|fish-shell-git}} follows the head branch of that, while the Fish package in the [community] repository provides latest stable milestones as announced on the webpage.
+
  $ eval (ssh-agent -c)
  
Upstream git head already contains full completions for archlinux-specific commands like pacman, pacman-key, makepkg, cower, pbget, pacmatic.
+
== See also ==
  
== External Links ==
+
* http://fishshell.com/ - Home page
*[http://ridiculousfish.com/shell/index.html Home Page]
+
* http://fishshell.com/docs/current/index.html - Documentation
*[http://ridiculousfish.com/shell/user_doc/html/index.html Documentation]
+

Latest revision as of 13:10, 18 August 2016

fish (the friendly interactive shell) is a user friendly command line shell intended mostly for interactive use.

Installation

Install the fish package. Alternatively install the fish-gitAUR package for the development version.

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 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.

Configuration

User configurations for fish are located at ~/.config/fish/config.fish. Adding commands or functions to the file will execute/define them when opening a terminal, similar to .bashrc.

Web interface

The fish prompt and terminal colors can be set with the interactive web interface:

fish_config

Selected settings are written to your personal configuration file. You can also view defined functions and your history.

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.

Troubleshooting

History substitution

Fish does not implement history substitution (e.g. sudo !!), and the fish developers have said that they do not plan to. Still, this is an essential piece of many users' workflow. Reddit user, crossroads1112, created a function that regains some of the functionality of history substitution and with another syntax. The function is on github and instructions are included as comments in it. There is a forked version that is closer to the original syntax and allows for command !! if you specify the command in the helper function.

Not setting fish as default shell

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.

Modify .bashrc to drop into fish

Keep your default shell as Bash and simply add the line fish && exit 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. When you exit fish you will seamlessly exit bash as well. Compared to the following options, this is the most universal solution, since it works both on a local machine and on an SSH server.

Tip: Use bash --norc to enter bash manually without dropping to fish after setting up as above.

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 LilyTerm and other light terminal emulators that do not support setting the shell it would look like this:

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.

Whenever you open your terminal emulator, you will be dropped into fish.

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.

Working with fish as default shell

If you decide to set fish as your default shell, you may find that you no longer have very much in your path. You can add a section to your ~/.config/fish/config.fish file that will set your path correctly on login. This is much like .profile or .bash_profile as it is only executed for login shells.

if status --is-login
        set PATH $PATH /usr/bin /sbin
end

Note that this route requires you to manually add various other environment variables, such as $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 to source /etc/profile and ~/.profile.

/etc/profile and ~/.profile compatibility

Since standard POSIX sh syntax is not compatible with fish, fish will not be able to source /etc/profile and thus all *.sh in /etc/profile.d) and ~/.profile. If you have fish as your default shell, you can work around this by doing the following:

Install dash and add this line to your config.fish:

env -i HOME=$HOME dash -l -c printenv | sed -e '/PWD/d; /PATH/s/:/ /g;s/=/ /;s/^/set -x /' | source

an alternative variant will save you one executable invocation by using a builtin command:

env -i HOME=$HOME dash -l -c 'export -p' | sed -e "/PWD/d; /PATH/s/'//g;/PATH/s/:/ /g;s/=/ /;s/^export/set -x/" | source

Also consider #Not setting fish as default shell.

Tips and tricks

Disable greeting

By default, fish prints a greeting message at startup. To disable it, add set fish_greeting to your fish configuration file.

Make su launch fish

If su starts with Bash (because Bash is the default shell), define a function in your fish configuration file:

function su
    /bin/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" -eq "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.

Note: See this issue for reasons why startx requires the -keeptty flag when using 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 add the following to your ~/.config/fish/config.fish:

# fish git prompt
set __fish_git_prompt_showdirtystate 'yes'
set __fish_git_prompt_showstashstate 'yes'
set __fish_git_prompt_showupstream 'yes'
set __fish_git_prompt_color_branch yellow

# Status Chars
set __fish_git_prompt_char_dirtystate '⚡'
set __fish_git_prompt_char_stagedstate '→'
set __fish_git_prompt_char_stashstate '↩'
set __fish_git_prompt_char_upstream_ahead '↑'
set __fish_git_prompt_char_upstream_behind '↓'
 
function fish_prompt
        set last_status $status
        set_color $fish_color_cwd
        printf '%s' (prompt_pwd)
        set_color normal
        printf '%s ' (__fish_git_prompt)
       set_color normal
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)

See also