「Fish」の版間の差分

提供: ArchWiki
ナビゲーションに移動 検索に移動
 
(4人の利用者による、間の23版が非表示)
1行目: 1行目:
 
{{lowercase title}}
 
{{lowercase title}}
[[Category:コマンドシェル]]
+
[[Category:コマンドラインシェル]]
 
[[de:Fish]]
 
[[de:Fish]]
 
[[en:Fish]]
 
[[en:Fish]]
 
[[ru:Fish]]
 
[[ru:Fish]]
  +
[[zh-hans:Fish]]
'''fish''' ('''friendly interactive shell''') は主に対話的に使用することを意図されたユーザフレンドリーなコマンドラインシェルです。
 
  +
{{Related articles start}}
  +
{{Related|コマンドラインシェル}}
  +
{{Related articles end}}
  +
'''fish''' ('''friendly interactive shell''') は主に対話的に使用することを意図されたユーザフレンドリーな [[コマンドラインシェル]] です。
   
  +
''fish'' は意図的に [[Wikipedia:POSIX|POSIX]] に準拠しておらず、簡略化された構文または異なる構文を使用して POSIX の矛盾に対処することを目的としています。これは、単純な POSIX 準拠のスクリプトであっても、fish で実行するにはかなりの適応や完全な書き換えが必要になる可能性があることを意味します。
 
== インストール ==
 
== インストール ==
   
