github twitter linkedin email rss
Peco: the most productive command line tool
Jan 23, 2017
2 minutes read

Introduction to Peco

Peco is one of the command line tools to achieve incremental search on your console. It is a great invention because it much improved my productivity.

before

For example, how would you select commands which you have run on your console? Most of people just push up key to look it for or either may run history | grep command to achieve that. However, these ways don’t lead you to a command what you want as fast as you want.

Part 1: History Selection

after

History selection is the most productive part for me. You can incremental search to look for commands which you have run before. After you chose a command, it would be executed immediately. Shell’s auto complete isn’t much enough because there is no way to look over all the history. Another way to achieve this thing is history | grep, however, it’s also not much enough because you still need to choose one of the history and probably need to copy and paste.

In my case, I binded Ctrl+R to the function through .zshrc below. So you don’t have to even anything to look for it!

# Peco history selection
function peco-history-selection() {
   local tac
   if which tac > /dev/null; then
     tac="tac"
   else
     tac="tail -r"
   fi
   BUFFER=$(history -1000 | eval $tac | cut -c 8- | peco --query "$LBUFFER")
   CURSOR=$#BUFFER
}

zle -N peco-history-selection
bindkey '^R' peco-history-selection

Part 2: Use “peco” instead of “grep”

ps

You can just replace “grep” to “peco” in various situation. Let’s say you’re looking for a process which you run through ps -aux command. In theory, you might do like ps -aux | grep 1234 to look for a process. However, you can do ps -aux | peco instead. Then you would just type a process id which you’re looking for. What’s the advantage of this way is you don’t have to run the command again even if you search in another process id.

Conclusion

Peco has a pretty good performance because it’s written in Go. Also, it’s pretty effective to solve daily small problems because you can pass everything you want to peco through pipe. It’s pretty effective to combine commands like git, docker, etc.


Back to posts


comments powered by Disqus