Lab Notebook

How can I autocomplete jrnl tags from the cli?

I was using obsidian for a daily work log, as I find that those type of logs seem to help tomorrow me a lot when I'm trying to remember what I did 3 days ago. The Obsidian entry was ok, but then I re-found jrnl and remembered how much fun it was to use.

jrnl is a simple cli for taking quick notes to a text file, and has a bunch of nice features to make searching for things by time, or tag pretty easy. So I kept the file in obsidian to take advantage of the sync it's doing, but have jrnl updating the entry.

One thing that would be nice is the ability to tab complete tags as I'm typing on the command line so here is the bash autocomplete syntax to do that:

_jrnl_completions() {
    local cur="${COMP_WORDS[COMP_CWORD]}"

    # Only complete tags (words starting with @)
    if [[ "$cur" == @* ]]; then
        local tags
        tags=$(jrnl --tags 2>/dev/null | awk '/^@/{print $1}')
        COMPREPLY=($(compgen -W "$tags" -- "$cur"))
    fi
}

complete -o default -F _jrnl_completions jrnl


alias js="jrnl -contains"