This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
if [ ! -z "${1:-""}" ]; then | |
case "$1" in | |
"last"|"-") | |
$EDITOR ~/notes/$(ls ~/notes | grep -E '\.md$' | tail -n1) | |
;; | |
"search") | |
shift | |
files=$(ag "$@" ~/notes -l) | |
if [ ! -z "$files" ]; then | |
if [ $(wc -l <<< "$files") = "1" ]; then | |
$EDITOR -p -- $files | |
else | |
files=$(while read file; do basename $file; done <<< "$files" | fzf -m --preview 'ag '$@' ~/notes/{} --color ' | while read file; do echo "$HOME/notes/$file"; done) | |
if [ ! -z "$files" ]; then | |
$EDITOR -p -- $files | |
fi | |
fi | |
fi | |
;; | |
*) | |
>&2 echo "note [search|last]" | |
exit 1 | |
;; | |
esac | |
exit | |
fi | |
file=$(mktemp) | |
mv "$file"{,.md} | |
file="${file}.md" | |
mtime_in=$(date -r "$file" "+%s") | |
if ${EDITOR:-/usr/bin/vim} $file; then | |
mtime_out=$(date -r "$file" "+%s") | |
if [ $mtime_in -eq $mtime_out ]; then | |
>&2 echo 'No changes to file. Discarding note.' | |
rm $file | |
exit 1 | |
fi | |
else | |
>&2 echo '$EDITOR returned a non-zero exit code. Discarding note.' | |
rm $file | |
exit 1 | |
fi | |
slug=$(sed '/[^[:blank:]]/q;d' < "$file" | | |
sed -e 's/[^a-zA-Z0-9]/-/g' | | |
sed -E 's/[\-]+/-/g' | | |
sed -e 's/^-//g' | | |
sed -e 's/-$//g' | | |
tr '[[:upper:]]' '[[:lower:]]') | |
dest="$HOME/notes/$(date -r "$file" "+%Y%m%d")-${slug:-untitled-note}" | |
suffix="" | |
while [ -e "${dest}${suffix}.md" ]; do | |
suffix=$((${suffix:-0} - 1)) | |
done | |
mv "$file" "${dest}${suffix}.md" | |
echo "Stored note at \"${dest}${suffix}.md\"." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi!
Thanks for this nicety. I changed it to use
ripgrep
instead ofag
(just a matter of taste) andbat
as previewer.Also, now the path to the notes is configurable via the
NOTES_PATH
env variable.https://gist.github.com/leophys/38093e4093e49a3bb0e0cdb584ec5679