#!/usr/bin/env bash # note (clean + dedupe + git sync) # needs: fzf, rg # written mostly by chatgpt (ai) in april 2026 # brex production set -euo pipefail # set -x NOTES_DIR="${NOTES_DIR:-$HOME/notes}" #EDITOR="${EDITOR:-vim}" # disabled # try multiple for ed in "${EDITOR:-}" hx vim nano; do if command -v "$ed" >/dev/null 2>&1; then EDITOR="$ed" break fi done # this should now also work with older versions of fzf, if not comment out next line: export FZF_DEFAULT_OPTS="--pointer='>' --color=fg:-1,bg:-1,hl:-1,fg+:-1,bg+:-1,hl+:-1,info:-1,prompt:-1,spinner:-1,pointer:-1,marker:-1,border:-1" # deps for cmd in fzf rg; do if ! command -v "$cmd" &>/dev/null; then echo "Error: $cmd is required" >&2 exit 1 fi done mkdir -p "$NOTES_DIR" today() { date +"%Y-%m-%d" } slugify() { local input="${1:-}" input="$(echo "$input" \ | tr '[:upper:]' '[:lower:]' \ | tr -cd 'a-z0-9 -' \ | tr ' ' '-' \ | sed 's/-\+/-/g' \ | sed 's/^-//' \ | sed 's/-$//')" echo "$input" } open_note_fzf() { local file file=$( find "$NOTES_DIR" -maxdepth 1 -type f -name '*.md' \ -printf '%T@ %f\n' \ | sort -nr \ | cut -d' ' -f2- \ | fzf --prompt="open> " \ --preview='cat "'"$NOTES_DIR"'"/{}' \ --height=80% \ --reverse ) [[ -z "$file" ]] && return "$EDITOR" "$NOTES_DIR/$file" } # unlike open_note_fzf, search_notes is curently not limited to *.md files search_notes() { local query="${1:-}" local selection file line selection=$( rg -i -m1 --no-heading --line-number --color=never "$query" "$NOTES_DIR" \ | fzf \ --delimiter : \ --prompt="search> " \ --preview='cat {1}' \ --bind "change:reload:rg -i -m1 --no-heading --line-number --color=never {q} '$NOTES_DIR' || true" \ --phony \ --query "$query" \ --height=80% \ --reverse ) [[ -z "$selection" ]] && return file="${selection%%:*}" line="${selection#*:}" line="${line%%:*}" "$EDITOR" "$file" "+$line" } create_note() { local text="$*" local title slug file [[ -z "$text" ]] && return title="$text" slug="$(slugify "$text" | cut -d- -f1-5)" [[ -z "$slug" ]] && slug="note" file="$NOTES_DIR/$(today)-$slug.md" if [[ -f "$file" ]]; then "$EDITOR" "$file" return fi cat > "$file" < "$file" <