対話的(インタラクティブ)な使用

fish は、対話的な使用において非常に快適であることを自負しています。その理由は、次のいくつかのセクションで説明する機能にあります。

fish は fish 言語でコマンドを与えることで使用します。詳細については The Fish Language を参照してください。

ヘルプ

fish には広範なヘルプシステムが備わっています。特定の主題やコマンドに関するヘルプを取得するには help コマンドを使用してください。例えば、 help syntax と入力すると、このドキュメントの 構文の概要 が表示されます。

fish には各コマンドの man ページもあり、ヘルプページを man ページへと変換して提供しています。例えば、 man set を実行すると set のドキュメントを man ページとして表示します。

特定の組み込みコマンドに関するヘルプは、 -h パラメータでも取得できます。例えば、 fg 組み込みコマンドのヘルプを取得するには、 fg -h と入力するか help fg を実行します。

メインページは help index (または単に help )、あるいは man fish-doc で閲覧できます。チュートリアルは help tutorialman fish-tutorial で閲覧可能です。

自動提案 (Autosuggestions)

fish は入力に合わせて、 コマンド履歴 、補完、および有効なファイルパスに基づいてコマンドを提案します。コマンドを入力していくと、カーソルの後ろに控えめなグレーで提案が表示されます(この色は fish_color_autosuggestion 変数で変更可能です)。

自動提案を受け入れる(コマンドラインの内容を置き換える)には、 (右矢印キー)または ctrl-f を押します。提案された最初の単語だけを受け入れるには、 alt-alt-f を押します。自動提案が意図したものでない場合は、無視してください。受け入れない限り、その内容が実行されることはありません。

自動提案は、最初の数文字を入力するだけで頻繁に使用するコマンドを素早く呼び出すための強力な手段です。また、ディレクトリ階層を効率的に移動するための優れたテクニックでもあります。

もし自動提案が不要な場合は、 $fish_autosuggestion_enabled を 0 に設定することで無効にできます:

set -g fish_autosuggestion_enabled 0

タブ補完

タブ補完は、現代的なシェルには欠かせない時間を節約するための機能です。 tab を入力すると、fish はカーソル下の単語の残りを推測しようとします。候補が 1 つしかない場合は、それが挿入されます。候補が複数ある場合は、曖昧でない最長の部分を挿入した上で、目的のものを選べるメニュー(「ページャ」)を開きます。

ページャ内は矢印キー、 pageup / pagedowntab 、または shift-tab で移動できます。 ctrl-s ( pager-toggle-search のバインド。vi モードでは / )を押すと検索メニューが開き、リストを絞り込むことができます。

fish は、コマンド、変数名、ユーザー名、ファイル名など、いくつかの汎用的な補完機能を提供しています。

また、特定のプログラムに特化したスクリプト化された補完機能も数多く提供しています。その多くは ls-l オプションのような単純なものですが、中にはより高度なものもあります。例えば以下のようなものです。

  • manwhatis では、インストールされているマニュアルページが補完候補として表示されます。

  • make では、カレントディレクトリ内の Makefile にあるターゲットが補完候補として使用されます。

  • mount では、 fstab で指定されたマウントポイントが補完候補として使用されます。

  • aptrpmyum では、インストール済みまたはインストール可能なパッケージが表示されます。

独自の補完を作成したり、他人が作成したものをインストールしたりすることもできます。これについては Writing your own completions を参照してください。

Completion scripts are loaded on demand, like functions are. The difference is the $fish_complete_path list is used instead of $fish_function_path. Typically you can drop new completions in ~/.config/fish/completions/<name-of-command>.fish and fish will find them automatically.

シンタックスハイライト

fish は入力されるコマンドラインを逐次解釈し、シンタックスハイライト(構文強調)によってフィードバックを行います。最も重要なフィードバックは潜在的なエラーの検出です。デフォルトでは、エラーは赤色で表示されます。

検出されるエラーには以下のものが含まれます。

  • 存在しないコマンド。

  • 存在しないファイルからの読み込み、または存在しないファイルへの追記。

  • 出力リダイレクトの誤った使用。

  • 括弧の対応関係の不整合。

シンタックスハイライトをカスタマイズするには、 シンタックスハイライト変数 セクションに記載されている環境変数を設定します。

