Skip to content

Repository files navigation

selecta - Interactively search and select entries from your bash/zsh history.

Python package

This is a Python3 clone of François Fleuret's excellent selector tool.

Screencast

Usage

Just type some characters and see which entries match your words.

You can search for whole sentences by prefixing your search with a double quote.

Use up and down arrows to navigate the list.

Escape/Backspace on the result list returns to the search input.

Escape on the search input closes selecta.

Press Enter to copy the selected entry to the console.

CTRL+a toggles case sensitivity

CTRL+r toggles regex search

Installation

pip install selecta

Install the keyboard shortcut Alt+{key}:

selecta_add_keybinding {the alt key you want to use}

This appends a shell wrapper function and keybinding to your ~/.bashrc or ~/.zshrc.

How it works (TIOCSTI-free): Instead of using the legacy TIOCSTI ioctl (disabled on Linux 6.2+), selecta now uses the same approach as fzf: the TUI draws to /dev/tty, and the selected command is printed to stdout. A shell wrapper function captures this output and uses shell-native readline commands to place the text on your prompt.

The -q/--query flag pre-fills the search box with the current command line, so if you've already typed part of a command before pressing the hotkey, selecta starts searching with it.

Manual shell setup

If you prefer to set up the integration manually, add the following to your rc file:

Bash (~/.bashrc):

# selecta shell integration (TIOCSTI-free)
selecta_insert() {
  local result
  result=$(selecta -b -y -p -q "$READLINE_LINE" <(history))
  if [[ -n "$result" ]]; then
    READLINE_LINE="$result"
    READLINE_POINT=${#result}
  fi
}
bind -x '"\C-[s": selecta_insert'

Zsh (~/.zshrc):

# selecta shell integration (TIOCSTI-free)
selecta_insert() {
  local result
  result=$(selecta -z -y -p -q "$BUFFER" <(history 0))
  if [[ -n "$result" ]]; then
    BUFFER="$result"
    CURSOR=${#BUFFER}
  fi
}
zle -N selecta_insert
bindkey '^[s' selecta_insert

Fish (~/.config/fish/config.fish):

# selecta shell integration (TIOCSTI-free)
function selecta_insert
  set -l result (PYTHON_GIL=1 selecta -z -y -p -q (commandline) (history | cut -d " " -f 1 --complement | psub))
  if test -n "$result"
    commandline -r "$result"
  end
end
bind \es selecta_insert

Replace s with your preferred key. After adding, run source ~/.bashrc (or source ~/.zshrc, or source ~/.config/fish/config.fish) or open a new terminal.

Legacy TIOCSTI mode

If you're on an older kernel and prefer the old behavior, selecta still supports TIOCSTI as a fallback (just omit the -p flag). To re-enable TIOCSTI on Linux < 6.2:

sudo sysctl -w dev.tty.legacy_tiocsti=1

Upgrade from older version to 0.2.x

Delete your old keybinding from .bashrc/.zshrc/config.fish and register the new version with:

selecta_add_keybinding {key}

--help output

    usage: selecta [-h] [-i] [-b] [-z] [-r] [-a] [-d] [-y] [-p] [infile]

    positional arguments:
      infile                the file which lines you want to select eg. <(history)

    optional arguments:
      -h, --help            show this help message and exit
      -i, --reverse-order   reverse the order of the lines
      -b, --remove-bash-prefix
                            remove the numeric prefix from bash history
      -z, --remove-zsh-prefix
                            remove the time prefix from zsh history
      -r, --regexp          start in regexp mode
      -a, --case-sensitive  start in case-sensitive mode
      -d, --remove-duplicates
                            remove duplicated lines
      -y, --highlight-matches
                            highlight the part of each line which matches the
                            substrings or regexp
      -p, --print           print the selected command to stdout
                            (use with shell wrapper for TIOCSTI-free operation)
      -q, --query           initial search string (e.g. the current shell
                            command line)
      -v, --version         print selecta version

About

Interactively select an entry from your bash history.

Resources

Stars

14 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages