web-dev-qa-db-ja.com

bashでのCtrl-R逆検索の代替

私は幸せで本当に好きです Ctrl-R bashシェルの後方検索機能。時々混乱するので、私の同僚の何人かはそれを好きではありません。私はそれらを理解しています。間違った文字を入力すると、履歴の現在の位置は過去のどこかにあり、最近の一致は見つかりません。

シェルの歴史を遡るのに、よりユーザーフレンドリーな方法はありますか?

バッシュにこだわりたい。別のシェルを提案することは、この質問に対する答えではありません。

「失われた位置」の問題はここで説明されています: https://stackoverflow.com/questions/34793704/reset-bash-history-search-position これらのソリューションは機能します。そのとおり。しかし、私の見解によれば、解決策は簡単でユーザーフレンドリーではありません。これらのソリューションは単純で単純ではありません。これらは過去の解決策です。かつて、人間はコンピュータが入力を望んだ方法を学ぶ必要がありました。しかし、今日のツールは、ユーザーにとって簡単な方法で入力を受け入れる必要があります。

たぶん誰かがPyCharmのようなジェットブレーンのIDEを知っています。「foobar」を検索すると、「foo_bar」を含む行が表示されます。それは素晴らしいです、それはunixです:-)

3
guettli

ファジーファインダープログラム [〜#〜] fzf [〜#〜] を使用しています。私は自分のキーバインディングとシェルスクリプトを記述して[〜#〜] fzf [〜#〜]をリバースの選択ツールとして利用しています-インタラクティブBashシェルの履歴を検索します。 Config GitHubリポジトリからコードをコピーして貼り付けてください。

〜/ .bashrc設定ファイル

# Test if fuzzy Finder program _Fzf_ is installed.
#
if type -p fzf &> /dev/null; then

  # Test if _Fzf_ specific _Readline_ file is readable.
  #
  if [[ -f ~/.inputrc.fzf && -r ~/.inputrc.fzf ]]; then

    # Make _Fzf_ available through _Readline_ key bindings.
    #
    bind -f ~/.inputrc.fzf
  fi
fi

〜/ .inputrc.fzf設定ファイル##

$if mode=vi

  # Key bindings for _Vi_ _Insert_ mode
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  set keymap vi-insert

  "\C-x\C-a": vi-movement-mode
  "\C-x\C-e": Shell-expand-line
  "\C-x\C-r": redraw-current-line
  "\C-x^": history-expand-line
  "\C-r": "\C-x\C-addi$(HISTTIMEFORMAT= history | fzf-history)\C-x\C-e\C-x\C-r\C-x^\C-x\C-a$a"

  # Key bindings for _Vi_ _Command_ mode
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  set keymap vi-command

  "\C-r": "i\C-r"
  "\ec": "i\ec"

$endif

fzf-history実行可能Bashスクリプト

#!/usr/bin/env bash
#
# Retrieve command from history with fuzzy Finder
# ===============================================
# Tim Friske <[email protected]>
#
# See also:
#   * man:bash[1]
#   * man:fzf[1]
#   * man:cat[1]

shopt -os nounset pipefail errexit errtrace
shopt -s extglob globstar

function print_help {
  1>&2 cat \
<<'HELP'
usage:
  HISTTIMEFORMAT= history | fzf-history
HELP
}

function fzf_history {
  if [[ -t 0 ]]; then
    print_help
    exit
  fi

  local fzf_options=()
  fzf_options+=(${FZF_DEFAULT_OPTS:-})
  fzf_options+=('--tac' '-n2..,..' '--tiebreak=index')
  fzf_options+=(${FZF_HISTORY_FZF_OPTS:-})
  fzf_options+=('--print0')

  local cmd='' cmds=()
  while read -r -d '' cmd; do
    cmds+=("${cmd/#+([[:digit:]])+([[:space:]])/}")
  done < <(fzf "${fzf_options[@]}")
  if [[ "${#cmds[*]}" -gt 0 ]]; then
    (IFS=';'; printf '%s\n' "${cmds[*]}")
  fi
}

fzf_history "$@"

key-bindings.bashソース可能なBashスクリプト

FZF'sBashから取得してわずかに変更キーバインディング ファイルは、Bashの履歴を逆検索するためのEmacsモード互換のキーバインディングです。 Ctrl-R (テストされていない):

if [[ ! -o vi ]]; then
  # Required to refresh the Prompt after fzf
  bind '"\er": redraw-current-line'
  bind '"\e^": history-expand-line'

  # CTRL-R - Paste the selected command from history into the command line
  bind '"\C-r": " \C-e\C-u\C-y\ey\C-u$(HISTTIMEFORMAT= history | fzf-history)\e\C-e\er\e^"'
fi
5
Tim Friske
  • 上矢印:ごく最近のものにのみ実用的です。
  • grep blablabla ~/.bash_history:各コマンドの後に履歴をファイルに保存するようにbashを設定する必要があります。

私から ~/.bashrcコマンドとTweakの機能を知りたい場合があります。

# don't put duplicate lines in the history. See bash(1) for more options
HISTCONTROL=ignorespace:ignoredups:erasedups
HISTFILESIZE=99999
HISTSIZE=99999
export Prompt_COMMAND="history -a; $Prompt_COMMAND"
# append to the history file, don't overwrite it
shopt -s histappend
#history
shopt -s cmdhist
shopt -s histreedit
shopt -s histverify
shopt -s lithist
2
ctrl-alt-delor

価値があるのは、ttyの停止/開始機能を無効にした場合(おそらくあまり使用しないこと)、Control-Sを押すことで検索方向を変更できます。

% stty stop undef

% true str1
% true str2
% true str3
% true str4
Control-R => (reverse-i-search)`str': true str4
Control-R => (reverse-i-search)`str': true str3
Control-S => (i-search)`str': true str3
Control-S => (i-search)`str': true str4

「ユーザーフレンドリー」または直感的でないキーバインディングについては、それらはemacsキーバインディングです。 emacseverybody向けではありませんが、そこには驚きはありません(秘書や他のそれほど気取らない個人がすぐに、大騒ぎすることなく習得できましたが)。それは文化的なものです。

0
pizdelect