また、 fish は fish_config で選択できる作成済みのカラーテーマも提供しています。単に fish_config を実行するとブラウザベースのインターフェースが開きます。または、 fish から fish_config theme を使用することもできます。:

# disable nearly all coloring
fish_config theme choose none
# restore fish's default theme
fish_config theme choose default

あるいは、ターミナル上で直接すべてのテーマを確認するには、以下のようにします。:

fish_config theme show

すべてのシェルセッションを再起動せずにテーマを更新するには、まず config.fish に以下を追加して再起動し、各セッションで イベントハンドラ を定義するようにします。:

function apply-my-theme --on-variable=my_theme
    fish_config theme choose $my_theme
end

その後、いずれかのセッションから対応する ユニバーサル変数 を設定します。:

> set -U my_theme lava
> set -U my_theme snow-day

シンタックスハイライト変数

The colors used by fish for syntax highlighting can be configured by changing the values of various variables. The value of these variables can be one of the colors accepted by the set_color command. Options accepted by set_color like --foreground=, --background=, --bold, --dim, --italics, --reverse, --strikethrough, --underline and --underline-color= are also accepted.

例: エラーを強調(下線)し、かつ赤色にするには以下のようにします。:

set fish_color_error red --bold

fish のハイライトカラーを変更するために、以下の変数が利用可能です。

変数

意味

fish_color_normal

デフォルトの色

fish_color_command

echo などのコマンド

fish_color_keyword

if などのキーワード(未設定の場合はコマンドの色が適用されます)

fish_color_quote

"abc" などの引用符で囲まれたテキスト

fish_color_redirection

>/dev/null などの入出力リダイレクト

fish_color_end

;& などのプロセス区切り文字

fish_color_error

構文エラー

fish_color_param

通常のコマンド引数(パラメータ)

fish_color_valid_path

引数やリダイレクト先のうち、ファイル名であるもの(ファイルが存在する場合)

fish_color_option

最初の "--" 引数より前にある、 "-" で始まるオプション

fish_color_comment

'# important' などのコメント

fish_color_selection

vi ビジュアルモードで選択されたテキスト

fish_color_operator

*~ などのパラメータ展開演算子

fish_color_escape

\n\x70 などのエスケープ文字

fish_color_autosuggestion

自動提案(入力中のコマンドの後に続く提案内容)

fish_color_cwd

デフォルトプロンプトにおける現在の作業ディレクトリ

fish_color_cwd_root

ルートユーザー(root)時のデフォルトプロンプトにおける現在の作業ディレクトリ

fish_color_user

デフォルトプロンプトにおけるユーザー名

fish_color_host

デフォルトプロンプトにおけるホスト名

fish_color_host_remote

リモートセッション(ssh など)時のデフォルトプロンプトにおけるホスト名

fish_color_status

デフォルトプロンプトにおける、直前のコマンドが 0 以外(異常終了)だった際の終了コード

fish_color_cancel

キャンセルされたコマンドに表示される '^C' インジケータ

fish_color_search_match

履歴検索のヒット箇所およびページャで選択中のアイテム(背景色のみ)

fish_color_history_current

dirhcdh などのコマンドにおける、履歴内の現在位置

変数が設定されていない場合、または --theme=テーマ名 オプションを除いた後に変数が空である場合、fish は通常 $fish_color_normal の使用を試みますが、以下の例外があります。

  • $fish_color_keyword : まず $fish_color_command を試します。

  • $fish_color_option : まず $fish_color_param を試します。

  • $fish_color_valid_path については、色指定がなく修飾子(太字など)のみが指定されている場合、 $fish_color_param などの本来適用されるべき色にそれらの修飾子を付加します。一方、有効なパスに色が設定されている場合は、その色を使用し、他の色設定から修飾子を継承します。

ページャの色変数

fish は、選択肢のリストをテーブル形式で表示することがあります。これをページャと呼びます。

例: ページャの各行の背景色を設定するには、以下のようにします。:

set fish_pager_color_background --background=white

白とグレーの背景を交互に配置し、黒いテキストを表示する場合の例です。:

set fish_pager_color_prefix black
set fish_pager_color_completion black
set fish_pager_color_description black
set fish_pager_color_background --background=white
set fish_pager_color_secondary_background --background=brwhite

