「Fish」の版間の差分

提供: ArchWiki
ナビゲーションに移動 検索に移動
(文字列「http://www.reddit.com/」を「https://www.reddit.com/」に置換)
(3人の利用者による、間の10版が非表示)
1行目: 1行目:
 
{{lowercase title}}
 
{{lowercase title}}
[[Category:Command shells]]
+
[[Category:コマンドシェル]]
 
[[de:Fish]]
 
[[de:Fish]]
 
[[en:Fish]]
 
[[en:Fish]]
 
[[ru:Fish]]
 
[[ru:Fish]]
  +
[[zh-hans:Fish]]
'''fish''' ('''friendly interactive shell''')は主に対話的に使用することを意図されたユーザフレンドリーなコマンドラインシェルです。
 
  +
'''fish''' ('''friendly interactive shell''') は主に対話的に使用することを意図されたユーザフレンドリーな[[コマンドラインシェル]]です。
   
 
== インストール ==
 
== インストール ==
   
[[公式リポジトリ]]より、[http://fishshell.com/ 主流fork]の安定版{{Pkg|fish}}パッケージを[[pacman|インストール]]できます。
+
{{Pkg|fish}} パッケージを[[pacman|インストール]]してください。開発版は {{AUR|fish-git}} パッケージでインストールできます。
   
  +
fish をデフォルトシェルにしたい場合は、[[シェル#デフォルトシェルを変更する]]を見てください。ただし、[[#fish をデフォルトシェルに設定しない|fish はデフォルトシェルにしない]]ことを推奨します。
最新の開発版は[[AUR]]より {{AUR|fish-shell-git}}{{Broken package link|{{aur-mirror|fish-shell-git}}}}を[[pacman|インストール]]します。
 
   
  +
インストールしたら {{ic|fish}} を実行することで fish シェルが起動します。
2011年に[https://github.com/liljencrantz オリジナルの開発者]によって[http://www.mail-archive.com/fish-users@lists.sourceforge.net/msg02893.html 中止された]従来のバージョンは[[AUR]]より
 
{{AUR|fish-git}}を[[pacman|インストール]]します。
 
   
  +
ドキュメントは fish から {{ic|help}} を実行することでウェブブラウザが開いて確認できます。fish の構文は他のシェルと異なっているので、最低でも "Syntax overview" セクションを読むことを推奨します。
fishをデフォルトシェルにするには、{{ic|chsh -s /usr/bin/fish}}を実行します。
 
   
 
== 入出力 ==
 
== 入出力 ==
   
=== File descriptors ===
+
=== ファイル記述子 ===
  +
他のシェルと同じように、入出力ストリームをリダイレクトすることが fish でも出来ます。テキストファイルを使ってプログラムのエラー出力を保存したり、テキストファイルを入力したりする場合に有用です。ほとんどのプログラムは3つの入出力ストリームを使います。ファイル記述子 (FD) と呼ばれる番号で表されます:
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:
 
   
  +
* 標準入力 (FD 0) は読み取りに使われます (デフォルトではキーボード)。
* Standard input (FD 0), used for reading (keyboard by default).
 
  +
* 標準出力 (FD 1) は書き込みに使われます (デフォルトではスクリーン)。
* Standard output (FD 1), used for writing (screen by default).
 
  +
* 標準エラー (FD 2) はエラーや警告の表示に使われます (デフォルトではスクリーン)。
* Standard error (FD 2), used for displaying errors and warnings (screen by default).
 
   
 
=== リダイレクト ===
 
=== リダイレクト ===
  +
リダイレクトという仕組みを使うことでファイル記述子の出力先を他のファイルに変えることができます:
Any file descriptor can be directed to other files through a mechanism called redirection:
 
   
{{bc|''Redirecting standard input:''
+
{{bc|''標準入力をリダイレクト:''
 
$ command < source_file
 
$ command < source_file
   
  +
''標準出力をリダイレクト:''
''Redirecting standard output:''
 
 
$ command > destination
 
$ command > destination
   
  +
''既存のファイルに標準出力を追記:''
''Appending standard output to an existing file:''
 
 
$ command >> destination
 
$ command >> destination
   
  +
''標準エラーをリダイレクト:''
''Redirecting standard error:''
 
 
$ command ^ destination
 
$ command ^ destination
   
  +
''既存のファイルに標準エラーを追記:''
''Appending standard error to an existing file:''
 
 
$ command ^^ destination}}
 
$ command ^^ destination}}
   
  +
{{ic|destination}} には以下のどれかを使えます:
You can use one of the following as {{ic|destination}}:
 
   
  +
* ファイル名 (出力は指定されたファイルに書き込まれます)。
* A filename (the output will be written to the specified file).
 
  +
* {{ic|&}} と他のファイル記述子の番号。出力は他のファイル記述子に書き込まれます。
* 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.
+
* {{ic|&}} {{ic|-}} 記号。出力はどこにも書き込まれません。
   
  +
例:
Examples:
 
  +
{{bc|''標準出力をファイルにリダイレクト:''
{{bc|''Redirecting standard output to a file:''
 
 
$ command > destination_file.txt
 
$ command > destination_file.txt
   
  +
''標準出力と標準出力の両方を同じファイルにリダイレクト:''
''Redirecting both standard output and standard error to the same file:''
 
 
$ command > destination_file.txt ^ &1
 
$ command > destination_file.txt ^ &1
   
  +
''標準出力を消す:''
''Silencing standard output:''
 
 
$ command > &-}}
 
$ command > &-}}
   
 
=== パイプ ===
 
=== パイプ ===
   
  +
あるコマンドの標準出力を次のコマンドの標準入力にリダイレクトすることができます。パイプ文字 ({{ic|<nowiki>|</nowiki>}}) を使ってコマンドを区切って下さい。例:
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:
 
   
 
cat example.txt | head
 
cat example.txt | head
   
  +
(標準出力だけでなく) 他のファイル記述子をパイプにリダイレクトすることもできます。次の例では、標準エラーのファイル記述子の番号と {{ic|>}} をパイプに付けることで、あるコマンドの標準エラーを他のコマンドの標準入力に流し込んでいます:
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:
 
   
 
$ command 2>| less
 
$ command 2>| less
   
This will run {{ic|command}} and redirect its standard error to the {{ic|less}} command.
+
上記を実行すると {{ic|command}} が実行されてから標準エラーが {{ic|less}} コマンドにリダイレクトされます。
   
== Configuration ==
+
== 設定 ==
  +
fish のユーザー設定ファイルは {{ic|~/.config/fish/config.fish}} に存在しています。{{ic|.bashrc}} と同じように、ターミナルが開かれる時に実行/定義するコマンド、関数を追加します。
   
  +
=== ウェブインターフェイス ===
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}}.
 
   
  +
fish のプロンプトとターミナルの色はウェブインターフェイスで対話的に設定できます:
=== Web interface ===
 
 
The fish prompt and terminal colors can be set with the interactive web interface:
 
   
 
fish_config
 
fish_config
   
  +
選択した設定は、個人用の設定ファイルに書き込まれます。また、定義された関数と履歴を表示することができます。
Selected settings are written to your personal configuration file. You can also view defined functions and your history.
 
 
=== プロンプト ===
 
 
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>}}
 
   
 
=== コマンド補完 ===
 
=== コマンド補完 ===
   
fishはmanページより自動補完を生成できます。{{ic|~/.config/fish/generated_completions/}} に補完が書き込まれており、以下を呼び出すことによって生成することができます
+
fish man ページから自動補完を生成できます。{{ic|~/.config/fish/generated_completions/}} に補完が書き込まれており、以下を呼び出すことによって生成することができます:
   
 
fish_update_completions
 
fish_update_completions
   
また、独自の補完を {{ic|~/.config/fish/completions/}}に定義できます。{{ic|/usr/share/fish/completions/}}でいくつかの例を見ることができ
+
また、独自の補完を {{ic|~/.config/fish/completions/}} に定義できます。{{ic|/usr/share/fish/completions/}} でいくつかの例を見ることができます
   
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 and ~/.profile compatibility ===
 
   
  +
=== fish をデフォルトシェルに設定しない ===
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}}
 
   
  +
Arch ではシェルスクリプトは [[Bash]] を使って書かれており、fish と完全な互換性がありません。fish をデフォルトシェルに設定しないことで、起動時に実行される Bash スクリプトで環境変数を正しく設定できます。また、fish などの Bash 非互換のターミナルを使った場合に起こる問題を避けられます。デフォルトシェルを fish に設定するとスクリプトエラーが発生します。以下では fish をデフォルトシェルに設定しないで使用する方法を説明しています。
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
 
   
  +
==== .bashrc から fish を起動する ====
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
 
   
  +
デフォルトシェルを Bash のままにしておいて {{ic|exec fish}} という行を[[Bash#設定ファイル|シェルの設定ファイル]] (例: {{ic|.bashrc}}) に追加してください。これで Bash が {{ic|/etc/profile}} や {{ic|/etc/profile.d}} のファイルを正しく読み込んでくれます。''fish'' は bash のプロセスを置き換えるため、''fish'' を終了するとターミナルも終了します。ローカルマシンだけでなく SSH サーバーでも使える方法のため、以下で説明する方法よりも汎用的です。
==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.
 
   
  +
{{Tip|上記のように設定する場合、{{ic|bash --norc}} を使うことで {{ic|~/.bashrc}} に書かれているコマンドを実行することなく Bash を開くことができます。普通に Bash を起動すると即座に Fish に戻ってしまいます。}}
== トラブルシューティング ==
 
   
  +
==== ターミナルエミュレータのオプションを使用する ====
{{Accuracy|Changing the default shell should not interfere with the execution of shell scripts, so long as they begin with a proper [https://en.wikipedia.org/wiki/Shebang_%28Unix%29 shebang line].}}
 
   
  +
fish を起動するコマンドラインオプションを使用してターミナルエミュレータを開くという方法もあります。大抵のターミナルでは {{ic|-e}} スイッチを使います。例えば fish を使用して gnome-terminal を開くには、ショートカットを以下のように変更してください:
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:
 
   
 
gnome-terminal -e fish
 
gnome-terminal -e fish
   
  +
シェルの設定をサポートしていない LilyTerm などの軽量ターミナルエミュレータでは以下のように設定してください:
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
   
  +
ターミナルの設定からターミナルのデフォルトシェルを fish に設定できたり、ターミナルエミュレータにプロファイル機能がある場合、ターミナルプロファイルで設定できることがあります。
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.
 
   
  +
どちらにしてもターミナルエミュレータを開いたら、fish が起動するようになります。
To set fish as the shell started in tmux, put this into your {{ic|~/.tmux.conf}}:
 
  +
  +
==== ターミナルマルチプレクサのオプションを使用する ====
  +
  +
[[tmux]] で起動するシェルを fish に設定するには、以下を {{ic|~/.tmux.conf}} に記述してください:
   
 
set-option -g default-shell "/usr/bin/fish"
 
set-option -g default-shell "/usr/bin/fish"
   
  +
''tmux'' を起動すると 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.
 
  +
  +
=== fish をデフォルトシェルに設定する ===
   
  +
fish をデフォルトシェルとして設定した場合、パスが正しく設定されていないことに気づくでしょう。{{ic|~/.config/fish/config.fish}} ファイルにセクションを追加してログイン時にパスを適切に設定することができます。ログインシェルの場合だけ実行される {{ic|.profile}} や {{ic|.bash_profile}} と同じです。
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.
 
   
 
{{bc|
 
{{bc|
164行目: 137行目:
 
}}
 
}}
   
  +
{{Note|{{ic|$MOZ_PLUGIN_PATH}} など、他の環境変数も手動で追加する必要があります。fish をデフォルトシェルとしてシームレスに使えるようにするには骨が折れる作業量になります。[[#fish をデフォルトシェルに設定しない|fish をデフォルトシェルに設定しない]]方が簡単です。}}
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が起動 ===
 
   
  +
デフォルトでは、fish は起動時にグリーティングメッセージを表示します。無効にするには、fish の設定ファイルに {{ic|set fish_greeting}} と追加してください。
もし、suでbashが起動する場合(Bashがデフォルトシェルのため), fishに関数を定義します。
 
  +
  +
=== su で fish を起動する ===
  +
  +
''su'' で Bash が起動する場合、fish の設定ファイルに以下の関数を定義します:
   
$ funced su
 
 
function su
 
function su
/bin/su --shell=/usr/bin/fish $argv
+
/bin/su --shell=/usr/bin/fish $argv
 
end
 
end
$ funcsave su
 
   
=== ログイン時にXを起動 ===
+
=== ログイン時に X を起動 ===
   
{{ic|~/.config/fish/config.fish}}の一番下以下を追加します。
+
以下を {{ic|~/.config/fish/config.fish}} 最後に追加してください:
   
 
{{bc|1=<nowiki>
 
{{bc|1=<nowiki>
# start X at login
+
# Start X at login
 
if status --is-login
 
if status --is-login
if test -z "$DISPLAY" -a $XDG_VTNR -eq 1
+
if test -z "$DISPLAY" -a $XDG_VTNR = 1
 
exec startx -- -keeptty
 
exec startx -- -keeptty
 
end
 
end
189行目: 164行目:
 
</nowiki>}}
 
</nowiki>}}
   
  +
=== liquidprompt を使う ===
{{Note|fish使用時に{{ic|startx}}が{{ic|-keeptty}}フラグを必要とする理由については[https://github.com/fish-shell/fish-shell/issues/1772 この記事]を参照してください。}}
 
  +
  +
[https://github.com/nojhan/liquidprompt Liquidprompt] は Bash と Zsh 用に作られたフル機能の人気のあるアダプティブプロンプトですが fish に対応する予定はありません [https://github.com/nojhan/liquidprompt/pull/230]。[https://github.com/wesbarnett/fish-lp fish-lp プロジェクト] は fish で Liquidprompt を実装しています。
  +
  +
=== プロンプトに git の状態を表示する ===
  +
  +
カレントディレクトリが git ディレクトリの場合に fish でブランチなどの状態を表示するには、以下を {{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>}}
  +
  +
=== ssh-agent の評価 ===
  +
  +
fish では適切な変数が設定されていないため {{ic|eval (ssh-agent)}} を実行するとエラーが生成されます。この問題を解決するには、csh 風のオプションである {{ic|-c}} を使ってください:
  +
  +
$ eval (ssh-agent -c)
  +
  +
=== "command not found" フック ===
  +
  +
[[pkgfile]] には "command not found" フックが含まれており、認識できないコマンドを入力したときに自動的に公式リポジトリが検索されます。[[pkgfile]] をインストールすると自動的にフックが実行されるようになっています。
  +
  +
=== ジョブリストからプロセスを削除する ===
  +
  +
fish を終了するとバックグラウンドで実行されているジョブは全て ''fish'' によって終了されます。fish の終了後もジョブを実行し続けるには {{ic|disown}} を使ってください。例えば、以下のコマンドは {{ic|firefox}} をバックグラウンドで起動してからプロセスとの関係を断ち切ります:
  +
  +
$ firefox &
  +
$ disown
  +
  +
fish のプロセスが終了しても firefox が閉じられることはありません。詳しくは ''fish'' で {{man|1|disown|url=}} の [[man ページ]]を見てください。
  +
  +
=== 一時的なエイリアスを永続化する ===
  +
  +
fish シェルを開いて以下のようにコマンドを実行することで簡単に永続的なエイリアスを作成することができます:
  +
{{bc|1=<nowiki>
  +
$ alias FooAliasName "foo"
  +
$ funcsave FooAliasName
  +
</nowiki>}}
  +
fish のシェル関数としてエイリアスが設定されます。シェル関数を確認・編集したい場合、{{ic|fish_config}} を使って作成されるウェブ設定ページの '''Function''' ページから全ての関数を閲覧できます。
  +
  +
== トラブルシューティング ==
  +
  +
===履歴置換===
  +
Fish は履歴置換 (例: {{ic|sudo !!}}) を実装しておらず、fish の開発者は [http://fishshell.com/docs/current/faq.html#faq-history 実装する予定もない] と発言しています。しかしながら、履歴置換は多くのユーザーにとって必要不可欠な機能です。Reddit ユーザーの [https://www.reddit.com/u/crossroads1112 crossroads1112] が履歴置換の機能を再現した関数を作成しています (ただし構文は異なっています)。関数は [https://gist.github.com/crossroads1112/77badb2c3455e23b873b github] にあり、使い方はコメントとして書いてあります。オリジナルの構文に近づけた [https://gist.github.com/b-/981892a65837ab0a387e フォークバージョン] も存在し、ヘルパー関数でコマンドを指定すれば {{ic|command !!}} が使えます。
  +
  +
{{ic|command !!}} を使用できるようにする方法は [https://github.com/fish-shell/fish-shell/wiki/Bash-Style-History-Substitution-%28%21%21-and-%21%24%29 Fish の github wiki] にもあります。{{ic|bind_bang}} 関数で {{ic|!!}} をコマンド履歴の最後のコマンドに展開する例が載っています。[https://github.com/fish-shell/fish-shell/issues/288#issuecomment-158704275 こちらの github issue] も参照してください。
   
 
== 参照 ==
 
== 参照 ==
   
 
* 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 - シェル文法の対応表

2018年2月6日 (火) 23:08時点における版

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

インストール

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

fish をデフォルトシェルにしたい場合は、シェル#デフォルトシェルを変更するを見てください。ただし、fish はデフォルトシェルにしないことを推奨します。

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

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

入出力

ファイル記述子

他のシェルと同じように、入出力ストリームをリダイレクトすることが fish でも出来ます。テキストファイルを使ってプログラムのエラー出力を保存したり、テキストファイルを入力したりする場合に有用です。ほとんどのプログラムは3つの入出力ストリームを使います。ファイル記述子 (FD) と呼ばれる番号で表されます:

  • 標準入力 (FD 0) は読み取りに使われます (デフォルトではキーボード)。
  • 標準出力 (FD 1) は書き込みに使われます (デフォルトではスクリーン)。
  • 標準エラー (FD 2) はエラーや警告の表示に使われます (デフォルトではスクリーン)。

リダイレクト

リダイレクトという仕組みを使うことでファイル記述子の出力先を他のファイルに変えることができます:

標準入力をリダイレクト:
$ command < source_file

標準出力をリダイレクト:
$ command > destination

既存のファイルに標準出力を追記:
$ command >> destination

標準エラーをリダイレクト:
$ command ^ destination

既存のファイルに標準エラーを追記:
$ command ^^ destination

destination には以下のどれかを使えます:

  • ファイル名 (出力は指定されたファイルに書き込まれます)。
  • & と他のファイル記述子の番号。出力は他のファイル記述子に書き込まれます。
  • &- 記号。出力はどこにも書き込まれません。

例:

標準出力をファイルにリダイレクト:
$ command > destination_file.txt

標準出力と標準出力の両方を同じファイルにリダイレクト:
$ command > destination_file.txt ^ &1

標準出力を消す:
$ command > &-

パイプ

あるコマンドの標準出力を次のコマンドの標準入力にリダイレクトすることができます。パイプ文字 (|) を使ってコマンドを区切って下さい。例:

cat example.txt | head

(標準出力だけでなく) 他のファイル記述子をパイプにリダイレクトすることもできます。次の例では、標準エラーのファイル記述子の番号と > をパイプに付けることで、あるコマンドの標準エラーを他のコマンドの標準入力に流し込んでいます:

$ command 2>| less

上記を実行すると command が実行されてから標準エラーが less コマンドにリダイレクトされます。

設定

fish のユーザー設定ファイルは ~/.config/fish/config.fish に存在しています。.bashrc と同じように、ターミナルが開かれる時に実行/定義するコマンド、関数を追加します。

ウェブインターフェイス

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 をデフォルトシェルに設定しない

Arch ではシェルスクリプトは Bash を使って書かれており、fish と完全な互換性がありません。fish をデフォルトシェルに設定しないことで、起動時に実行される Bash スクリプトで環境変数を正しく設定できます。また、fish などの Bash 非互換のターミナルを使った場合に起こる問題を避けられます。デフォルトシェルを fish に設定するとスクリプトエラーが発生します。以下では fish をデフォルトシェルに設定しないで使用する方法を説明しています。

.bashrc から fish を起動する

デフォルトシェルを Bash のままにしておいて exec fish という行をシェルの設定ファイル (例: .bashrc) に追加してください。これで Bash が /etc/profile/etc/profile.d のファイルを正しく読み込んでくれます。fish は bash のプロセスを置き換えるため、fish を終了するとターミナルも終了します。ローカルマシンだけでなく SSH サーバーでも使える方法のため、以下で説明する方法よりも汎用的です。

ヒント: 上記のように設定する場合、bash --norc を使うことで ~/.bashrc に書かれているコマンドを実行することなく Bash を開くことができます。普通に Bash を起動すると即座に Fish に戻ってしまいます。

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

fish を起動するコマンドラインオプションを使用してターミナルエミュレータを開くという方法もあります。大抵のターミナルでは -e スイッチを使います。例えば fish を使用して gnome-terminal を開くには、ショートカットを以下のように変更してください:

gnome-terminal -e fish

シェルの設定をサポートしていない LilyTerm などの軽量ターミナルエミュレータでは以下のように設定してください:

SHELL=/usr/bin/fish lilyterm

ターミナルの設定からターミナルのデフォルトシェルを fish に設定できたり、ターミナルエミュレータにプロファイル機能がある場合、ターミナルプロファイルで設定できることがあります。

どちらにしてもターミナルエミュレータを開いたら、fish が起動するようになります。

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

tmux で起動するシェルを fish に設定するには、以下を ~/.tmux.conf に記述してください:

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

tmux を起動すると fish が立ち上がるようになります。

fish をデフォルトシェルに設定する

fish をデフォルトシェルとして設定した場合、パスが正しく設定されていないことに気づくでしょう。~/.config/fish/config.fish ファイルにセクションを追加してログイン時にパスを適切に設定することができます。ログインシェルの場合だけ実行される .profile.bash_profile と同じです。

if status --is-login
        set PATH $PATH /usr/bin /sbin
end
ノート: $MOZ_PLUGIN_PATH など、他の環境変数も手動で追加する必要があります。fish をデフォルトシェルとしてシームレスに使えるようにするには骨が折れる作業量になります。fish をデフォルトシェルに設定しない方が簡単です。

グリーティングを無効化

デフォルトでは、fish は起動時にグリーティングメッセージを表示します。無効にするには、fish の設定ファイルに set fish_greeting と追加してください。

su で fish を起動する

su で Bash が起動する場合、fish の設定ファイルに以下の関数を定義します:

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

liquidprompt を使う

Liquidprompt は Bash と Zsh 用に作られたフル機能の人気のあるアダプティブプロンプトですが fish に対応する予定はありません [1]fish-lp プロジェクト は fish で Liquidprompt を実装しています。

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

カレントディレクトリが git ディレクトリの場合に fish でブランチなどの状態を表示するには、以下を ~/.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

ssh-agent の評価

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

$ eval (ssh-agent -c)

"command not found" フック

pkgfile には "command not found" フックが含まれており、認識できないコマンドを入力したときに自動的に公式リポジトリが検索されます。pkgfile をインストールすると自動的にフックが実行されるようになっています。

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

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

$ firefox &
$ disown

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

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

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

$ alias FooAliasName "foo"
$ funcsave FooAliasName

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

トラブルシューティング

履歴置換

Fish は履歴置換 (例: sudo !!) を実装しておらず、fish の開発者は 実装する予定もない と発言しています。しかしながら、履歴置換は多くのユーザーにとって必要不可欠な機能です。Reddit ユーザーの crossroads1112 が履歴置換の機能を再現した関数を作成しています (ただし構文は異なっています)。関数は github にあり、使い方はコメントとして書いてあります。オリジナルの構文に近づけた フォークバージョン も存在し、ヘルパー関数でコマンドを指定すれば command !! が使えます。

command !! を使用できるようにする方法は Fish の github wiki にもあります。bind_bang 関数で !! をコマンド履歴の最後のコマンドに展開する例が載っています。こちらの github issue も参照してください。

参照