Menu

Tree [e44727] master /
 History

HTTPS access


File Date Author Commit
 README.md 2014-04-07 samozrejmost samozrejmost [e44727] add config and command line info to README4
 hf.c 2014-04-07 samozrejmost samozrejmost [aed040] edit usage printout - verbose

Read Me

hf - the history filter

Description

What was that command I used some time ago where I was able to find all the big text files in a directory? I remember using find and size...

I can use the classic Ctrl-r bash reverse search and skim through dozens of find command lines or - I can use hf!

So let's run it and key in find size:

hf screenshot

Now it's much easier to choose the right line. I can move up and down in the list and then hit Enter to execute the command or Tab to edit it. I can also use regular expressions or change to case insensitive if I need to. I can edit the pattern by hitting Backspace or Ctrl + u. Enjoy!

Keyboard shortcuts

Ctrl + t - toggle case (in)sensitive

Ctrl + r - toggle the use of regular expressions

Ctrl + k/u - clear the pattern

Ctrl + g/x - quit hf

Enter - execute the highlighted line

Tab - print out the edited line so that it can be edited

Esc - quit hf, but you need to wait for one second (use C-g instead)

, - move about in the list

How to install

  • Download the hf.c file
  • Run this command to install ncurses library, compile the code, and move the binary to /usr/local/bin/ (remove the sudo command if you are already root)

sudo apt-get install libncursesw5-dev && gcc -O2 -Wall -o hf hf.c -lncursesw && sudo mv hf /usr/local/bin/hf && sudo chown root:root /usr/local/bin/hf

  • Run hf

hf

If you like hf, you can set it up as the default bash history search application like this:

hf --show-config >> $HOME/.bashrc

This will add the following configuration to bash:

$ hf --show-config

# add this configuration to ~/.bashrc
shopt -s histappend               # append new history items to .bash_history
export HISTCONTROL=ignorespace    # leading space hides commands from history
export HISTFILESIZE=10000         # increase history file size (default is 500)
export HISTSIZE=${HISTFILESIZE}   # increase history size (default is 500)
export PROMPT_COMMAND="history -a; ${PROMPT_COMMAND}"   # mem/file sync
bind '"\C-r": "\C-a\C-k hf \C-j"' # bind hf to Ctrl-r

Some notes on the config

  • PROMPT_COMMAND executes a command everytime bash prints a prompt
  • history -a automatically adds the last command to the .bash_history file so that the file always stays current
  • bind '"\C-r": "\C-a\C-k hf \C-j"' binds the Ctrl-r shortcut to hf and executes it. The hf command itself will not clutter the bash history as it has a leading space and it's not saved

hf command line

$ hf -h
Usage: hf [option] [pattern]
Shell history filter

  -f | --file <filepath>       ... specify file to read from (default: $HISTFILE)
  -p | --pattern <pattern>     ... pattern to be used for filtering (default: none)
  -i | --case-insensitive      ... make case-insensitive searches default (default: case-sensitive)
  -E | --regex-extended        ... make extended regex searches default (default: no regex)
  -G | --regex-basic           ... turn on basic regex search as default (default: no regex or extended if turned on by C-r)
  -s | --show-configuration    ... show configuration to be added to .bashrc
  -v | --verbose               ... be more verbose and print out messages to stderr
  -h | --help                  ... display this help and exit

Based heavily on: https://github.com/dvorka/hstr

Note: if you want to see error or debug messages, run 'hf' like this: hf 2> /tmp/errors.log
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.