ページャの色に影響を与える変数:

変数

意味

fish_pager_color_progress

左下隅のプログレスバー

fish_pager_color_background

行の背景色

fish_pager_color_prefix

プレフィックス(接頭辞)文字列。すなわち、補完対象となっている入力済みの文字列

fish_pager_color_completion

補完内容そのもの。すなわち、提案された残りの文字列

fish_pager_color_description

補完内容の説明文

fish_pager_color_selected_background

選択されている補完候補の背景色

fish_pager_color_selected_prefix

選択されている補完候補のプレフィックス

fish_pager_color_selected_completion

選択されている補完候補のサフィックス(接尾辞)

fish_pager_color_selected_description

選択されている補完候補の説明文

fish_pager_color_secondary_background

選択されていない補完候補のうち、1行おき(偶数行)の背景色

fish_pager_color_secondary_prefix

選択されていない補完候補のうち、1行おき(偶数行)のプレフィックス

fish_pager_color_secondary_completion

選択されていない補完候補のうち、1行おき(偶数行)のサフィックス

fish_pager_color_secondary_description

選択されていない補完候補のうち、1行おき(偶数行)の説明文

「セカンダリ(1行おき)」や「選択時(selected)」の変数が設定されていない、あるいは空である場合は、通常の変数が使用されます。ただし、 $fish_pager_color_selected_background だけは例外で、まず $fish_color_search_match の背景色が試されます。

Abbreviations

To avoid needless typing, a frequently-run command like git checkout can be abbreviated to gco using the abbr command.

abbr -a gco git checkout

After entering gco and pressing space or enter, a gco in command position will turn into git checkout in the command line. If you want to use a literal gco sometimes, use ctrl-space [1].

Abbreviations are a lot more powerful than just replacing literal strings. For example you can make going up a number of directories easier with this:

function multicd
    echo cd (string repeat -n (math (string length -- $argv[1]) - 1) ../)
end
abbr --add dotdot --regex '^\.\.+$' --function multicd

Now, .. transforms to cd ../, while ... turns into cd ../../ and .... expands to cd ../../../.

The advantage over aliases is that you can see the actual command before using it, add to it or change it, and the actual command will be stored in history.

Programmable prompt

When it is fish's turn to ask for input (like after it started or the command ended), it will show a prompt. Often this looks something like:

you@hostname ~>

This prompt is determined by running the fish_prompt and fish_right_prompt functions.

The output of the former is displayed on the left and the latter's output on the right side of the terminal. For vi mode, the output of fish_mode_prompt will be prepended on the left.

If fish_transient_prompt is set to 1, fish will redraw the prompt with a --final-rendering argument before running a commandline, allowing you to change it before pushing it to the scrollback.

Fish ships with a few prompts which you can see with fish_config. If you run just fish_config it will open a web interface [2] where you'll be shown the prompts and can pick which one you want. fish_config prompt show will show you the prompts right in your terminal.

For example fish_config prompt choose disco will temporarily select the "disco" prompt. If you like it and decide to keep it, run fish_config prompt save.

You can also change these functions yourself by running funced fish_prompt and funcsave fish_prompt once you are happy with the result (or fish_right_prompt if you want to change that).

Configurable greeting

When it is started interactively, fish tries to run the fish_greeting function. The default fish_greeting prints a simple message. You can change its text by changing the $fish_greeting variable, for instance using a universal variable:

set -U fish_greeting

or you can set it globally in config.fish:

set -g fish_greeting 'Hey, stranger!'

or you can script it by changing the function:

function fish_greeting
    random choice "Hello!" "Hi" "G'day" "Howdy"
end

save this in config.fish or a function file. You can also use funced and funcsave to edit it easily.

Programmable title

Most terminals allow setting the text displayed in the titlebar of the terminal window. Fish does this by running the fish_title function. It is executed before and after a command and the output is used as a titlebar message.

The status current-command builtin will always return the name of the job to be put into the foreground (or fish if control is returning to the shell) when the fish_title function is called. The first argument will contain the most recently executed foreground command as a string.

The default title shows the hostname if connected via ssh, the currently running command (unless it is fish) and the current working directory. All of this is shortened to not make the tab too wide.

Examples:

To show the last command and working directory in the title:

