回答:
そのためにbash関数を使用できます。
〜/ .bashrcに以下を追加します。
qh() {
type -all "$1" ; { man "$1" || "$1" --help ;} | egrep -i -- "$2"
}
bashrc do を保存するとsource ~/.bashrc、次のことができます。
$ qh ls size
--block-size=SIZE scale sizes by SIZE before printing them; e.g.,
'--block-size=M' prints sizes in units of
-h, --human-readable with -l and/or -s, print human readable sizes
-s, --size print the allocated size of each file, in blocks
-S sort by file size, largest first
--sort=WORD sort by WORD instead of name: none (-U), size (-S),
-T, --tabsize=COLS assume tab stops at each COLS instead of 8
-aが、については何も言っていない-lかを-all、しかし機能は、作業を行います。
ではzsh、グローバルエイリアスを使用します。
$ alias -g '^^=--help|grep --color -i'
$ ls ^^ size
--block-size=SIZE scale sizes by SIZE before printing them; e.g.,
'--block-size=M' prints sizes in units of
1,048,576 bytes; see SIZE format below
-h, --human-readable with -l and/or -s, print human readable sizes
-s, --size print the allocated size of each file, in blocks
-S sort by file size, largest first
--sort=WORD sort by WORD instead of name: none (-U), size (-S),
-T, --tabsize=COLS assume tab stops at each COLS instead of 8
The SIZE argument is an integer and optional unit (example: 10K is 10*1024)
ではbash、あなたは使用することができるかもしれ履歴展開それがパイプを代入で仕事ができることをシェル構文解析で早期に十分に起こるものです。
置換したいテキストと、他の方法で使用する可能性が低い特殊文字(£私のキーボードにあるような場合)で履歴を準備します。
$ --help $(: £)|grep
bash: --help: command not found
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.次に、履歴展開を使用してそれを取得します。
$ ls !?£? size
ls --help $(: £)|grep size
--block-size=SIZE scale sizes by SIZE before printing them; e.g.,
'--block-size=M' prints sizes in units of
-h, --human-readable with -l and/or -s, print human readable sizes
-s, --size print the allocated size of each file, in blocks
-S sort by file size, largest first
--sort=WORD sort by WORD instead of name: none (-U), size (-S),
-T, --tabsize=COLS assume tab stops at each COLS instead of 8または、キーまたはキーシーケンスを押してreadline展開--help|grepすることもできます。bash(gdbreadlineを使用するような他のアプリケーションではなく)のみに適用するために、例えばで設定するためのAPIでbindあるbash組み込みコマンドを使用できます:bashreadline~/.bashrc
bind '"^^": "--help|grep "'
または~/.inputrc(readlineの設定ファイル)に追加します:
$if Bash
"^^": "--help|grep "
$endif
(readlineを使用するシェルrcやes、readlineを使用するシェルがありますが、そのバインディングを行うことが理にかなっていますが、AFAICTでは、rl_readline_name呼び出す前に変数を設定しないreadlineため、いくつかの$ifステートメントを追加できません(otherすべてのアプリケーションのように表示されます)そのアプリケーション名を告げることなくreadlineを使用します))。
^置換を行うには、最初のものから0.5 秒以内に(デフォルトで)秒を入力する必要があることに注意してください。
readlineバインディングを使用できます。
次のような行を追加します
"^^": "--help | grep "
〜/ .inputrcに
次に、用語で^ X ^ Rを押すと、バインディングがアクティブになります。
キーイングのls ^^結果はになりls --help | grepます。
lessヘルプメッセージの表示に使用検索クエリに一致する行の周囲のコンテキストを確認すると便利な場合があります。
hh () { "${1}" --help | less -p "${2}" ; }
このbash関数を呼び出す構文はqh、@ tgwtdtの答えの関数に似ています。最初の引数は調べるコマンドで、2番目の引数は検索語です。例えば:
hh ls size
hh ls "symbolic link"
これにより、ヘルプメッセージ全体が開き、less検索語のすべてのインスタンスが強調表示され、検索語の最初のインスタンスまでスクロールします。次に、を押しnて、検索語を含む次の行まで前方にスクロールし、次の行に移動することができnます。前のインスタンスに戻るには、を押しNます。使用しHome、End、Page Up、Page Down、Up Arrow、およびDown Arrow一般的なナビゲーションのためのキーを。qまたはQを押して終了lessし、コマンドラインに戻ります。
@tgwtdtのソリューションが気に入ったので、少し改善しました。
これは同じことをしますが、エラーを処理するために少しを行い、組み込みを処理しようとします。
qhは{}の代わりに()を使用するため、qh1()とoutはローカル(サブシェル内)です。
function qh () (
function qh1 () {
out="$(help "$1" 2>&1 )"
[ $? -ne 0 ] && return 1
echo "$out"
}
type -all "$1" ; { qh1 "$1" || "$1" --help 2>/dev/null || man "$1" 2>/dev/null ;} | egrep -i -- "$2"
)
qh () { type -all "$1" ; { "$1" --help || man "$1" ;} | egrep -i -- "$2" ;}#したがって、次のことができます:qh ls size、qh ls "something | another" etc.(オプション)type -all "$1"は$ 1に関する情報も追加します:エイリアス、関数、コマンドなど。そして、コマンド$ 1にオプション "--help"がなかった場合、男性 "$ 1"から情報を提供します(これは時々起こります)