Sample Usage

Enno edited this page May 20, 2018 · 37 revisions

pecoghq = ghq + peco (miyagawa)

This version shortens the displayed path so that stuff like your username doesn't repeat (e.g. /Users/lestrrat/.../src/github.com/somebody/foobar will match my "lestrrat" username when it's not the github username it's matching:

# feed this result into cd via zsh or whatever
ghq list -p | perl -nlpe 's[.*src/(.*)][$1\0$_]' | peco --null

pecodoc = ghq + godoc + peco on zsh

requires ghq

function peco-godoc() {
    local selected_dir=$(ghq list --full-path | peco --query "$LBUFFER")
    if [ -n "$selected_dir" ]; then
        if [ ! -n "$PAGER" ]; then
           PAGER=less
        fi
        BUFFER="godoc ${selected_dir} | $PAGER"
        zle accept-line
    fi
    zle clear-screen
}
zle -N peco-godoc
bindkey '^g' peco-godoc # map to whatever key you like

pecodoc (minus ghq)

http://d.hatena.ne.jp/syohex/20140620/1403257070

pecrant = vagrant global-status + peco

http://gongo.hatenablog.com/entry/2014/06/19/232538

pcd = directory changer on windows

@echo off

if "%1" equ "add" (
  if "%2" neq "" (
    echo %2 >> "%USERPROFILE%\.peco-cd"
    goto end
  )
  goto usage
)
if "%1" equ "edit" (
  goto edit
)
if "%1" neq "" (
  goto usage
)
goto query

:usage
echo %0 [add PATH ^| edit]
goto end

:edit
if "%EDITOR%" neq "" (
  "%EDITOR%" "%USERPROFILE%\.peco-cd"
  goto end
)
notepad "%USERPROFILE%\.peco-cd"
goto end

:query
rem NOTE:
rem
rem If you have a problem caused by character-set, modify below part like:
rem   'type ^"%USERPROFILE%\.peco-cd^" ^| iconv -f char -t utf-8 ^| peco'
rem
for /f %%i in ('type ^"%USERPROFILE%\.peco-cd^" ^| peco') do (
  cd /d %%i
  break
)

:end

phq = peco + ghq on windows

@echo off

for /f %%i in ('ghq list -p ^| peco') do (
  cd /d %%i
  break
)

pfeed = feed reader using peco on windows

@echo off

for /f %%i in ('more +8 %~f0 ^| perl -S - ^| peco --null') do (
  start %%i
  break
)
exit /b 0
use strict;
use warnings;

use XML::Feed;
use YAML::Syck;

my $feed = XML::Feed->parse(URI->new('http://qiita.com/tags/go/feed'))
  or die XML::Feed->errstr;
for my $entry ($feed->entries) {
  print $entry->title . "\0" . $entry->link . "\n";
}

jump to gem directory managed by bundler

bundler_gems command

#!/bin/env ruby
# -*- encoding: utf-8 -*-

require 'bundler'

Bundler.load.specs.sort_by { |s| s.name }.each do |s|
  puts s.full_gem_path
end
cd $(bundler_gems | peco)

writeback result to prompt (with zle)

function peco_and_writeback() {
    BUFFER=$($LBUFFER | peco)
    CURSOR=$#BUFFER
    zle -R -c
}
zle -N peco_and_writeback
bindkey '^K' peco_and_writeback

perl local::lib (cpanm/carton) doc viewer

by cho45

perldoc $(ls local/lib/perl5/**/*.pm | peco)

perldoc browser

by jhthorsen

perldoc $(perl -MFile::Find -E'for my $d (grep{/\w/}@INC){find sub{$File::Find::name=~/$d\/?(.*)\.(?:pod|pm)$/&&say $1=~s!/!::!gr},$d;}'|peco)

zsh auto-complete from history (ctrl+R)

php builtin function browser

https://gist.github.com/yuya-takeyama/3963c64af6be037c277e

peco + snippets

http://blog.glidenote.com/blog/2014/06/26/snippets-peco-percol/

peco + vim

Put this in your .vimrc:

function! PecoOpen()
  for filename in split(system("find . -type f | peco"), "\n")
    execute "e" filename
  endfor
endfunction
nnoremap <Leader>op :call PecoOpen()<CR>

Now you can use the mapleader (backslash by default) and op to bring up a list of files in and underneath the current directory and use peco to select a file to open.

peco + git branch (git checkout to branch)

https://gist.github.com/iori-o/ee6b19d08f0fc162a647

bash + linux installer

https://gist.github.com/tobynet/eab44788828dd01c487c

peco + git (add, reset, diff, revert code)

Put this in your $HOME/.gitconfig

[alias]
  ad = "!git ls-files --modified --others --exclude-standard | peco --initial-filter=Fuzzy | xargs -r git add --"
  df = "!git diff --name-only HEAD | peco --initial-filter=Fuzzy | xargs -r git diff HEAD --"
  rs = "!git diff --name-only --cached | peco --initial-filter=Fuzzy | xargs -r git reset -q --"
  rb = "!git diff --name-only HEAD | peco --initial-filter=Fuzzy | xargs -I {} -r sh -c 'git reset -q -- {}; git checkout -- {} 2>/dev/null'"

peco + pass (copy or show)

Ensure PASSWORD_STORE_ENABLE_EXTENSIONS is set to true:

export PASSWORD_STORE_ENABLE_EXTENSIONS=true

Put the following in $HOME/.password-store/.extensions/peco.bash (or name it as you wish)

#!/usr/bin/env bash
this=$(find ~/.password-store -type f -name "*.gpg" \
           | sed 's@'"$HOME"'/\.password-store/\(.\+\?\)\.gpg@\1@' \
           | peco --prompt="[pass]")

[[ -n "$this" ]] && pass $1 $this

Then chmod a+x "$HOME/.password-store/.extensions/peco.bash".

Use pass peco to choose which password to show. To copy, you can use pass peco -c (alternatively, replace $1 with -c to copy by default).

peco + apt

Something like the function below can make installing packages you don't know the exact name much easier:

function iapt() {
    if [ -z "$1" ]; then
        echo "Usage: iapt <initial search string> - select packages on peco and they will be installed" 
    else 
        sudo apt-cache search $1 | peco | awk '{ print $1 }' | tr "\n" " " | xargs -- sudo apt-get -y install
    fi  
}

Insert file or directory at cursor position on Bash command-line