function fish_title
    # `prompt_pwd` shortens the title. This helps prevent tabs from becoming very wide.
    echo $argv[1] (prompt_pwd)
    pwd
end

コマンドラインエディタ

fish のエディタは、コピー&ペースト、 検索可能な履歴 、および特別なキーボードショートカットに割り当て可能な多くのエディタ機能を備えています。

bash や他のシェルと同様に、fish には 2 種類のキーボードショートカット(キーバインド)が用意されています。一つは Emacs テキストエディタ、もう一つは vi テキストエディタにインスパイアされたものです。デフォルトの編集モードは Emacs です。 fish_vi_key_bindings を実行することで vi モードに切り替えられ、 fish_default_key_bindings で元に戻せます。また、関数を作成し、 fish_key_bindings 変数にその関数名を設定することで、独自のキーバインドを作成することも可能です。例:

function fish_hybrid_key_bindings --description \
"Vi-style bindings that inherit emacs-style bindings in all modes"
    for mode in default insert visual
        fish_default_key_bindings -M $mode
    end
    fish_vi_key_bindings --no-erase
end
set -g fish_key_bindings fish_hybrid_key_bindings

fish に含まれるキーバインドは、それぞれのテキストエディタで人気のあるショートカットを多く取り入れていますが、完全な実装ではありません。エディタの全機能が必要な場合のために、現在のコマンドラインをお好みのエディタで開くショートカット(デフォルトは alt-e )が用意されています。

共通のバインド

一部のバインドは Emacs モードと vi モードで共通です。これは、それらがテキスト編集用のバインドではないため、あるいは特定のキーに対する vi/Vim の挙動がシェルには適さないためです。

  • tab は現在のトークンを 補完 します。 shift-tab は現在のトークンを補完し、ページャの検索モードを開始します。 tabctrl-i と同じです。

  • (左矢印)および (右矢印)は、カーソルを左右に 1 文字移動させます。カーソルがすでに末尾にあり、自動提案が表示されている場合、 を押すとその提案を確定(受け入れ)します。

  • enter は現在のコマンドラインを実行します。内容が未完了の場合(例: )end が不足している場合)は改行を挿入します。

  • alt-enter はカーソル位置に改行を挿入します。これは、すでに完結しているコマンドラインに新しい行を追加したい場合に便利です。

  • alt- および alt- は、カーソルを 1 引数(macOS では 1 単語)ずつ左右に移動させます。コマンドラインが空の場合、これらはディレクトリ履歴を前後に移動します。カーソルがすでに末尾にあり、自動提案が表示されている場合、 alt- (または alt-f )を押すと、提案の最初の引数(macOS では 1 単語)を確定します。

  • ctrl- および ctrl- は、カーソルを 1 単語ずつ左右に移動させます。自動提案がある場合、移動した範囲の単語を確定します。

  • shift- および shift- は、句読点で止まらずにカーソルを 1 単語(大きな単語単位)ずつ左右に移動させます。自動提案がある場合、その「大きな単語」を確定します。

  • および (Emacs 愛好家の方は ctrl-p および ctrl-n )は、検索開始前にコマンドラインに入力されていた文字列を含むコマンドを、履歴から前後方向に検索します。検索開始時にコマンドラインが空だった場合は、すべてのコマンドが一致対象となります。詳細は 履歴 セクションを参照してください。

  • alt- および alt- は、検索開始前にカーソル下にあったトークンを含むものを、履歴から前後方向に検索します。開始時にカーソルがトークン上になかった場合は、すべてのトークンが一致対象となります。詳細は 履歴 セクションを参照してください。

  • ctrl-c は実行中のプロセスを中断(終了)させます(SIGINT)。

  • ctrl-d はカーソルの右側の 1 文字を削除します。コマンドラインが空の状態で ctrl-d を押すと、fish を終了します。

  • ctrl-u は、行の先頭からカーソル位置までの内容を削除します(削除した内容は キルリング に移動します)。

  • ctrl-l は、プロンプトより上のテキストをターミナルのスクロールバックに押し出し、画面を消去して再描画します。

  • ctrl-w は、直前のパスコンポーネント(直前の "/"、":"、"@"までのすべて)を削除します(削除した内容は コピー&ペースト(キルリング) に移動します)。

  • ctrl-x は現在のバッファをシステムのクリップボードにコピーし、 ctrl-v はクリップボードの内容を挿入します。(fish_clipboard_copy および fish_clipboard_paste を参照してください)

  • alt-d は次の単語を コピー&ペースト(キルリング) に移動(削除)します。

  • ctrl-delete は次の単語(macOS では次の引数)を コピー&ペースト(キルリング) に移動します。

  • alt-d は、コマンドラインが空の場合、ディレクトリ履歴を一覧表示します。

  • alt-delete は次の引数(macOS では次の単語)を コピー&ペースト(キルリング) に移動します。

  • shift-delete は、現在の履歴項目または自動提案をコマンド履歴から削除します。

  • alt-h (または f1 )は、現在のコマンドのマニュアルページが存在すれば表示します。

  • alt-l はカレントディレクトリの内容を表示します。ただし、カーソルがディレクトリの引数上にある場合は、そのディレクトリの内容を表示します。

  • alt-o は、カーソル位置のファイルをページャで開きます。カーソルがコマンドの位置にあり、そのコマンドがスクリプトである場合は、代わりにエディタでそのスクリプトを開きます。エディタは $VISUAL$EDITOR 変数のうち、最初に見つかったものが使用されます。

  • alt-p は、カーソル下のジョブの末尾に文字列 &| less; を追加します。これにより、コマンドの出力がページング(画面ごとに表示)されます。 PAGER 変数を設定している場合は、 less の代わりにその値が使用されます。

  • alt-w は、カーソル下のコマンドの短い説明を表示します。

  • alt-e は、現在のコマンドラインを外部エディタで編集します。エディタは $VISUAL$EDITOR 変数のうち、最初に見つかったものが使用されます。

  • alt-valt-e と同じです。

  • alt-s は現在のコマンドラインの先頭に sudo を付与します。コマンドラインが空の場合は、直前のコマンドラインの先頭に sudo を付与します。 sudo がインストールされていない場合は、 doaspleaserun0 といった類似のコマンドを順に試行します。

  • ctrl-space は、 省略名(abbreviation) を展開せずにスペースを挿入します。 vi モードの場合、これは挿入モードにのみ適用されます。

