MochiuWiki : SUSE, EC, PCB
検索
個人用ツール
ログイン
Toggle dark mode
名前空間
ページ
議論
表示
閲覧
ソースを閲覧
履歴を表示
Fishの基礎 - 補完のソースを表示
提供: MochiuWiki : SUSE, EC, PCB
←
Fishの基礎 - 補完
あなたには「このページの編集」を行う権限がありません。理由は以下の通りです:
この操作は、次のグループのいずれかに属する利用者のみが実行できます:
管理者
、new-group。
このページのソースの閲覧やコピーができます。
== 概要 == Fishシェルの補完システムは非常に強力で柔軟性がある。<br> <br> Fishの補完システムを理解するには、これらの概念を理解して、実際に補完スクリプトを記述することが重要である。<br> また、既存の補完スクリプト (~/.config/fish/completionsディレクトリ) を参考にする。<br> <br> 補完システムの詳細を知りたい場合は、Fishの公式サイトにある[https://fishshell.com/docs/current/completions.html# Writing your own completions]を参照すること。<br> <br><br> == 補完関数 == <code>complete</code>コマンドを使用して、補完関数を定義する。<br> <br> <syntaxhighlight lang="fish"> # 基本構文 complete -c <コマンド> <オプション> -a '<補完候補>' -d '<補完候補の説明>' </syntaxhighlight> <br> * -c <コマンド> *: 補完を定義するコマンドを指定する。 * -a '<補完候補>' または -a "<補完候補>" *: 補完候補を指定する。 * -d '<補完候補の説明>' または -d "<補完候補の説明>" *: 補完候補の説明を提供する。 <br><br> == オプションの補完 == コマンドのオプションを補完する場合は、<code>-s</code> (短いオプション)、または、<code>-l</code> (長いオプション) を使用する。<br> <br> <syntaxhighlight lang="fish"> complete -c cat -s n -l number -d "Enumerate lines" </syntaxhighlight> <br><br> == 条件付き補完 == Fishでは、条件に基づいて異なる補完を定義することができる。<br> <br> <syntaxhighlight lang="fish"> if command -q gcc complete -c gcc -s O -a "0 1 2 3" -d "Optimization level" end </syntaxhighlight> <br> また、<code>-n "<条件>"</code>オプションを使用して、特定の条件下でのみ補完を行うことができる。<br> <br> 以下の例では、変数$commandsに含まれるサブコマンドがまだ入力されていない場合にのみ、それらのサブコマンドを補完候補として提示する。<br> <br> <syntaxhighlight lang="fish"> complete -c systemctl -n "not __fish_seen_subcommand_from $commands" -a "$commands" </syntaxhighlight> <br><br> == 動的補完 == 補完候補を動的に生成する場合は、コマンド置換を使用して動的に補完候補を生成する。<br> <br> <syntaxhighlight lang="fish"> complete -c ssh -a "(command cat ~/.ssh/config | string match -r '^Host\s+(\S+)' | string replace -r '^Host\s+' '')" -d "Known host" </syntaxhighlight> <br> 以下の例では、systemctl --state=helpの出力を動的に処理して、補完候補を生成している。<br> <br> <syntaxhighlight lang="fish"> complete -c systemctl -l state -d 'List of unit states' -xa '(systemctl --state=help --no-legend --no-pager | string match -v "*:")' </syntaxhighlight> <br><br> == 補完スクリプト == 複雑な補完ロジックは、別のファイルに記述して、<code>source</code>コマンドで読み込むことができる。<br> <br> <syntaxhighlight lang="fish"> source ~/.config/fish/completions/mycommand.fish </syntaxhighlight> <br><br> == 補完の優先順位 == <code>-n</code>オプションを使用して、特定の状況下でのみ補完を適用できる。<br> <br> <syntaxhighlight lang="fish"> complete -c git -n "__fish_use_subcommand" -a clone -d "Clone a repository" </syntaxhighlight> <br><br> == サブコマンドの補完 == 多くのコマンドはサブコマンドを持つ。<br> これらは、一般的に、<code>__fish_use_subcommand</code>関数を使用して処理する。<br> <br> <syntaxhighlight lang="fish"> complete -c git -f -n "__fish_use_subcommand" -a "clone" -d "Clone a repository" </syntaxhighlight> <br> <syntaxhighlight lang="fish"> complete -c git -f -n "__fish_use_subcommand" -a "commit" -d "Record changes to the repository" </syntaxhighlight> <br><br> == サブコマンドのオプション補完 == サブコマンドのオプション補完を行う場合は、以下に示すような手順を行う。<br> # メインコマンドの補完を定義する。 # サブコマンドの補完を定義する。 # サブコマンドのオプション補完を定義する。 <br> 以下に示す関数およびオプションを使用して、サブコマンドとそのオプションの補完を細かく制御できる。<br> * __fish_use_subcommand関数 *: Fishの組み込み関数であり、現在のコマンドラインにサブコマンドがまだ入力されていない場合に真を返す。 *: これにより、サブコマンドの補完を適切なタイミングで提供することができる。 * __fish_git_using_command clone関数 *: Fishの補完ファイル (completionsディレクトリ) で定義される関数である。 *: この関数は、現在のコマンドラインでサブコマンドが使用されている場合に真を返す。 * -nオプション *: これは、conditionオプションであり、指定された条件が真の場合にのみ補完を適用する。 * -fオプション *: これは、no-filesオプションであり、ファイル名の補完を無効にする。 * -xオプション *: これは、requires-parameterオプションであり、このオプションが値を取ることを示す。 * -aオプション *: これは、argumentsオプションであり、補完の候補を指定する。 * -dオプション *: これは、descriptionオプションであり、補完候補の説明を提供する。 <br> サブコマンドのオプションの補完を効果的に実装するには、以下に示す事柄を考慮する。<br> * 補完関数を作成して、現在のコマンドラインの状態を判断する。 * 条件付き補完を使用して、適切なコンテキストで正しい補完を提供する。 * 可能な場合は、動的補完を使用して、実行時に補完候補を生成する。 <br> 以下の例では、git cloneコマンドのオプション補完を示している。<br> 完全なgitコマンドの補完スクリプトは、Fishのインストールディレクトリ内にあるcompletionsディレクトリのgit.fishファイルで確認できる。<br> <br> <syntaxhighlight lang="fish"> function __fish_git_needs_command # Figure out if the current invocation already has a command. # # This is called hundreds of times and the argparse is kinda slow, # so we cache it as long as the commandline doesn't change. set -l cmdline "$(commandline -c)" if set -q __fish_git_cmdline; and test "$cmdline" = "$__fish_git_cmdline" if set -q __fish_git_cmd[1] echo -- $__fish_git_cmd return 1 end return 0 end set -g __fish_git_cmdline $cmdline set -l cmd (commandline -opc) set -e cmd[1] argparse -s (__fish_git_global_optspecs) -- $cmd 2>/dev/null or return 0 # These flags function as commands, effectively. set -q _flag_version; and return 1 set -q _flag_html_path; and return 1 set -q _flag_man_path; and return 1 set -q _flag_info_path; and return 1 if set -q argv[1] # Also print the command, so this can be used to figure out what it is. set -g __fish_git_cmd $argv[1] echo $argv[1] return 1 end set -g __fish_git_cmd return 0 end function __fish_git_using_command set -l cmd (__fish_git_needs_command) test -z "$cmd" and return 1 contains -- $cmd $argv and return 0 # Check aliases. set -l varname __fish_git_alias_(string escape --style=var -- $cmd) set -q $varname and contains -- $$varname[1][1] $argv and return 0 return 1 end # 1. git コマンドの補完を定義 complete -f -c git -n __fish_use_subcommand -a clone -d "Clone a repository into a new directory" # 2. git clone サブコマンドの補完を定義 complete -f -c git -n '__fish_git_using_command clone' # 3. git clone のオプション補完を定義 complete -f -c git -n '__fish_git_using_command clone' -l recursive -d "Initialize submodules in the clone" complete -f -c git -n '__fish_git_using_command clone' -s q -l quiet -d "Operate quietly" complete -f -c git -n '__fish_git_using_command clone' -s v -l verbose -d "Run verbosely" complete -f -c git -n '__fish_git_using_command clone' -l progress -d "Force progress reporting" complete -f -c git -n '__fish_git_using_command clone' -s n -l no-checkout -d "No checkout of HEAD is performed after clone is complete" complete -f -c git -n '__fish_git_using_command clone' -l bare -d "Make a bare Git repository" complete -f -c git -n '__fish_git_using_command clone' -l mirror -d "Set up a mirror of the source repository" complete -f -c git -n '__fish_git_using_command clone' -s o -l origin -x -d "Use <name> instead of 'origin' to track upstream" complete -f -c git -n '__fish_git_using_command clone' -s b -l branch -x -d "Checkout <branch> instead of the remote's HEAD" complete -f -c git -n '__fish_git_using_command clone' -l depth -x -d "Create a shallow clone of that depth" </syntaxhighlight> <br><br> __FORCETOC__ [[カテゴリ:シェルスクリプト]] == 組み込み関数 == Fishには補完を支援するための多くの組み込み関数がある。<br> <br> * <code>__fish_complete_directories</code> *: ディレクトリの補完 *: <br> *: 以下の例では、mycommandコマンドに対して、現在のディレクトリ内の全てのサブディレクトリを補完候補として提供している。 *: <syntaxhighlight lang="fish">complete -c mycommand -f -a "(__fish_complete_directories)"</syntaxhighlight> *: <br> * <code>__fish_complete_path</code> *: ファイルパスの補完 *: <br> *: 以下の例では、myfileコマンドに対して、現在のディレクトリ内の全てのファイルとディレクトリを補完候補として提供している。 *: <syntaxhighlight lang="fish">complete -c myfile -f -a "(__fish_complete_path)"</syntaxhighlight> *: <br> * <code>__fish_complete_users</code> *: システム上のユーザ名の補完 *: <br> *: 以下の例では、usercommandコマンドに対して、システム上の全てのユーザ名を補完候補として提供している。 *: <syntaxhighlight lang="fish">complete -c usercommand -f -a "(__fish_complete_users)"</syntaxhighlight> *: <br> * <code>__fish_complete_groups</code> *: グループ名の補完 *: <br> *: 以下の例では、groupcommandコマンドに対して、システム上の全てのグループ名を補完候補として提供している。 *: <syntaxhighlight lang="fish">complete -c groupcommand -f -a "(__fish_complete_groups)"</syntaxhighlight> *: <br> * <code>__fish_complete_pids</code> *: 実行中のプロセスIDの補完 *: <br> *: 以下の例では、killprocessコマンドに対して、システム上の実行中のプロセスIDを補完候補として提供している。 *: <syntaxhighlight lang="fish">complete -c killprocess -f -a "(__fish_complete_pids)"</syntaxhighlight> *: <br> * <code>__fish_complete_list</code> *: 指定されたリストから項目を補完する。 *: <br> *: 以下の例では、mycommandコマンドに対して、option1、option2、option3を補完候補として提供している。 *: <syntaxhighlight lang="fish">complete -c mycommand -f -a "(__fish_complete_list , 'option1' 'option2' 'option3')"</syntaxhighlight> *: <br> * <code>__fish_complete_command</code> *: 環境変数<code>PATH</code>に指定している実行可能ファイルを補完する。 *: <br> *: 以下の例では、runコマンドに対して、システムにインストールされている全てのコマンドを補完候補として提供している。 *: <syntaxhighlight lang="fish">complete -c run -f -a "(__fish_complete_command)"</syntaxhighlight> *: <br> * <code>__fish_complete_subcommand</code> *: サブコマンドの補完を支援する。 *: <br> *: 以下の例では、gitコマンドに対して、add、commit、pushをサブコマンドとして補完候補に提供している。 *: <syntaxhighlight lang="fish">complete -c git -f -n "__fish_use_subcommand" -a "(__fish_complete_subcommand -d 'Git command' -s add -s commit -s push)"</syntaxhighlight> *: <br> * <code>__fish_complete_suffix</code> *: 指定された接尾辞を持つファイルを補完する。 *: <br> *: 以下の例では、openfileコマンドに対して、現在のディレクトリ内の.txt拡張子を持つファイルを補完候補として提供している。 *: <syntaxhighlight lang="fish">complete -c openfile -f -a "(__fish_complete_suffix .txt)"</syntaxhighlight> *: <br> * <code>__fish_print_hostnames</code> *: <u>/etc/hosts</u>ファイルとmDNSから取得したホスト名を補完する。 *: <br> *: 以下の例では、some_commandコマンドに対して、システムが知りうるホスト名を補完候補として提供している。 *: <syntaxhighlight lang="fish">complete -c some_command -f -a "(__fish_print_hostnames)"</syntaxhighlight> *: <br> * <code>__fish_complete_history</code> *: コマンド履歴から項目を補完する。 *: <br> *: 以下の例では、rerunコマンドに対して、過去に実行したコマンドを補完候補として提供している。 *: <syntaxhighlight lang="fish">complete -c rerun -f -a "(__fish_complete_history)"</syntaxhighlight> *: <br> * <code>__fish_print_interfaces</code> *: システムのネットワークインターフェースを補完する。 *: <br> *: 以下の例では、ifconfigコマンドに対して、利用可能なネットワークインターフェース (例: eth0, wlan0等) を補完候補として提供している。 *: <syntaxhighlight lang="fish">complete -c ifconfig -f -a "(__fish_print_interfaces)"</syntaxhighlight> *: <br> * <code>__fish_print_filesystems</code> *: システムでサポートされているファイルシステムの一覧を補完する。 *: <br> *: 以下の例では、mountコマンドに対して、利用可能なファイルシステムタイプ (例: ext4, ntfs, vfat等) を補完候補として提供している。 *: <syntaxhighlight lang="fish">complete -c mount -f -a "(__fish_print_filesystems)"</syntaxhighlight> *: <br> * <code>__fish_print_packages</code> *: パッケージ管理システムからインストールされたパッケージの一覧を補完する。 *: <br> *: 以下の例では、zepper removeコマンドに対して、インストール済みのパッケージ名を補完候補として提供している。 *: <syntaxhighlight lang="fish">complete -c apt-get -f -n "__fish_seen_subcommand_from remove" -a "(__fish_print_packages)"</syntaxhighlight> *: <br> * <code>__fish_complete_uninstalled_packages</code> *: システムにインストールされていないが利用可能なパッケージの一覧を補完する。 *: <br> *: 以下の例では、zypper installコマンドに対して、インストールされていない利用可能なパッケージ名を補完候補として提供している。 *: <syntaxhighlight lang="fish">complete -c apt-get -f -n "__fish_seen_subcommand_from install" -a "(__fish_complete_uninstalled_packages)"</syntaxhighlight> *: <br> * <code>__fish_complete_man</code> *: manページの名前を補完する。 * <code>__fish_complete_time</code> *: 時間関連の補完を提供する。 * <code>__fish_apropos</code> *: aproposコマンドの結果を基に補完候補を生成する。 <br> また、組み込み関数を組み合わせてカスタム関数内で使用することにより、より複雑な補完ロジックを実現することができる。<br> <br><br> == 型付き引数 == 以下に示すオプションを使用して、引数の型を指定することができる。<br> * -r *: ファイルパス * -x *: 排他的 * -f *: ファイル <br><br> == カスタム関数 == 補完システムをより柔軟にするために、カスタム関数を定義することができる。<br> <br> 以下の例では、systemctlコマンドの--propertyオプションの補完候補を生成する。<br> <br> <syntaxhighlight lang="fish"> function __fish_systemd_properties # プロパティのリストを生成するロジック end </syntaxhighlight> <br><br> == 補完の削除 == 特定のコマンドの全ての補完を削除することができる。<br> <syntaxhighlight lang="fish"> complete -c <コマンド> -e </syntaxhighlight> <br><br> == 補完と関数の組み合わせ == 以下の例では、Systemdバージョンに応じて、異なる補完を提供している。<br> <br> <syntaxhighlight lang="fish"> set -l systemd_version (systemctl --version | string match "systemd*" | string replace -r "\D*(\d+)\D.*" '$1') if test $systemd_version -gt 208 2>/dev/null set commands $commands cat if test $systemd_version -gt 217 2>/dev/null set commands $commands edit end end </syntaxhighlight> <br><br> == 外部コマンドの使用 == <u>/<Fishのインストールディレクトリ>/share/fish/functions</u>ディレクトリに定義した関数を使用して、動的に補完候補を生成することができる。<br> <br><br> == エスケープと文字列操作 == Fishの文字列操作関数である<code>string replace</code>、<code>string match</code>等を使用して、補完候補を処理することができる。<br> <br><br> == 補完のテスト == 特定のコマンドの補完をテストできる。<br> <br> <syntaxhighlight lang="fish"> fish_complete_path <コマンド> </syntaxhighlight> <br><br> __FORCETOC__ [[カテゴリ:シェルスクリプト]]
Fishの基礎 - 補完
に戻る。
案内
メインページ
最近の更新
おまかせ表示
MediaWiki についてのヘルプ
ツール
リンク元
関連ページの更新状況
特別ページ
ページ情報
We ask for
Donations
Collapse