Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fuzzy bash completions #17

Merged
merged 5 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ instantly using a fuzzy match, with or without an interactive menu.

# Fuzzy CD

![Version](https://img.shields.io/badge/version-0.2.3-blue.svg)
![Version](https://img.shields.io/badge/version-0.2.4-blue.svg)
[![Build Status](https://github.com/DannyBen/fuzzycd/workflows/Test/badge.svg)](https://github.com/DannyBen/fuzzycd/actions?query=workflow%3ATest)

## Features
Expand All @@ -28,6 +28,7 @@ instantly using a fuzzy match, with or without an interactive menu.
- Minimal - cd to best match
- Interactive
- Interactive with `ls` preview
- Optional fuzzy bash completions.

## Prerequisites

Expand Down Expand Up @@ -61,16 +62,14 @@ You are encouraged to inspect the [setup script](setup) before running.

```
$ cd -h

fuzzycd 0.2.3

Usage:
cd DIR change working directory
cd SEARCH change working directory or show selection menu
cd -l list history with fzf
cd -e edit history file
cd -s show history file
cd -d [DIR] delete current or specified directory from history
cd -c show completions function [usage: eval "$(cd -c)"]
cd -v show version
cd -h show this help

Expand All @@ -84,6 +83,10 @@ You are encouraged to inspect the [setup script](setup) before running.
i = interactive when needed, no preview
p = interactive when needed, with ls preview

FUZZYCD_COMPLETIONS_COUNT
Maximum number of suggestions to show in bash completions
(default: 10)

Interactive Keyboard Bindings:
Del
Delete selected directory from history
Expand Down Expand Up @@ -119,10 +122,25 @@ matching directories when running in interactive mode, or you will
`cd` to the best match when running in non-interactive mode (default).


## Bash completions

To enable fuzzy bash completions, add the following line to your `~/.bashrc`:

```bash
eval "$(cd -c)"
```

This works best when tab completion is configured for inline completions, which
you can set by adding/updating the `~/.inputrc` file:

```bash
# ~/.inputrc
TAB: menu-complete
```

## Uninstall

1. Remove the `source /usr/local/bin/fuzzycd` line from your startup script(s)
(`~/.bashrc` and/or `~/.zshrc`).
1. Remove the `source /usr/local/bin/fuzzycd` line from your `~/.bashrc`.
2. Delete `/usr/local/bin/fuzzycd`.
3. Optionally, delete the history file (`~/.fuzzycd-history`).
4. Retsrat your session.
Expand Down
21 changes: 20 additions & 1 deletion fuzzycd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

fuzzycd_run() {
local version="0.2.3"
local version="0.2.4"
local histfile=${FUZZYCD_HISTORY_FILE:-"$HOME/.fuzzycd-history"}

_fzcd_is_dirname() {
Expand Down Expand Up @@ -107,6 +107,7 @@ fuzzycd_run() {
echo " cd -e edit history file"
echo " cd -s show history file"
echo " cd -d [DIR] delete current or specified directory from history"
echo " cd -c show completions function [usage: eval \"\$(cd -c)\"]"
echo " cd -v show version"
echo " cd -h show this help"
echo ""
Expand All @@ -120,11 +121,28 @@ fuzzycd_run() {
echo " i = interactive when needed, no preview"
echo " p = interactive when needed, with ls preview"
echo ""
echo " FUZZYCD_COMPLETIONS_COUNT"
echo " Maximum number of suggestions to show in bash completions"
echo " (default: 10)"
echo ""
echo "Interactive Keyboard Bindings:"
echo " Del"
echo " Delete selected directory from history"
}

# shellcheck disable=SC2016
_fzcd_show_completions() {
echo '_fuzzycd_completions() {'
echo ' local cur=${COMP_WORDS[COMP_CWORD]}'
echo ' local histfile=${FUZZYCD_HISTORY_FILE:-"$HOME/.fuzzycd-history"}'
echo ' local count=${FUZZYCD_COMPLETIONS_COUNT:-10}'
echo ' _cd' # invoke original completions
echo ' [[ $cur =~ ^(/|\.) ]] && return'
echo ' COMPREPLY+=( $(fzf --filter "$cur" --exit-0 <"$histfile" | head -n$count) )'
echo '}'
echo 'complete -o nosort -F _fuzzycd_completions cd'
}

_fzcd_handle_command() {
if _fzcd_unhandled_command "$@"; then
_fzcd_chdir "$@"
Expand All @@ -143,6 +161,7 @@ fuzzycd_run() {
"-v") _fzcd_show_version ;;
"-e") _fzcd_edit_histfile ;;
"-s") _fzcd_show_histfile ;;
"-c") _fzcd_show_completions ;;
"-d")
shift
_fzcd_delete_dir "$@"
Expand Down
1 change: 1 addition & 0 deletions op.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
shellcheck: shellcheck setup fuzzycd && echo PASS
shfmt: shfmt -d -i 2 -ci setup fuzzycd && echo PASS
codespell: codespell
test: test/approve
9 changes: 9 additions & 0 deletions test/approvals/cd_c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
_fuzzycd_completions() {
local cur=${COMP_WORDS[COMP_CWORD]}
local histfile=${FUZZYCD_HISTORY_FILE:-"$HOME/.fuzzycd-history"}
local count=${FUZZYCD_COMPLETIONS_COUNT:-10}
_cd
[[ $cur =~ ^(/|\.) ]] && return
COMPREPLY+=( $(fzf --filter "$cur" --exit-0 <"$histfile" | head -n$count) )
}
complete -o nosort -F _fuzzycd_completions cd
7 changes: 6 additions & 1 deletion test/approvals/cd_h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fuzzycd 0.2.3
fuzzycd 0.2.4

Usage:
cd DIR change working directory
Expand All @@ -7,6 +7,7 @@ Usage:
cd -e edit history file
cd -s show history file
cd -d [DIR] delete current or specified directory from history
cd -c show completions function [usage: eval "$(cd -c)"]
cd -v show version
cd -h show this help

Expand All @@ -20,6 +21,10 @@ Environment Variables:
i = interactive when needed, no preview
p = interactive when needed, with ls preview

FUZZYCD_COMPLETIONS_COUNT
Maximum number of suggestions to show in bash completions
(default: 10)

Interactive Keyboard Bindings:
Del
Delete selected directory from history
2 changes: 1 addition & 1 deletion test/approvals/cd_v
Original file line number Diff line number Diff line change
@@ -1 +1 @@
fuzzycd 0.2.3
fuzzycd 0.2.4
4 changes: 4 additions & 0 deletions test/approve
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ context "when the shell is interactive"
it "shows version"
approve "cd -v"

describe "cd -c"
it "shows completions function"
approve "cd -c"

describe "cd DIR"
it "adds it to history"
cd tmp/one/two > /dev/null
Expand Down