Emacs モードのコマンド

Emacs モードを有効にするには、 fish_default_key_bindings を使用します。これは fish のデフォルト設定でもあります。

  • homectrl-a は、カーソルを行の先頭に移動します。

  • endctrl-e は、行の末尾に移動します。カーソルがすでに末尾にあり、自動提案が表示されている場合、 endctrl-e を押すとその提案を確定します。

  • ctrl-bctrl-f は、共通バインドの (左矢印)および (右矢印)と同様に、カーソルを左右に 1 文字移動させるか、自動提案を確定します。

  • alt-balt-f は、カーソルを左右に 1 単語移動させるか、自動提案の 1 単語分を確定します。コマンドラインが空の場合、代わりにディレクトリ履歴を前後に移動します。

  • ctrl-nctrl-p は、共通バインドの上下矢印キーと同様に、カーソルを上下に移動させるか、履歴を辿ります。

  • delete は前方の 1 文字を、 backspacectrl-h は後方の 1 文字を削除します。

  • ctrl-backspace は後方の 1 単語を削除し、 alt-backspace は後方の 1 引数を削除します。macOS では、この割り当てが逆になります。

  • alt-< はコマンドラインの先頭へ、 alt-> は末尾へ移動します。

  • ctrl-k は、カーソル位置から行末までを削除します(削除した内容は コピー&ペースト(キルリング) に移動します)。

  • escape および ctrl-g は、現在の操作をキャンセルします。一意な補完が行われた直後に押すと、その補完を取り消します。

  • alt-c は、現在の単語の先頭を大文字にします(キャピタライズ)。

  • alt-u は、現在の単語をすべて大文字にします。

  • ctrl-t は、直前の 2 文字を入れ替えます。

  • alt-t は、直前の 2 単語を入れ替えます。

  • ctrl-zctrl-_ (一部のターミナルでは ctrl-/ )は、直前の編集操作を取り消します(アンドゥ)。

  • alt-/ctrl-shift-z は、直前のアンドゥをやり直します(リドゥ)。

  • ctrl-r は、履歴をページャで開きます。検索に一致する履歴項目が数件ずつ表示されます。再度 ctrl-r を押すとより古い項目を検索し、 ctrl-s (通常はページャ検索の切り替え)を押すとより新しい項目へと移動します。検索バーは常に選択された状態になります。

