selector - A simple command line utility for dynamic pattern selection
selector [options] [<filename1> [<filename2> ...]]
selector is a command line utility for interactive real-time pattern matching. It reads the content of the specified files, and as the user types a list of strings separated by ";" (or a regexp), the display is updated in real time to show only the lines containing all the said strings (or matching the regexp).
This command was mainly designed as a way to search efficiently in the shell history, for which it is substantially better than the standard readline ^R binding. With the -v option, it injects the selected line into the tty input buffer, hence allowing the user to edit the line and execute it as a standard command.
Selector is also a handy tool to test regexps, or to display menus with many possible choices. For the latter, the -x option allows to show different strings than the ones returned.
Note that because this is an interactive command, the standard input cannot be used as one of the input files.
The selector command comes with a shell script for bash. If you add
source bash-selector.sh --hist --cdin your ~/.bashrc, it will remap M-r to the smart history search, and redefine cd so that M-c provides a smart cd history.
This script relies on readline being configured with the default emacs-style key bindings. You may have to hack a bit if you want to use it with the vi-style mode.
Keys corresponding to ASCII codes between " " and "~" add a character to the pattern string. The Backspace key, "^H" and "^?" delete the character immediately on the left of the cursor, while the Delete key and "^D" delete the character at cursor location.
The Left and Right cursor keys move the editing cursor accordingly. The Up and Down cursor keys move the selected line one entry at a time, and PageUp and PageDown move it by ten entries. The Home and End key move to the top and the bottom of the list respectively. The return key selects the current line and exits.
The keys "^A", "^E", "^U", and "^K" do somehow what they do in readline, and you can exit selector without doing anything by either interrupting the command with "^C" or by typing "^G" or the Escape key.
The key "^R" switches between the multi-substring and the regexp selection mode, and "^I" between the case-sensitive and case-insensitive modes.
-b -i -d -v -w -l ${HISTSIZE}
<fg_modeline>,<bg_modeline>,<fg_highlight>,<bg_highlight>
To use selector to search into your bash history
selector -q --bash <(history)To show a list of directories and insert a cd command to the selected one
selector -v -x "\n" <(find . -type d | awk '{ print $0"\ncd "$0 }')To select a line in a long text and write the line number in /tmp/nb
selector -o /tmp/nb -x "\n" <(awk < something.txt '{ print $0"\n"NR }')
There are modeline display problems if the pattern is too long. This program does not handle multibyte characters.
The -v option does not work on FreeBSD 8.0 since the TIOCSTI ioctl request is broken.
Written by Francois Fleuret <francois@fleuret.org> and distributed under the terms of the GNU General Public License version 3 as published by the Free Software Foundation. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.