Skip to content

Commit 21a41d2

Browse files
authored
Merge pull request #17 from DannyBen/add/completions-function
Add fuzzy bash completions
2 parents 758b4b1 + a54676e commit 21a41d2

File tree

7 files changed

+65
-9
lines changed

7 files changed

+65
-9
lines changed

README.md

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ instantly using a fuzzy match, with or without an interactive menu.
1010

1111
# Fuzzy CD
1212

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

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

3233
## Prerequisites
3334

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

6263
```
6364
$ cd -h
64-
65-
fuzzycd 0.2.3
66-
6765
Usage:
6866
cd DIR change working directory
6967
cd SEARCH change working directory or show selection menu
7068
cd -l list history with fzf
7169
cd -e edit history file
7270
cd -s show history file
7371
cd -d [DIR] delete current or specified directory from history
72+
cd -c show completions function [usage: eval "$(cd -c)"]
7473
cd -v show version
7574
cd -h show this help
7675
@@ -84,6 +83,10 @@ You are encouraged to inspect the [setup script](setup) before running.
8483
i = interactive when needed, no preview
8584
p = interactive when needed, with ls preview
8685
86+
FUZZYCD_COMPLETIONS_COUNT
87+
Maximum number of suggestions to show in bash completions
88+
(default: 10)
89+
8790
Interactive Keyboard Bindings:
8891
Del
8992
Delete selected directory from history
@@ -119,10 +122,25 @@ matching directories when running in interactive mode, or you will
119122
`cd` to the best match when running in non-interactive mode (default).
120123

121124

125+
## Bash completions
126+
127+
To enable fuzzy bash completions, add the following line to your `~/.bashrc`:
128+
129+
```bash
130+
eval "$(cd -c)"
131+
```
132+
133+
This works best when tab completion is configured for inline completions, which
134+
you can set by adding/updating the `~/.inputrc` file:
135+
136+
```bash
137+
# ~/.inputrc
138+
TAB: menu-complete
139+
```
140+
122141
## Uninstall
123142

124-
1. Remove the `source /usr/local/bin/fuzzycd` line from your startup script(s)
125-
(`~/.bashrc` and/or `~/.zshrc`).
143+
1. Remove the `source /usr/local/bin/fuzzycd` line from your `~/.bashrc`.
126144
2. Delete `/usr/local/bin/fuzzycd`.
127145
3. Optionally, delete the history file (`~/.fuzzycd-history`).
128146
4. Retsrat your session.

fuzzycd

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22

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

77
_fzcd_is_dirname() {
@@ -107,6 +107,7 @@ fuzzycd_run() {
107107
echo " cd -e edit history file"
108108
echo " cd -s show history file"
109109
echo " cd -d [DIR] delete current or specified directory from history"
110+
echo " cd -c show completions function [usage: eval \"\$(cd -c)\"]"
110111
echo " cd -v show version"
111112
echo " cd -h show this help"
112113
echo ""
@@ -120,11 +121,28 @@ fuzzycd_run() {
120121
echo " i = interactive when needed, no preview"
121122
echo " p = interactive when needed, with ls preview"
122123
echo ""
124+
echo " FUZZYCD_COMPLETIONS_COUNT"
125+
echo " Maximum number of suggestions to show in bash completions"
126+
echo " (default: 10)"
127+
echo ""
123128
echo "Interactive Keyboard Bindings:"
124129
echo " Del"
125130
echo " Delete selected directory from history"
126131
}
127132

133+
# shellcheck disable=SC2016
134+
_fzcd_show_completions() {
135+
echo '_fuzzycd_completions() {'
136+
echo ' local cur=${COMP_WORDS[COMP_CWORD]}'
137+
echo ' local histfile=${FUZZYCD_HISTORY_FILE:-"$HOME/.fuzzycd-history"}'
138+
echo ' local count=${FUZZYCD_COMPLETIONS_COUNT:-10}'
139+
echo ' _cd' # invoke original completions
140+
echo ' [[ $cur =~ ^(/|\.) ]] && return'
141+
echo ' COMPREPLY+=( $(fzf --filter "$cur" --exit-0 <"$histfile" | head -n$count) )'
142+
echo '}'
143+
echo 'complete -o nosort -F _fuzzycd_completions cd'
144+
}
145+
128146
_fzcd_handle_command() {
129147
if _fzcd_unhandled_command "$@"; then
130148
_fzcd_chdir "$@"
@@ -143,6 +161,7 @@ fuzzycd_run() {
143161
"-v") _fzcd_show_version ;;
144162
"-e") _fzcd_edit_histfile ;;
145163
"-s") _fzcd_show_histfile ;;
164+
"-c") _fzcd_show_completions ;;
146165
"-d")
147166
shift
148167
_fzcd_delete_dir "$@"

op.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
shellcheck: shellcheck setup fuzzycd && echo PASS
22
shfmt: shfmt -d -i 2 -ci setup fuzzycd && echo PASS
3+
codespell: codespell
34
test: test/approve

test/approvals/cd_c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
_fuzzycd_completions() {
2+
local cur=${COMP_WORDS[COMP_CWORD]}
3+
local histfile=${FUZZYCD_HISTORY_FILE:-"$HOME/.fuzzycd-history"}
4+
local count=${FUZZYCD_COMPLETIONS_COUNT:-10}
5+
_cd
6+
[[ $cur =~ ^(/|\.) ]] && return
7+
COMPREPLY+=( $(fzf --filter "$cur" --exit-0 <"$histfile" | head -n$count) )
8+
}
9+
complete -o nosort -F _fuzzycd_completions cd

test/approvals/cd_h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
fuzzycd 0.2.3
1+
fuzzycd 0.2.4
22

33
Usage:
44
cd DIR change working directory
@@ -7,6 +7,7 @@ Usage:
77
cd -e edit history file
88
cd -s show history file
99
cd -d [DIR] delete current or specified directory from history
10+
cd -c show completions function [usage: eval "$(cd -c)"]
1011
cd -v show version
1112
cd -h show this help
1213

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

24+
FUZZYCD_COMPLETIONS_COUNT
25+
Maximum number of suggestions to show in bash completions
26+
(default: 10)
27+
2328
Interactive Keyboard Bindings:
2429
Del
2530
Delete selected directory from history

test/approvals/cd_v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
fuzzycd 0.2.3
1+
fuzzycd 0.2.4

test/approve

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ context "when the shell is interactive"
3333
it "shows version"
3434
approve "cd -v"
3535

36+
describe "cd -c"
37+
it "shows completions function"
38+
approve "cd -c"
39+
3640
describe "cd DIR"
3741
it "adds it to history"
3842
cd tmp/one/two > /dev/null

0 commit comments

Comments
 (0)