これらのキーバインドは、組み込みの bind コマンドを使用して変更できます。

Vi mode commands

Vi mode allows for the use of vi-like commands at the prompt. Initially, insert mode is active. escape enters command mode. The commands available in command, insert and visual mode are described below. Vi mode shares some bindings with Emacs mode.

To enable vi mode, use fish_vi_key_bindings. It is also possible to add all Emacs mode bindings to vi mode by using something like:

function fish_user_key_bindings
    # Execute this once per mode that emacs bindings should be used in
    fish_default_key_bindings -M insert

    # Then execute the vi-bindings so they take precedence when there's a conflict.
    # Without --no-erase fish_vi_key_bindings will default to
    # resetting all bindings.
    # The argument specifies the initial mode (insert, "default" or visual).
    fish_vi_key_bindings --no-erase insert
end

When in vi mode, the fish_mode_prompt function will display a mode indicator to the left of the prompt. To disable this feature, override it with an empty function. To display the mode elsewhere (like in your right prompt), use the output of the fish_default_mode_prompt function.

When a binding switches the mode, it will repaint the mode-prompt if it exists, and the rest of the prompt only if it doesn't. So if you want a mode-indicator in your fish_prompt, you need to erase fish_mode_prompt e.g. by adding an empty file at ~/.config/fish/functions/fish_mode_prompt.fish. (Bindings that change the mode are supposed to call the repaint-mode bind function, see bind)

The fish_vi_cursor function will be used to change the cursor's shape depending on the mode in supported terminals. The following snippet can be used to manually configure cursors after enabling vi mode:

# Emulates vim's cursor shape behavior
# Set the normal and visual mode cursors to a block
set fish_cursor_default block
# Set the insert mode cursor to a line
set fish_cursor_insert line
# Set the replace mode cursors to an underscore
set fish_cursor_replace_one underscore
set fish_cursor_replace underscore
# Set the external cursor to a line. The external cursor appears when a command is started.
# The cursor shape takes the value of fish_cursor_default when fish_cursor_external is not specified.
set fish_cursor_external line
# The following variable can be used to configure cursor shape in
# visual mode, but due to fish_cursor_default, is redundant here
set fish_cursor_visual block

Additionally, blink can be added after each of the cursor shape parameters to set a blinking cursor in the specified shape.

Fish knows the shapes "block", "line" and "underscore", other values will be ignored.

If the cursor shape does not appear to be changing after setting the above variables, it's likely your terminal emulator does not support the capabilities necessary to do this.

Command mode

Command mode is also known as normal mode.

  • h moves the cursor left.

  • l moves the cursor right.

  • k and j search the command history for the previous/next command containing the string that was specified on the commandline before the search was started. If the commandline was empty when the search started, all commands match. See the history section for more information on history searching. In multi-line commands, they move the cursor up and down respectively.

  • i enters insert mode at the current cursor position.

  • I enters insert mode at the beginning of the line.

  • v enters visual mode at the current cursor position.

  • a enters insert mode after the current cursor position.

  • A enters insert mode at the end of the line.

  • o inserts a new line under the current one and enters insert mode

  • O (capital-"o") inserts a new line above the current one and enters insert mode

  • 0 (zero) moves the cursor to beginning of line (remaining in command mode).

  • d,d deletes the current line and moves it to the コピー&ペースト(キルリング).

  • D deletes text after the current cursor position and moves it to the コピー&ペースト(キルリング).

  • p pastes text from the コピー&ペースト(キルリング).

  • u undoes the most recent edit of the command line.

  • ctrl-r redoes the most recent edit.

  • [ and ] search the command history for the previous/next token containing the token under the cursor before the search was started. See the history section for more information on history searching.

  • / opens the history in a pager. This will show history entries matching the search, a few at a time. Pressing it again will search older entries, pressing ctrl-s (that otherwise toggles pager search) will go to newer entries. The search bar will always be selected.

  • backspace moves the cursor left.

  • g,g / G moves the cursor to the beginning/end of the commandline, respectively.

  • ~ toggles the case (upper/lower) of the character and moves to the next character.

  • g,u lowercases to the end of the word.

  • g,U uppercases to the end of the word.

  • :,q exits fish.

