Superpowering history_search in the shell

2017/10/20

In the last few days I read a lot about fzf and how it allows the user to fuzzy-find everything. Even though all those fancy blog posts stir my geeky side, I decided to bring some attention to peco and how I superpower my linux setup with this handy tool.

What is peco?

Peco is a simple Go executable that can be downloaded from the offical releases page at https://github.com/peco/peco/releases and dropped in the $PATH.

Let’s define a custom keybinding for the shell using peco to search my command history. I’m a big fan of fish and the following snippet defines a custom keybinding on Ctrl-r:

function fish_user_key_bindings
    bind \cr 'peco_select_history (commandline -b)'
end

The function peco_select_history is defined as follows:

function peco_select_history
  if test (count $argv) = 0
    set peco_flags --layout=bottom-up
  else
    set peco_flags --layout=top-down --query "$argv"
  end

  history|peco $peco_flags|read foo

  if [ $foo ]
    commandline $foo
  else
    commandline ''
  end
end

Now whenever I press Ctrl-r, I have a lightning fast, super easy to use and powerful search for my shell.

Update: The video is a bit too big for my blog layout. Just watch it in fullscreen to see me switching between search modes.