コマンド履歴検索・補完

## Ubuntu
### インストール
```
$ sudo apt install peco
```

### 設定
`.bashrc`に以下を追記する
```bash
##################
#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`に記載する内容を以下にする。

```bash
##################
# 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の設定ファイルを作成する
```powershell
> $profile
> New-Item -Path $Profile -ItemType file -force
> Notepad $profile
```
#### 設定ファイルに以下を書き込む
##### 履歴表示１
```powershell
Set-PSReadLineKeyHandler -Chord 'ctrl+r' {  Invoke-Expression $(history | select CommandLine | peco) }
```
##### 履歴表示２
```powershell
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` でヒストリーが出るようになる


---

## Page Navigation

- Canonical URL: https://kznagamori.growi.cloud/公開ページ/便利ツール/peco
- Permalink: https://kznagamori.growi.cloud/66b1b22ad2ffcccf356b0b66
- Parent: [便利ツール](/6698832858843f831d164a05.md)
- Children: 0 total
- Total descendants: 0
- Siblings: 8 total
  - [Everything](/6698834758843f831d164bd3.md)
  - [Microsoft PowerToys](/675bbd001d6fdcaf9a3f32a2.md)
  - [VB-CABLE](/66e2db2c3ab3bc23670d90d1.md)
  - [aria2](/66fd2272fe62eb559b11803f.md)
  - [com0com](/66e2d0863ab3bc23670d7d69.md)
  - [snipaste](/6822dcb05a9a57bb7f18c51b.md)
  - [typora](/689567a569e87601389dc2c7.md)
  - [バイナリ⇔モトローラフォーマット変換](/66e2ce533ab3bc23670d746f.md)
- Last updated: 2025-05-08T17:19:46.751Z by kznagamori
- Full page listing (all children regardless of count): https://kznagamori.growi.cloud/_api/v3/page-listing/children?id=66b1b22ad2ffcccf356b0b66