Insert mode

  • escape enters command mode.

  • backspace removes one character to the left.

  • ctrl-n accepts the autosuggestion.

Visual mode

カスタムバインド

ここに挙げた標準のバインドに加えて、 bind を使って独自のバインドを定義することもできます。:

# Prints ``^C`` and a new prompt
bind ctrl-c cancel-commandline

bind 文は config.fish か、 fish_user_key_bindings という名前の関数の中に記述してください。

設定したバインドを変更したくなり、fish のデフォルトに戻したい場合は、その設定を消去できます。:

bind --erase ctrl-c

fish はプリセットのバインドを記憶しているため、消去すると元の設定が再び有効になります。これにより、以前の設定が何だったかを覚えて自分で再設定する手間が省けます。

vi バインド を使用している場合、 bind はデフォルトで ノーマルモード(コマンドモード) のキーをバインドすることに注意してください。 挿入モード でバインドするには以下のようにします。:

bind --mode insert ctrl-c 'commandline -r ""'

キーシーケンス

キーの名前を調べるには、 fish_key_reader を使用できます。

> fish_key_reader # Press Alt + right-arrow
Press a key:
bind alt-right 'do something'

注意点として、ターミナルがキーをエンコードしてアプリケーション(この場合は fish)に送信する歴史的な仕組みのせいで、多くの組み合わせが区別できなかったり、バインドできなかったりします。通常のエンコーディングでは、 ctrl-i はタブキーと 同一 であり、 ctrl が押されている間は shift を検出できません。

より強力なエンコーディング・スキームも存在し、fish はターミナルに対してそれらを有効にするよう試みますが、依然として未対応のターミナルも多く存在します。 fish_key_reader が 2 つの異なるキーに対して同じシーケンスを表示する場合、それはターミナル側が同じシーケンスを送信しているためであり、fish 側で対処できることはありません。これらのスキームが普及し、入力の柔軟性が高まることが期待されています。

歴史的な仕組みでは、ターミナルにおいて escapealt と同じ扱いになります。 escape の後に別のキーを押した場合と、 alt とそのキー(またはそのキーが送るエスケープシーケンス)を同時に押した場合を区別するため、fish はエスケープ文字を検知した後に一定時間待機します。この待機時間は fish_escape_delay_ms 変数で設定可能です。

もし escape を押した後に別の文字を押し、それを alt +その文字として認識させたい場合は、以下のように高い値を設定してください。:

set -g fish_escape_delay_ms 100

同様に、短いシーケンスとそれを含む長いシーケンスの両方をバインドしている場合の「他の」キー入力の曖昧さを解消するために、 fish_sequence_key_delay_ms が用意されています:

# This binds the sequence j,k to switch to normal mode in vi mode.
# If you kept it like that, every time you press "j",
# fish would wait for a "k" or other key to disambiguate
bind -M insert -m default j,k cancel repaint-mode

# After setting this, fish only waits 200ms for the "k",
# or decides to treat the "j" as a separate sequence, inserting it.
set -g fish_sequence_key_delay_ms 200

コピー&ペースト(キルリング)

fish はコピー&ペースト機能として、 Emacs スタイルの「キルリング」を使用します。例えば、 ctrl-k ( kill-line )を使うと、現在のカーソル位置から行末までをカットできます。カットされた( Emacs 用語で言うところの「キル(killed)」された)文字列は、キルリングと呼ばれるリストに挿入されます。キルリングから最新の値を貼り付ける( Emacs ではこれを「ヤンク(yanking)」と呼びます)には、 ctrl-y ( yank 入力関数)を使用します。貼り付けた直後に alt-y ( yank-pop )を押すと、一つ前のカット内容に順次切り替えることができます。

外部とのコピー&ペーストも、 ctrl-x / ctrl-v のバインド( fish_clipboard_copy および fish_clipboard_paste 関数 [3] )と、ターミナルの貼り付け機能の両方でサポートされています。ターミナル経由の場合、 fish は「ブラケット・ペースト・モード(Bracketed Paste Mode)」を有効にしているため、手入力されたテキストと貼り付けられたテキストを区別できます。さらに、シングルクォート内で貼り付けを行うと、貼り付けられた内容に含まれるシングルクォートやバックスラッシュが自動的にエスケープされます。これにより、貼り付け後にクォートを閉じるだけで、結果を一つのトークンとして扱うことができます。キルリングの内容は変数 fish_killring に保存されます。

