コマンド履歴検索・補完

Ubuntu

インストール

$ sudo apt install peco

設定

.bashrcに以下を追記する

################## #for peco ################## function peco-select-history() { local tac which gtac &> /dev/null && tac="gtac" || \ which tac &> /dev/null && tac="tac" || \ tac="tail -r" READLINE_LINE=$(HISTTIMEFORMAT= history | $tac | sed -e 's/^\s*[0-9]\+\s\+//' | awk '!a[$0]++' | peco --query "$READLINE_LINE") READLINE_POINT=${#READLINE_LINE} } bind -x '"\C-r": peco-select-history'

WSL + Ubuntu 24.04LTSだと表示がおかしなる

.bashrcに記載する内容を以下にする。

################## # for peco ################## function peco-select-history() { local original_term=$TERM # 現在のTERMを保存 export TERM=xterm local tac which gtac &> /dev/null && tac="gtac" || \ which tac &> /dev/null && tac="tac" || \ tac="tail -r" READLINE_LINE=$(HISTTIMEFORMAT= history | $tac | sed -e 's/^\s*[0-9]\+\s\+//' | awk '!a[$0]++' | peco --query "$READLINE_LINE") READLINE_POINT=${#READLINE_LINE} export TERM=$original_term # 元のTERMに戻す } bind -x '"\C-r": peco-select-history'

使用方法

Ctrl + R でヒストリーが出るようになる

Windows

インストール

> scoop install peco

設定

powershellの設定ファイルを作成する

> $profile > New-Item -Path $Profile -ItemType file -force > Notepad $profile

設定ファイルに以下を書き込む

履歴表示1
Set-PSReadLineKeyHandler -Chord 'ctrl+r' { Invoke-Expression $(history | select CommandLine | peco) }
履歴表示2
Set-PSReadLineKeyHandler -Chord Ctrl+r -ScriptBlock { $command = Get-Content (Get-PSReadLineOption).HistorySavePath | peco --select-1 --on-cancel error if (-not $command) { return } [Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt() [Microsoft.PowerShell.PSConsoleReadLine]::Insert($command) } Set-PSReadLineOption -PredictionSource HistoryAndPlugin Set-PSReadLineOption -PredictionViewStyle ListView Set-PSReadLineOption -Colors @{ InLinePrediction = [ConsoleColor]::Cyan }

使用方法

Ctrl + R でヒストリーが出るようになる