tmuxのバッファ内容に基づいてオートコンプリートするには?

tmuxのバッファ内容に基づいてオートコンプリートするには?

時には画面上のテキストを入力したいのですが、コピーして貼り付けるのがそれほど速くない場合があります。

バッファの内容に基づいてコマンドラインからテキストを自動補完する方法はありますか?

ベストアンサー1

魚の皮

fzf-complete-from-tmux.sh

#!/bin/bash
tmux capture-pane -pS -100000 |      # Dump the tmux buffer.
  tac |                              # Reverse so duplicates use the first match.
  pcregrep -o "[\w\d_\-\.\/]+" |     # Extract the words.
  awk '{ if (!seen[$0]++) print }' | # De-duplicate them with awk, then pass to fzf.
  fzf --no-sort --exact +i           # Pass to fzf for completion.

魚の殻と組み合わせた例です。

# Ctrl-N: Complete based on the tmux buffer content.
bind \cn "commandline -i (fzf-complete-from-tmux.sh) 2>/dev/null"

すごい皮

Bashのバージョンを追加してくれた@juanitocaleroに感謝します。

#!/bin/bash

__screen-autocomplete__() {
    tmux capture-pane -pS -100000 |      # Dump the tmux buffer.
      tac |                              # Reverse so duplicates use the first match.
      grep -P -o "[\w\d_\-\.\/]+" |      # Extract the words.
      awk '{ if (!seen[$0]++) print }' | # De-duplicate them with awk, then pass to fzf.
      fzf --no-sort --exact +i           # Pass to fzf for completion.
}

__screen_autocomplete-inline__() {
    local selected="$(__screen-autocomplete__)"
    READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$selected${READLINE_LINE:$READLINE_POINT}"
    READLINE_POINT=$(( READLINE_POINT + ${#selected} ))
}

# Example binding with bash shell.
bind -x '"\C-n": "__screen_autocomplete-inline__"'

おすすめ記事