Skip to content

Commit b52e0b5

Browse files
author
Linus Wallgren
committed
bash completion support
1 parent 94edfb6 commit b52e0b5

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ curl https://cdn.rawgit.com/pimterry/notes/v0.1.2/notes > /usr/local/bin/notes &
2929

3030
By default your notes live in ~/notes, but you can change that to anywhere you like by setting the `$NOTES_DIRECTORY` environmental variable.
3131

32+
### Bash completion
33+
34+
If you want bash autocompletion copy the completion script into the bash completion directory. The bash completion directory is `/usr/share/bash-completion/completions/` on a typical debian jessie install, you can you can get the bash completion directory by running the following command:
35+
36+
pkg-config --variable=completionsdir bash-completion
37+
38+
Installing the completions might be as follows:
39+
40+
```bash
41+
curl https://cdn.rawgit.com/pimterry/notes/v0.1.1/notes.bash_completion > /usr/share/bash-completion/completions/notes
42+
```
43+
3244
## How do I use it?
3345

3446
### `notes new <note-name>`
@@ -62,8 +74,8 @@ All the above works. Here's what's coming next:
6274
- [ ] Combining find and grep, to match either one
6375
- [ ] More interesting and nicer looking file/grep search result formatting (...only when not piping?)
6476
- [ ] Make the file extension optional
65-
- [ ] Bash/zsh command autocompletion
66-
- [ ] Bash/zsh note name autocompletion
77+
- [ ] zsh command autocompletion
78+
- [ ] zsh note name autocompletion
6779
- [ ] Interactive mode? `notes` could open a scrollable list of notes, open your editor when you pick one, and reappear after you close it.
6880
- [ ] Tree view.
6981
- [ ] Easy way to see short notes snippets in find/grep/tree? Could be option (`--snippets`) or by piping to a command (`notes find | notes snippets`). Maybe call it `head`?

notes.bash_completion

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
3+
_notes_complete_notes() {
4+
local configured_dir=${NOTES_DIRECTORY%/} # Remove trailing slashes
5+
local notes_dir="${configured_dir:-$HOME/notes}"
6+
local items=($(compgen -f "$notes_dir/$1"))
7+
for item in "${items[@]}"; do
8+
[[ -d $item ]] && item="$item/"
9+
local filename=${item#$notes_dir/}
10+
COMPREPLY+=("${filename%.md}")
11+
done
12+
}
13+
14+
_notes()
15+
{
16+
local cur
17+
readonly commands="new find grep open"
18+
19+
COMPREPLY=() # Array variable storing the possible completions.
20+
cur=${COMP_WORDS[COMP_CWORD]}
21+
22+
if [[ $COMP_CWORD -gt 1 ]]; then
23+
case "${COMP_WORDS[1]}" in
24+
new)
25+
_notes_complete_notes "$cur"
26+
;;
27+
find)
28+
_notes_complete_notes "$cur"
29+
;;
30+
grep)
31+
;;
32+
open)
33+
_notes_complete_notes "$cur"
34+
;;
35+
esac
36+
else
37+
compopt +o nospace
38+
COMPREPLY=($(compgen -W "${commands}" -- "${cur}"))
39+
fi
40+
return 0
41+
}
42+
43+
complete -o filenames -o nospace -F _notes notes

0 commit comments

Comments
 (0)