Last active
July 22, 2020 17:16
-
-
Save Jun-Dai/1dd3b154918d37a76bd515efafa25acf to your computer and use it in GitHub Desktop.
Zsh script to use vim + Dropbox for random notes.
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
# USAGE: | |
# Set NOTE_FOLDER to where you want to store the notes | |
# Add a line to your .zshrc with the appropriate path: | |
# source path/to/open-note.sh | |
# note -> lists all notes | |
# note -n foo -> creates a new note named 2020-07-10-foo.md, where 2020-07-10 is today's date | |
# (if there's a note today already with the same name, it will just open it) | |
# note foo -> if there's a single note with the substring "foo", it will open it | |
# (if there are none, it will fail. If there is more than one it will fail--and it will list the matching notes) | |
# note -l foo -> it will open the most recent note with the substring "foo" | |
# | |
# -c will open it using code (VS Code). Else it will open it using whatever $NOTE_EDITOR is set to (default: vim) | |
# -p just prints the name of the file (if you want to use it for something else) | |
# -C copies the contents of the file to the clipboard using pbcopy (in case you just want to paste the note somewhere) | |
# -d puts out some useful debugging for this dang script. | |
export NOTE_FOLDER="$HOME/Dropbox (Personal)/vim/notes" | |
new-notefile() | |
{ | |
echo "$NOTE_FOLDER/`date +%Y-%m-%d`-$1.md" | |
} | |
note() { | |
if [[ ! -v NOTE_EDITOR ]]; then NOTE_EDITOR=vim; fi | |
unset list_notes use_last create_new just_print_path copy_to_clipboard debug_mode | |
while getopts "dlLnpcCh" opt; do | |
case ${opt} in | |
l ) | |
local use_last=true ;; | |
L ) | |
local list_notes=true ;; | |
n ) | |
local create_new=true ;; | |
p ) | |
local just_print_path=true ;; | |
c ) | |
local NOTE_EDITOR=code ;; | |
d ) | |
local debug_mode=true ;; | |
C ) | |
local copy_to_clipboard=true ;; | |
h ) | |
echo "Usage:" | |
echo " note -- lists all notes" | |
echo " note foo -- if there's one note with the name containing 'foo', opens that note" | |
echo " note -L foo -- prints the paths for all notes containing 'foo'" | |
echo " note -n foo -- creates a new note named 'foo'" | |
echo " -c -- opens using \`code\` (VS Code) instead of vim. Or set NOTE_EDITOR if you want to use another editor" | |
echo " -C -- copies the contents of the note to the clipboard with pbcopy instead of opening it for edit" | |
echo " -l -- opens the most recent matching note (useful if the substring matches multiple notes" | |
echo " -p -- prints out the file path rather than opening the file" | |
return 0 | |
;; | |
\? ) | |
echo "Invalid option(s) specified: $@" 1>&2 | |
return 1 | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
if [[ -v debug_mode ]]; then | |
echo "Use last: $use_last" | |
echo "Create new: $create_new" | |
echo "Just print: $just_print_path" | |
echo "copy to clipboard: $copy_to_clipboard" | |
echo "NOTE_EDITOR: $NOTE_EDITOR" | |
echo "Arg1: $1, Arg2: $2, Arg3: $3, Arg4: $3, Arg@: $@" | |
fi | |
if [[ -v create_new ]]; then | |
if [[ -n $1 ]]; then | |
$NOTE_EDITOR "`new-notefile $1`" | |
return 0 | |
else | |
>&2 echo "Cannot create a new notefile if you don't specify a name for the note." | |
return 1 | |
fi | |
fi | |
if [[ -n $1 ]]; then | |
matches=`ls -r1 $NOTE_FOLDER | grep -i $1` | |
if [[ -v use_last ]]; then matches=`echo $matches | head -1`; fi | |
if [[ -v debug_mode ]]; then echo "matches: $matches"; fi | |
if [[ -v list_notes ]]; then | |
echo "$matches" | |
return 0 | |
fi | |
else | |
if [[ -v copy_to_clipboard ]]; then | |
2&> echo "I can't copy a file to clipboard if you don't specify a file!" | |
return 1 | |
else | |
echo "`ls -r1 $NOTE_FOLDER`" | |
return 0 | |
fi | |
fi | |
notecount=`echo -n "$matches" | grep -c '^'` | |
if [[ -v debug_mode ]]; then echo "notecount: $notecount"; fi | |
if [[ $notecount -eq 0 ]]; then | |
>&2 echo "No notes matching \"$1\" found!" | |
>&2 echo " Use -n if you want to create a new note named $1" | |
return 1 | |
elif [[ $notecount -gt 1 ]]; then | |
>&2 echo "Found $notecount notes matching \"$1\"." | |
>&2 echo " Use a more specific pattern, or the -l option if you just want the last one." | |
echo $matches | |
return 2 | |
else | |
if [[ -v just_print_path ]]; then | |
echo "$NOTE_FOLDER/$matches" | |
elif [[ -v copy_to_clipboard ]]; then | |
cat "$NOTE_FOLDER/$matches" | pbcopy | |
echo "Copied $NOTE_FOLDER/$matches to Clipboard! `wc -l < $NOTE_FOLDER/$matches` lines" | |
else | |
$NOTE_EDITOR $NOTE_FOLDER/$matches | |
fi | |
fi | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment