Use Ultisnips to insert ING Direct Paywave rebate records for ledger

2016年02月08日

It has been realy a long time since my last article. Anyway, I am here again. Now in Australia, I think I should start to write in English to expose the content to a broader scope of audience.

I have been using ledger for personal accounting for 10 months now. It amazes myself that I could persist in putting every cents spent down in the ledger file for such a long time.

Recently, I have got a ING Direct Visa Debit Card and like its idea of 2% of cash rebate for every Paywave transaction lower than AUD 100. It quickly comes now my default card for everything. However, it causes extra work during my updating ledger book, I am tired of regularly checking my bank account for updates of cash rebates so that I can insert them into the ledger book.

Luckily, Ultisnips has the functionality of using inline Python/Shell interpolation, and spending a little of time I got the problem solved.

global !p
def ing_paywave_rebate(amount):
    return round(float(amount) * 2.0 / 100, 2)
endglobal

snippet pay "Expense paid with ING Paywave"
${1:`date +%Y`}/${2:`date +%m`}/${3:`date +%d`} * ${4:Expense title}
    ${5:Expenses}:${6:Grocery}      ${7:$} ${8:0}
    Income:INGRebate        $ -`!p snip.rv=ing_paywave_rebate(t[8])`
    Assets:ING:Everyday

endsnippet

The code is not difficult. The magic !p snip.rv= sentence tells Ultisnips here the return value of the field is set to be the result from a Python function. That’s it.

Now I can use pay to trigger this snippet, the rebate amount will be automatically populated when expense is input.