[[公式リポジトリ]]より、[http://fishshell.com/ 主流フォーク] の安定版 {{Pkg|fish}} パッケージを[[pacman|インストール]]できます。
+
{{Pkg|fish}} パッケージを[[pacman|インストール]]してください。開発版は {{AUR|fish-git}} パッケージでインストールできます。
   
  +
インストールしたら {{ic|fish}} を実行することで fish シェルが起動します。
最新の開発版は [[AUR]] より {{AUR|fish-shell-git}}{{Broken package link|{{aur-mirror|fish-shell-git}}}} を[[pacman|インストール]]します。
 
   
  +
ドキュメントは fish から {{ic|help}} を実行することでウェブブラウザが開いて確認できます。fish の構文は他のシェルと異なっているので、最低でも "Syntax overview" セクションを読むことを推奨します。
2011年に [https://github.com/liljencrantz オリジナルの開発者] による [http://www.mail-archive.com/fish-users@lists.sourceforge.net/msg02893.html 開発が中止された] 従来のバージョンは [[AUR]] より {{AUR|fish-git}} を[[pacman|インストール]]します。
 
   
  +
== システムへの統合 ==
fish をデフォルトシェルにするには、{{ic|chsh -s /usr/bin/fish}} を実行します。
 
   
  +
fish をデフォルトのユーザーシェルとするのか、つまりログイン時に直接 fish に入るのか、それとも現在のデフォルトシェルの子プロセスとしてインタラクティブシェルモードで使用するのか、ここでは後者を [[Bash]] と仮定しています。この2つの設定について詳しく説明します。
== 入出力 ==
 
   
  +
* '''デフォルトシェル''' として使用される fish: このモードでは、fish の機能とそのスクリプト言語に関する基本的な理解が必要です。ユーザーの現在の初期化スクリプトと環境変数を新しい fish 環境に移行する必要があります。このモードでシステムを設定するには、[[fish#fish をデフォルトのシェルとして設定する|fish をデフォルトのシェルとして設定する]] に従ってください。
=== File descriptors ===
 
  +
* '''インタラクティブシェルオンリー''': このモードでは、Bash の初期化スクリプトは通常通り実行され、fish はターミナルに接続された対話型モードで Bash の上で実行されるため、混乱が少ないモードです。このモードで fish を設定するには、[[fish#fish をインタラクティブシェルとして設定する|fish をインタラクティブシェルとして設定する]] に従ってください。
Like other shells, fish lets you redirect input/output streams. This is useful when using text files to save programs output or errors, or when using text files as input. Most programs use three input/output streams, represented by numbers called file descriptors (FD). These are:
 
   
  +
=== fish をデフォルトのシェルとして設定する ===
* Standard input (FD 0), used for reading (keyboard by default).
 
* Standard output (FD 1), used for writing (screen by default).
 
* Standard error (FD 2), used for displaying errors and warnings (screen by default).
 
   
  +
fish をデフォルトのユーザーシェルとして設定することに決めた場合、最初のステップは、この特定のユーザーのシェルを {{ic|/usr/bin/fish}} に設定することです。これは、[[コマンドラインシェル#デフォルトシェルを変更する]] の説明に従って行うことができます。
=== リダイレクト ===
 
Any file descriptor can be directed to other files through a mechanism called redirection:
 
   
  +
次のステップは、様々な Bash の初期化スクリプト、すなわち {{ic|/etc/profile}}, {{ic|~/.bash_profile}}, {{ic|/etc/bash.bashrc}}, {{ic|~/.bashrc}} で行われる現在必要な動作と設定を、fish フレームワークに移植するものです。
{{bc|''Redirecting standard input:''
 
$ command < source_file
 
   
  +
特に、環境変数 {{ic|$PATH}} の内容は、一度 fish に直接ログインして確認し、自分の必要性に応じて調整することが必要です。fish では、{{ic|$PATH}} は ''グローバル環境変数'' として定義されています。すべての機能にわたって ''グローバル'' なスコープを持ち、再起動すると失われ、子プロセスにエクスポートされる ''環境変数'' となります。パスに場所を追加する推奨方法は、{{ic|config.fish}} から [https://fishshell.com/docs/current/cmds/fish_add_path.html fish_add_path] コマンドを呼び出す方法です。例えば
''Redirecting standard output:''
 
$ command > destination
 
   
  +
$ fish_add_path -p ''/first/path'' ''/second/path'' ''/third/one''
''Appending standard output to an existing file:''
 
$ command >> destination
 
   
  +
これら 3つの場所がパスの先頭に追加されます。
''Redirecting standard error:''
 
$ command ^ destination
 
   
  +
=== fish をインタラクティブシェルとして設定する ===
''Appending standard error to an existing file:''
 
$ command ^^ destination}}
 
   
  +
fish をシステム全体またはユーザーのデフォルトとして設定しないと、起動時に現在の Bash スクリプトを実行できます。これにより、現在のユーザーの環境変数が変更されず、Bash の子として実行される fish にエクスポートされます。以下は、fish をデフォルトのシェルとして設定せずにインタラクティブシェルモードで実行するいくつかの方法です。
You can use one of the following as {{ic|destination}}:
 
   
  +
==== .bashrc を変更して fish にドロップする ====
* A filename (the output will be written to the specified file).
 
* An {{ic|&}} followed by the number of another file descriptor. The output will be written to the other file descriptor.
 
* An {{ic|&}} followed by a {{ic|-}} sign. The output will not be written anywhere.
 
   
  +
デフォルトのシェルを Bash にして、{{ic|exec fish}} という行を {{ic|.bashrc}} などの適切な [[Bash#設定ファイル]] に追加するだけです。これにより、Bash は {{ic|/etc/profile}} と {{ic|/etc/profile.d}} にあるすべてのファイルを適切に source するようになります。fish は Bash のプロセスを置き換えるので、fish を終了すると端末も終了します。以下のオプションと比較すると、これはローカルマシンでも SSH サーバでも動作するため、最も普遍的な解決策と言えます。
Examples:
 
{{bc|''Redirecting standard output to a file:''
 
$ command > destination_file.txt
 
   
  +
{{Tip|
''Redirecting both standard output and standard error to the same file:''
 
  +
* このセットアップでは、{{ic|bash --norc}} を使用して、{{ic|~/.bashrc}} からコマンドを実行せずに手動で Bash に入り、{{ic|exec fish}} を実行して fish に戻ります。
$ command > destination_file.txt ^ &1
 
  +
* {{ic|bash -c 'echo test'}} のようなコマンドを fish の起動ではなく Bash で実行させるには、代わりに {{ic|if [ -z "$BASH_EXECUTION_STRING" ]; then exec fish; fi}} と記述します。
  +
* ログインシェルかどうかを fish に知らせるには、{{ic|~/.bashrc}} でログインシェルのステータスを検出し、{{ic|-login}} オプションを fish に渡します。fish のシェルコマンド {{ic|status}} を使えば、ステータスを表示することもできます。
  +
* 親プロセスが fish でない場合のみ、fish にドロップインします。これにより、{{ic|bash}} の設定を失うことなく、{{ic|~/.bashrc}} コマンドを実行することで bash に素早く入ることができます。
  +
{{bc|<nowiki>if [[ $(ps --no-header --pid=$PPID --format=comm) != "fish" && -z ${BASH_EXECUTION_STRING} ]]
  +
then
  +
shopt -q login_shell && LOGIN_OPTION='--login' || LOGIN_OPTION=''
  +
exec fish $LOGIN_OPTION
  +
fi</nowiki>}}
  +
}}
   
  +
==== ターミナルエミュレータでオプションを使用する ====
''Silencing standard output:''
 
$ command > &-}}
 
   
  +
もうひとつの方法は、fish を実行するコマンドラインオプションを指定してターミナルエミュレータを開くことです。ほとんどのターミナルでは、これは {{ic|-e}} スイッチなので、たとえば fish を使って gnome-terminal を開くには、ショートカットを変更してください。
=== パイプ ===
 
   
  +
gnome-terminal -e fish
You can redirect standard output of one command to standard input of the next command. This is done by separating the commands by the pipe character ({{ic|<nowiki>|</nowiki>}}). Example:
 
   
  +
{{aur|lilyterm-git}} など、シェルの設定をサポートしていない端末エミュレーターでは、次のようになります。
cat example.txt | head
 
   
  +
SHELL=/usr/bin/fish lilyterm
You can redirect other file descriptors to the pipe (besides standard output). The next example shows how to use standard error of one command as standard input of another command, prepending standard error file descriptor's number and {{ic|>}} to the pipe:
 
   
  +
また、ターミナルによっては、ターミナル設定またはターミナルプロファイルで fish をデフォルトのシェルとして設定できる場合があります。
$ command 2>| less
 
  +
  +
==== ターミナルマルチプレクサでオプションを使用する ====
  +
  +
[[tmux]] で開始されたシェルとして fish を設定するには、これを {{ic|~/.tmux.conf}} に入れます:
  +
  +
set-option -g default-shell "/usr/bin/fish"
   
  +
''tmux'' を実行するたびに、fish にドロップされます。
This will run {{ic|command}} and redirect its standard error to the {{ic|less}} command.
 
   
 
== 設定 ==
 
== 設定 ==
  +
fish のユーザー設定ファイルは {{ic|~/.config/fish/config.fish}} に存在しています。{{ic|.bashrc}} と同じように、ターミナルが開かれる時に実行/定義するコマンド、関数を追加します。
 
  +
設定ファイルはログインごとに実行され、{{ic|~/.config/fish/config.fish}} にあります。ファイルにコマンドまたは関数を追加すると、{{ic|.bashrc}} と同様に、ターミナルを開いたときにそれらが実行/定義されます。変数を保存する必要がある場合は、前述の設定ファイルで定義するのではなく、変数を ''ユニバーサル'' として設定する必要があることに注意してください。
  +
  +
ユーザーの関数は、ディレクトリ {{ic|~/.config/fish/functions}} のファイル名 {{ic|''function_name''.fish}} にあります。
   
 
=== ウェブインターフェイス ===
 
=== ウェブインターフェイス ===
81行目: 91行目:
   
 
選択した設定は、個人用の設定ファイルに書き込まれます。また、定義された関数と履歴を表示することができます。
 
選択した設定は、個人用の設定ファイルに書き込まれます。また、定義された関数と履歴を表示することができます。
 
=== プロンプト ===
 
 
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>}}
 
   
 
=== コマンド補完 ===
 
=== コマンド補完 ===
119行目: 102行目:
 
fish の開発元のポリシーが、上流の tarball に存在する補完を全て含めるというものなので ''pacman'', ''pacman-key'', ''makepkg'', ''cower'', ''pbget'', ''pacmatic'' などの Arch Linux 固有のコマンドの文脈に沿った補完も fish に含まれています。メモリ管理が賢いために、リソースに悪影響を与えることはありません。
 
fish の開発元のポリシーが、上流の tarball に存在する補完を全て含めるというものなので ''pacman'', ''pacman-key'', ''makepkg'', ''cower'', ''pbget'', ''pacmatic'' などの Arch Linux 固有のコマンドの文脈に沿った補完も fish に含まれています。メモリ管理が賢いために、リソースに悪影響を与えることはありません。
   
  +
==ヒントとテクニック‎==
=== /etc/profile と ~/.profile の互換性 ===
 
   
  +
=== コマンド置換 ===
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}}
 
   
  +
''fish'' は Bash スタイルの履歴置換 (例:{{ic|sudo !!}}) を実装していません。開発者は [https://fishshell.com/docs/current/faq.html#why-doesn-t-history-substitution-etc-work fish faq] で、代わりにインタラクティブな履歴呼び出しインターフェースを使用するよう推奨しています: {{ic|Up}} 矢印は過去の行全体を呼び出し、{{ic|Alt+Up}} は個々の引数を呼び出します。(または {{ic|Alt+.}}) は個々の引数を呼び出し、{{ic|Alt+s}} は既存の行の前に {{ic|sudo}} を追加します。
If you want fish to source those files, install {{Pkg|dash}} and add this line to your {{ic|config.fish}}:
 
env -i HOME=$HOME dash -l -c printenv | sed -e '/PATH/s/:/ /g;s/=/ /;s/^/set -x /' | source
 
   
  +
しかし、いくつかの回避策が [https://github.com/fish-shell/fish-shell/wiki/Bash-Style-Command-Substitution-and-Chaining-(!!-!$) fish wiki] に記述されています。完全な履歴置換はできませんが、いくつかの関数は {{ic|!!!}} を直前のコマンドに、{{ic|!$}} を直前の最終引数に置き換えます。
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 "/PATH/s/'//g;/PATH/s/:/ /g;s/=/ /;s/^export/set -x/" | source
 
   
  +
=== グリーティングを無効化 ===
==Tips and Tricks==
 
===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.
 
   
  +
デフォルトでは、fish は起動時にグリーティングメッセージを表示します。無効にするには、次のコマンドを 1 回実行します:
== トラブルシューティング ==
 
   
  +
$ set -U fish_greeting
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 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:
 
   
  +
これにより、すべての Fish インスタンスと共有され、シェルの再起動時に保存されるユニバーサル変数 {{ic|fish_greeting}} がクリアされます。
gnome-terminal -e fish
 
   
  +
=== su で fish を起動する ===
With LilyTerm and other light terminal emulators that do not support setting the shell it would look like this:
 
   
  +
''su'' が Bash で始まる場合、Bash がターゲットユーザー (ユーザー名が指定されていない場合は ''root'' ) のデフォルトシェルですが、ユーザーのシェルに関係なく fish にリダイレクトする関数を定義することもできます:
SHELL=/usr/bin/fish lilyterm
 
   
  +
{{hc|~/.config/fish/functions/su.fish|2=
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.
 
  +
function su
  +
command su --shell=/usr/bin/fish $argv
  +
end}}
   
  +
=== ログイン時に X を起動 ===
To set fish as the shell started in tmux, put this into your {{ic|~/.tmux.conf}}:
 
   
  +
以下を {{ic|~/.config/fish/config.fish}} の最後に追加してください:
set-option -g default-shell "/usr/bin/fish"
 
   
  +
{{bc|1=<nowiki>
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.
 
  +
# Start X at login
  +
if status --is-login
  +
if test -z "$DISPLAY" -a $XDG_VTNR = 1
  +
exec startx -- -keeptty
  +
end
  +
end
  +
</nowiki>}}
   
  +
=== プロンプトに git の状態を表示する ===
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.
 
   
  +
git ディレクトリにいるときに、fish にブランチやダーティの状態を表示させたい場合は、以下の {{ic|fish_prompt}} 関数を定義するとよいでしょう。
{{bc|
 
  +
if status --is-login
 
  +
{{hc|~/.config/fish/functions/fish_prompt.fish|
set PATH $PATH /usr/bin /sbin
 
  +
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
 
end
 
}}
 
}}
   
  +
しかし、これは現在では非推奨となっています。[https://github.com/fish-shell/fish-shell/blob/master/share/functions/__fish_git_prompt.fish fish-shell git] を参照してください。代わりに、[https://mariuszs.github.io/blog/2013/informative_git_prompt.html Informative Git Prompt] が fish に組み込まれ、必要であれば fish_config から起動できるようになりました。
Note that you will need 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.
 
   
=== su bash が起動する ===
+
=== SSH のプロンプトホスト名に色を付ける ===
   
  +
SSH 経由で接続するたびにプロンプ​​トでホスト名を動的に色付けするには、fish_prompt 関数または fish 設定ファイルのいずれかに次の行を追加します。ここでは赤色を使用しています。
su で bash が起動する場合 (Bash がデフォルトシェルのため)、fish に関数を定義してください:
 
   
  +
{{hc|~/.config/fish/functions/fish_prompt.fish|
$ funced su
 
  +
...
function su
 
  +
if set -q SSH_TTY
/bin/su --shell=/usr/bin/fish $argv
 
  +
set -g fish_color_host brred
  +
end
  +
...}}
  +
  +
=== ssh-agent の評価 ===
  +
  +
fish では適切な変数が設定されていないため {{ic|eval (ssh-agent)}} を実行するとエラーが生成されます。この問題を解決するには、csh 風のオプションである {{ic|-c}} を使ってください:
  +
  +
$ eval (ssh-agent -c)
  +
  +
=== "command not found" フック ===
  +
  +
Fish には、"command not found" フックがあり、認識されないコマンドを入力すると自動的に公式リポジトリを検索します。このフックは {{Pkg|pkgfile}} を使用して実行され、インストールされていない場合は{{ic|pacman-F}} にフォールバックされます。
  +
  +
3.2.2 以降では、 [https://github.com/fish-shell/fish-shell/issues/7841 パフォーマンスが落ちる] ため、"command not found" はデフォルトで {{ic|pacman-F}} にフォールバックしません。
  +
  +
この動作によって生じる遅延が望ましくない場合は、エラーメッセージのみを出力するように {{ic|fish_command_not_found}} を再定義することでこのフックをオーバーライドできます。
  +
  +
$ function fish_command_not_found
  +
__fish_default_command_not_found_handler $argv[1]
  +
end
  +
  +
この変更を永続的にするには、{{ic|funcsave}} ビルトインを使用します:
  +
  +
$ funcsave fish_command_not_found
  +
  +
=== ジョブリストからプロセスを削除する ===
  +
  +
fish を終了するとバックグラウンドで実行されているジョブは全て ''fish'' によって終了されます。fish の終了後もジョブを実行し続けるには {{ic|disown}} を使ってください。例えば、以下のコマンドは {{ic|firefox}} をバックグラウンドで起動してからプロセスとの関係を断ち切ります:
  +
  +
$ firefox &
  +
$ disown
  +
  +
fish のプロセスが終了しても firefox が閉じられることはありません。詳しくは ''fish'' で {{man|1|disown|url=}} の [[man ページ]]を見てください。
  +
  +
=== 一時的なエイリアスを永続化する ===
  +
  +
fish シェルを開いて以下のようにコマンドを実行することで簡単に永続的なエイリアスを作成することができます。
  +
$ alias lsl "ls -l"
  +
$ funcsave lsl
  +
fish バージョン3.0 以降では、エイリアス --save(-s) オプションをサポートしています。
  +
$ alias -s lsl "ls-l"
  +
  +
これは関数を作成します。
  +
function lsl
  +
ls -l $argv
 
end
 
end
  +
fish のシェル関数としてエイリアスが設定されます。シェル関数を確認・編集したい場合、{{ic|fish_config}} を使って作成されるウェブ設定ページの '''Function''' ページから全ての関数を閲覧できます。
$ funcsave su
 
   
  +
詳細については、 [https://fishshell.com/docs/current/cmds/alias.html alias - create a function — fish-shell]
=== ログイン時に X を起動 ===
 
   
  +
=== ログイン時のソース /etc/profile ===
{{ic|~/.config/fish/config.fish}} の一番下に以下を追加します:
 
   
  +
{{ic|bash}} とは異なり、{{ic|fish}} は tty ログインで {{ic|/etc/profile}} をソースにしません。このファイルのソースを
{{bc|1=<nowiki>
 
  +
{{ic|/etc/profile.d}} ディレクトリに環境変数が追加され、宣言されている場合は、config に以下を追加します:
# start X at login
 
  +
if status --is-login
 
  +
{{hc|~/.config/fish/config.fish|
if test -z "$DISPLAY" -a $XDG_VTNR -eq 1
 
  +
# source /etc/profile with bash
exec startx -- -keeptty
 
  +
if status is-login
end
 
  +
exec bash -c "test -e /etc/profile && source /etc/profile;\
  +
exec fish"
 
end
 
end
  +
}}
</nowiki>}}
 
   
  +
{{ic|bash}} ログインセッションのように、ログインシェルとして {{ic|fish}} を実行しながら、通常使用するすべての環境変数を使用できます。
{{Note|fish 使用時に {{ic|startx}} が {{ic|-keeptty}} フラグを必要とする理由については [https://github.com/fish-shell/fish-shell/issues/1772 この記事] を参照してください。}}
 
   
 
== 参照 ==
 
== 参照 ==
   
 
* http://fishshell.com/ - ホームページ
 
* http://fishshell.com/ - ホームページ
* http://fishshell.com/docs/2.1/index.html - ドキュメント
+
* http://fishshell.com/docs/current/index.html - ドキュメント
  +
* http://hyperpolyglot.org/unix-shells - シェル文法の対応表
  +
* https://github.com/fish-shell/fish-shell - fish on GitHub

2024年1月4日 (木) 17:09時点における最新版

関連記事

fish (friendly interactive shell) は主に対話的に使用することを意図されたユーザフレンドリーな コマンドラインシェル です。

fish は意図的に POSIX に準拠しておらず、簡略化された構文または異なる構文を使用して POSIX の矛盾に対処することを目的としています。これは、単純な POSIX 準拠のスクリプトであっても、fish で実行するにはかなりの適応や完全な書き換えが必要になる可能性があることを意味します。

インストール

fish パッケージをインストールしてください。開発版は fish-gitAUR パッケージでインストールできます。

インストールしたら fish を実行することで fish シェルが起動します。

ドキュメントは fish から help を実行することでウェブブラウザが開いて確認できます。fish の構文は他のシェルと異なっているので、最低でも "Syntax overview" セクションを読むことを推奨します。

システムへの統合

fish をデフォルトのユーザーシェルとするのか、つまりログイン時に直接 fish に入るのか、それとも現在のデフォルトシェルの子プロセスとしてインタラクティブシェルモードで使用するのか、ここでは後者を Bash と仮定しています。この2つの設定について詳しく説明します。

  • デフォルトシェル として使用される fish: このモードでは、fish の機能とそのスクリプト言語に関する基本的な理解が必要です。ユーザーの現在の初期化スクリプトと環境変数を新しい fish 環境に移行する必要があります。このモードでシステムを設定するには、fish をデフォルトのシェルとして設定する に従ってください。
  • インタラクティブシェルオンリー: このモードでは、Bash の初期化スクリプトは通常通り実行され、fish はターミナルに接続された対話型モードで Bash の上で実行されるため、混乱が少ないモードです。このモードで fish を設定するには、fish をインタラクティブシェルとして設定する に従ってください。

fish をデフォルトのシェルとして設定する

fish をデフォルトのユーザーシェルとして設定することに決めた場合、最初のステップは、この特定のユーザーのシェルを /usr/bin/fish に設定することです。これは、コマンドラインシェル#デフォルトシェルを変更する の説明に従って行うことができます。

次のステップは、様々な Bash の初期化スクリプト、すなわち /etc/profile, ~/.bash_profile, /etc/bash.bashrc, ~/.bashrc で行われる現在必要な動作と設定を、fish フレームワークに移植するものです。

特に、環境変数 $PATH の内容は、一度 fish に直接ログインして確認し、自分の必要性に応じて調整することが必要です。fish では、$PATHグローバル環境変数 として定義されています。すべての機能にわたって グローバル なスコープを持ち、再起動すると失われ、子プロセスにエクスポートされる 環境変数 となります。パスに場所を追加する推奨方法は、config.fish から fish_add_path コマンドを呼び出す方法です。例えば

$ fish_add_path -p /first/path /second/path /third/one

これら 3つの場所がパスの先頭に追加されます。

fish をインタラクティブシェルとして設定する

fish をシステム全体またはユーザーのデフォルトとして設定しないと、起動時に現在の Bash スクリプトを実行できます。これにより、現在のユーザーの環境変数が変更されず、Bash の子として実行される fish にエクスポートされます。以下は、fish をデフォルトのシェルとして設定せずにインタラクティブシェルモードで実行するいくつかの方法です。

.bashrc を変更して fish にドロップする

デフォルトのシェルを Bash にして、exec fish という行を .bashrc などの適切な Bash#設定ファイル に追加するだけです。これにより、Bash は /etc/profile/etc/profile.d にあるすべてのファイルを適切に source するようになります。fish は Bash のプロセスを置き換えるので、fish を終了すると端末も終了します。以下のオプションと比較すると、これはローカルマシンでも SSH サーバでも動作するため、最も普遍的な解決策と言えます。

ヒント:
  • このセットアップでは、bash --norc を使用して、~/.bashrc からコマンドを実行せずに手動で Bash に入り、exec fish を実行して fish に戻ります。
  • bash -c 'echo test' のようなコマンドを fish の起動ではなく Bash で実行させるには、代わりに if [ -z "$BASH_EXECUTION_STRING" ]; then exec fish; fi と記述します。
  • ログインシェルかどうかを fish に知らせるには、~/.bashrc でログインシェルのステータスを検出し、-login オプションを fish に渡します。fish のシェルコマンド status を使えば、ステータスを表示することもできます。
  • 親プロセスが fish でない場合のみ、fish にドロップインします。これにより、bash の設定を失うことなく、~/.bashrc コマンドを実行することで bash に素早く入ることができます。
if [[ $(ps --no-header --pid=$PPID --format=comm) != "fish" && -z ${BASH_EXECUTION_STRING} ]]
then
	shopt -q login_shell && LOGIN_OPTION='--login' || LOGIN_OPTION=''
	exec fish $LOGIN_OPTION
fi

ターミナルエミュレータでオプションを使用する

もうひとつの方法は、fish を実行するコマンドラインオプションを指定してターミナルエミュレータを開くことです。ほとんどのターミナルでは、これは -e スイッチなので、たとえば fish を使って gnome-terminal を開くには、ショートカットを変更してください。

gnome-terminal -e fish

lilyterm-gitAUR など、シェルの設定をサポートしていない端末エミュレーターでは、次のようになります。

SHELL=/usr/bin/fish lilyterm

また、ターミナルによっては、ターミナル設定またはターミナルプロファイルで fish をデフォルトのシェルとして設定できる場合があります。

ターミナルマルチプレクサでオプションを使用する

tmux で開始されたシェルとして fish を設定するには、これを ~/.tmux.conf に入れます:

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

tmux を実行するたびに、fish にドロップされます。

設定

設定ファイルはログインごとに実行され、~/.config/fish/config.fish にあります。ファイルにコマンドまたは関数を追加すると、.bashrc と同様に、ターミナルを開いたときにそれらが実行/定義されます。変数を保存する必要がある場合は、前述の設定ファイルで定義するのではなく、変数を ユニバーサル として設定する必要があることに注意してください。

ユーザーの関数は、ディレクトリ ~/.config/fish/functions のファイル名 function_name.fish にあります。

ウェブインターフェイス

fish のプロンプトとターミナルの色はウェブインターフェイスで対話的に設定できます:

fish_config

選択した設定は、個人用の設定ファイルに書き込まれます。また、定義された関数と履歴を表示することができます。

コマンド補完

fish は man ページから自動補完を生成できます。~/.config/fish/generated_completions/ に補完が書き込まれており、以下を呼び出すことによって生成することができます:

fish_update_completions

また、独自の補完を ~/.config/fish/completions/ に定義できます。/usr/share/fish/completions/ でいくつかの例を見ることができます。

fish の開発元のポリシーが、上流の tarball に存在する補完を全て含めるというものなので pacman, pacman-key, makepkg, cower, pbget, pacmatic などの Arch Linux 固有のコマンドの文脈に沿った補完も fish に含まれています。メモリ管理が賢いために、リソースに悪影響を与えることはありません。

ヒントとテクニック‎

コマンド置換

fish は Bash スタイルの履歴置換 (例:sudo !!) を実装していません。開発者は fish faq で、代わりにインタラクティブな履歴呼び出しインターフェースを使用するよう推奨しています: Up 矢印は過去の行全体を呼び出し、Alt+Up は個々の引数を呼び出します。(または Alt+.) は個々の引数を呼び出し、Alt+s は既存の行の前に sudo を追加します。

しかし、いくつかの回避策が fish wiki に記述されています。完全な履歴置換はできませんが、いくつかの関数は !!! を直前のコマンドに、!$ を直前の最終引数に置き換えます。

グリーティングを無効化

デフォルトでは、fish は起動時にグリーティングメッセージを表示します。無効にするには、次のコマンドを 1 回実行します:

$ set -U fish_greeting

これにより、すべての Fish インスタンスと共有され、シェルの再起動時に保存されるユニバーサル変数 fish_greeting がクリアされます。

su で fish を起動する

su が Bash で始まる場合、Bash がターゲットユーザー (ユーザー名が指定されていない場合は root ) のデフォルトシェルですが、ユーザーのシェルに関係なく fish にリダイレクトする関数を定義することもできます:

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

ログイン時に X を起動

以下を ~/.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

プロンプトに git の状態を表示する

git ディレクトリにいるときに、fish にブランチやダーティの状態を表示させたい場合は、以下の fish_prompt 関数を定義するとよいでしょう。

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

しかし、これは現在では非推奨となっています。fish-shell git を参照してください。代わりに、Informative Git Prompt が fish に組み込まれ、必要であれば fish_config から起動できるようになりました。

SSH のプロンプトでホスト名に色を付ける

SSH 経由で接続するたびにプロンプ​​トでホスト名を動的に色付けするには、fish_prompt 関数または fish 設定ファイルのいずれかに次の行を追加します。ここでは赤色を使用しています。

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

ssh-agent の評価

fish では適切な変数が設定されていないため eval (ssh-agent) を実行するとエラーが生成されます。この問題を解決するには、csh 風のオプションである -c を使ってください:

$ eval (ssh-agent -c)

"command not found" フック

Fish には、"command not found" フックがあり、認識されないコマンドを入力すると自動的に公式リポジトリを検索します。このフックは pkgfile を使用して実行され、インストールされていない場合はpacman-F にフォールバックされます。

3.2.2 以降では、 パフォーマンスが落ちる ため、"command not found" はデフォルトで pacman-F にフォールバックしません。

この動作によって生じる遅延が望ましくない場合は、エラーメッセージのみを出力するように fish_command_not_found を再定義することでこのフックをオーバーライドできます。

$ function fish_command_not_found
      __fish_default_command_not_found_handler $argv[1]
  end

この変更を永続的にするには、funcsave ビルトインを使用します:

$ funcsave fish_command_not_found

ジョブリストからプロセスを削除する

fish を終了するとバックグラウンドで実行されているジョブは全て fish によって終了されます。fish の終了後もジョブを実行し続けるには disown を使ってください。例えば、以下のコマンドは firefox をバックグラウンドで起動してからプロセスとの関係を断ち切ります:

$ firefox &
$ disown

fish のプロセスが終了しても firefox が閉じられることはありません。詳しくは fishdisown(1)man ページを見てください。

一時的なエイリアスを永続化する

fish シェルを開いて以下のようにコマンドを実行することで簡単に永続的なエイリアスを作成することができます。

$ alias lsl "ls -l"
$ funcsave lsl

fish バージョン3.0 以降では、エイリアス --save(-s) オプションをサポートしています。

$ alias -s lsl "ls-l"

これは関数を作成します。

function lsl
    ls -l $argv
end

fish のシェル関数としてエイリアスが設定されます。シェル関数を確認・編集したい場合、fish_config を使って作成されるウェブ設定ページの Function ページから全ての関数を閲覧できます。

詳細については、 alias - create a function — fish-shell

ログイン時のソース /etc/profile

bash とは異なり、fish は tty ログインで /etc/profile をソースにしません。このファイルのソースを /etc/profile.d ディレクトリに環境変数が追加され、宣言されている場合は、config に以下を追加します:

~/.config/fish/config.fish
# source /etc/profile with bash
if status is-login
    exec bash -c "test -e /etc/profile && source /etc/profile;\
    exec fish"
end

bash ログインセッションのように、ログインシェルとして fish を実行しながら、通常使用するすべての環境変数を使用できます。

参照