begin-selectionend-selection コマンド(デフォルトでは未バインド。 vi ビジュアルモードでの選択に使用されます)は、選択範囲を広げるカーソル移動コマンドと組み合わせてテキスト選択を制御します。変数 fish_cursor_selection_mode を使用して、選択範囲にカーソル下の文字を含めるか( inclusive )、含めないか( exclusive )を設定できます。デフォルトは exclusive で、どのようなカーソル形状でもうまく機能します。 vi モード、特に block (ブロック型)や underscore (アンダースコア型)のカーソル形状を使用している場合は、 inclusive の方が好みに合うかもしれません。

複数行編集

fish のコマンドラインエディタでは、数行にわたる長いコマンドを扱うことができます。コマンドを複数行に分けるには、以下の 3 つの方法があります。

  • forbeginif などのブロックコマンドに対して、対応する end コマンドが入力されず、ブロックが閉じられていない状態で enter キーを押す。

  • enter キーの代わりに alt-enter を押す。

  • enter キーを押す前にバックスラッシュ( \ )を挿入して、改行をエスケープする。

fish のコマンドラインエディタは、単一行モードでも複数行モードでも全く同じように動作します。行間を移動するには、左右の矢印キーやその他のキーボードショートカットを使用します。

検索可能なコマンド履歴

コマンドが実行されると、その内容は履歴リストに記憶されます。重複する履歴項目は自動的に削除されます。上下の矢印キーを押すことで、履歴を前後に検索できます。履歴検索を開始した際にコマンドラインが空でない場合、入力されている文字列を含むコマンドのみが表示されます。

alt- および alt- を押すことでも履歴検索が行われますが、こちらはコマンドライン全体ではなく、実行前のように各要素に分割された単位で検索されます。具体的には、カーソル位置にある要素と一致するものを履歴から探し出します。

より複雑な検索を行う場合は、 ctrl-r を押して、履歴を検索できるページャを開くことができます。1 ページに表示される項目数は限られていますが、再度 ctrl-r [4] を押すと次のページへ、 ctrl-s [5] を押すと前のページへ移動します。テキストを入力して検索結果を絞り込むことも可能です。

履歴検索は、検索文字列に大文字が含まれていない限り大文字小文字を区別しません。 escapepagedown を押すと、検索を終了して検索文字列を編集できます。

コマンドラインの先頭にスペースを入れると、その行全体が履歴に保存されなくなります。次のコマンドが実行されるまでは呼び出し可能ですが、ディスクには保存されません。これは、入力ミスを修正する場合などに便利です。

デフォルトでは、コマンド履歴は ~/.local/share/fish/fish_history (または $XDG_DATA_HOME/fish/fish_history が設定されている場合はその場所)に保存されます。ただし、環境変数 fish_history を設定することで、履歴セッションの名前( <セッション名>_history というファイルになります)を変更可能です。これはシェルの起動前でも実行中でも行えます。

その他の操作については、 history コマンドを参照してください。

Examples:

「make」という単語を含む過去の項目を検索するには、コンソールに make と入力して上矢印キーを押します。

コマンドラインが cd m となっている場合、 m の文字の上にカーソルを置いて alt- を押すと、「m」を含む過去に入力された単語を検索します。

プライベートモード

fish には、コマンド履歴をディスク上の履歴ファイルに書き込まない「プライベートモード」があります。このモードを有効にするには、 $fish_private_mode に空でない値を設定するか、 fish --private (短縮形は fish -P )で起動します。

-P オプションで起動すると、過去の履歴を非表示にするだけでなく、新たな履歴がディスクに書き込まれるのも防ぎます。これは(スクリーンキャストなどで)個人情報の漏洩を防ぎたい場合や、機密情報を扱う際に便利です。

独自の fish スクリプトを作成する際、ユーザーのプライバシーへの配慮として挙動を変更したい場合は、 fish_private_mode 変数を確認( if test -n "$fish_private_mode" ... )することができます。