Add this code to your ~/.bashrc. Then, on the Bash command-line, hit Ctrl-X F (or Ctrl-X Ctrl-F) to fuzzy find a directory (or file) by Peco and insert it at the current cursor position:

  __peco_select_dir ()
  {
    if command -v fd >/dev/null 2>&1; then
      builtin typeset DIR="$(command fd --type directory --hidden --no-ignore --exclude .git/ --color never | peco )"
    else
      builtin typeset DIR="$(
      command find \( -path '*/\.*' -o -fstype dev -o -fstype proc \) -type f -print 2> /dev/null \
        | sed 1d \
        | cut -b3- \
        | awk '{a[length($0)" "NR]=$0}END{PROCINFO["sorted_in"]="@ind_num_asc"; for(i in a) print a[i]}' - \
        | peco
      )"
    fi

    if [[ -n $DIR ]]; then
      builtin bind '"\er": redraw-current-line'
      builtin bind '"\e^": magic-space'

      DIR=$(printf %q "$DIR")

      READLINE_LINE=${READLINE_LINE:+${READLINE_LINE:0:READLINE_POINT}}${DIR}${READLINE_LINE:+${READLINE_LINE:READLINE_POINT}}
      READLINE_POINT=$(( READLINE_POINT + ${#DIR} ))
    else
      builtin bind '"\er":'
      builtin bind '"\e^":'
    fi
  }

  __peco_select_file ()
  {
    if command -v fd >/dev/null 2>&1; then
      builtin typeset FILE="$(command fd --type file --full-path --hidden --no-ignore --exclude .git/ --color never . | peco)"
    elif command -v rg >/dev/null 2>&1; then
      builtin typeset FILE="$(rg --glob "" --files --hidden --no-ignore-vcs --iglob !.git/ --color never | peco)"
    elif command -v ag >/dev/null 2>&1; then
      builtin typeset FILE="$(ag --files-with-matches --unrestricted --skip-vcs-ignores --ignore .git/ --nocolor -g "" | peco)"
    else
      builtin typeset FILE="$(
      command find \( -path '*/\.*' -o -fstype dev -o -fstype proc \) -type f -print 2> /dev/null \
        | sed 1d \
        | cut -b3- \
        | awk '{a[length($0)" "NR]=$0}END{PROCINFO["sorted_in"]="@ind_num_asc"; for(i in a) print a[i]}' - \
        | peco
      )"
    fi

    if [[ -n $FILE ]]; then
      builtin bind '"\er": redraw-current-line'
      builtin bind '"\e^": magic-space'

      FILE=$(printf %q "$FILE")

      READLINE_LINE=${READLINE_LINE:+${READLINE_LINE:0:READLINE_POINT}}${FILE}${READLINE_LINE:+${READLINE_LINE:READLINE_POINT}}
      READLINE_POINT=$(( READLINE_POINT + ${#FILE} ))
    else
      builtin bind '"\er":'
      builtin bind '"\e^":'
    fi
  }

  bind -m emacs -x '"\e\C-xf": __peco_select_dir'
  bind -m emacs '"\C-xf": "\e\C-xf\e^\er"'
  bind -m emacs -x '"\e\C-x\C-f": __peco_select_file'
  bind -m emacs '"\C-x\C-f": "\e\C-x\C-f\e^\er"'

Insert file or directory at cursor position on ZSH command-line

Add this code to your ~/.zshrc. Then, on the ZSH command-line, hit Ctrl-X F (or Ctrl-X Ctrl-F) to fuzzy find a directory (or file) by Peco and insert it at the current cursor position:

  function peco-directories() {
    local current_buffer=$BUFFER
    if command -v fd >/dev/null 2>&1; then
      local dir="$(command fd --type directory --hidden --no-ignore --exclude .git/ --color never | peco )"
    else
      local dir="$(
        command find \( -path '*/\.*' -o -fstype dev -o -fstype proc \) -type d -print 2> /dev/null \
        | sed 1d \
        | cut -b3- \
        | awk '{a[length($0)" "NR]=$0}END{PROCINFO["sorted_in"]="@ind_num_asc"; for(i in a) print a[i]}' - \
        | peco
      )"
    fi

    if [ -n "$dir" ]; then
      dir=$(echo "$dir" | tr --delete '\n')
      dir=$(printf %q "$dir")

      BUFFER="${current_buffer}${dir}"
      CURSOR=$#BUFFER
    fi
  }

  function peco-files() {
    local current_buffer=$BUFFER
    if command -v fd >/dev/null 2>&1; then
      local file="$(command fd --type file --hidden --no-ignore --exclude .git/ --color never | peco)"
    elif command -v rg >/dev/null 2>&1; then
      local file="$(rg --glob "" --files --hidden --no-ignore-vcs --iglob !.git/ --color never | peco)"
    elif command -v ag >/dev/null 2>&1; then
      local file="$(ag --files-with-matches --unrestricted --skip-vcs-ignores --ignore .git/ --nocolor -g "" | peco)"
    else
      local file="$(
      command find \( -path '*/\.*' -o -fstype dev -o -fstype proc \) -type f -print 2> /dev/null \
        | sed 1d \
        | cut -b3- \
        | awk '{a[length($0)" "NR]=$0}END{PROCINFO["sorted_in"]="@ind_num_asc"; for(i in a) print a[i]}' - \
        | peco
      )"
    fi

    if [ -n "$file" ]; then
      file=$(echo "$file" | tr --delete '\n')
      file=$(printf %q "$file")
      BUFFER="${current_buffer}${file}"
      CURSOR=$#BUFFER
    fi
  }

  zle -N peco-directories
  bindkey '^Xf' peco-directories
  zle -N peco-files
  bindkey '^X^f' peco-files
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.