Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 50 lines (44 sloc) 1.22 KB
#!/bin/sh
# Copies a previous transaction to today.
#
# Select a previous transaction by fuzzy matching on the titles. This
# transaction is then copied to the end of the journal, uncleared and
# set to the current date. Finally, it opens the end of the file in
# $EDITOR.
#
# Bonus over `hledger add`: interactive matching it easier, and
# transaction formatting is retained because it's just a copy.
#
# Depends on:
# - fzf (https://github.com/junegunn/fzf)
# - ed
ledger="${1:-$LEDGER_FILE}"
# fuzzy select a transaction title
selection="$(sed -n 's/....-..-..\( [*!]\)\? //p' "$ledger" | sort | uniq | fzf)"
[ -z "$selection" ] && clear && exit 0 # quit if escaped
cat <<HERE | ed --silent "$ledger"
# append a newline for easier searching
\$a
.
# yank matching transaction (closed by empty line)
?^....-..-..\( [*!]\)\? ${selection}\$?;/^\$/y
# select line below last none-empty line
$
?^..*?
+
# put transaction
x
# replace date with today
?^\$?
+s/....-..-..\( [*!]\)\?/$(date +'%Y-%m-%d')/
# delete empty lines at end and quit
/^\$/,\$d
wq
HERE
# review change: open end of the file (if I know how)
case "$EDITOR" in
vi*) "$EDITOR" +\$ "$ledger" ;;
emacs) "$EDITOR" "$ledger" -f end-of-buffer ;;
*) "$EDITOR" "$ledger" ;;
esac
clear
You can’t perform that action at this time.