Vim Macros for Beancount
57 points
3 days ago
| 3 comments
| tangled.sh
| HN
xarcolade
3 days ago
[-]
Hello! Sharing on HN is a first for me. When I wrote this I thought I was producing something with a steady enough on-ramp that someone could follow along with a little bit of sustained curiosity. After sharing it with some friends I realise it might still be a steeper/poorly constructed ramp than I initially thought. Either way I'd love to hear from you if you found this interesting!
reply
vanous
3 days ago
[-]
Very cool! A while ago i did something similar and tried to learn vim more in depth by creating some more complex macros - several of them, to convert some text snippets into markdown. Problem was, that several months later i could not exactly remember some details of these "reusable" macros - where exactly to place cursor when starting them, the order of execution and so on. Thankfully, vim has the amazing ability to run commands/script on text selection, so i rewrote my macros in a scripting language. It has several bonus points: i can store them in git and track changes, code can be self-documented via comments. My macros were not too complex, but still, using a proper scripting ended up being much better.
reply
aidenn0
3 days ago
[-]
Macroing Tip: whenever possible start a macro with a motion that will move the cursor to a known location. E.g. for line-oriented macros use "0" and for paragraph oriented macros use "}{". Then the macro will work regardless of where you put the cursor.
reply
xarcolade
3 days ago
[-]
Perfect! Great advice
reply
xarcolade
3 days ago
[-]
Insightful! I have had the same issue with forgetting where exactly I need the cursor. I did not know you could run script/commands on text selection, I will definitely look into this. The extent of my vimscript journey so far is directly making system() calls to external scripts, and I've been using the ability to Ctrl-R while entering a :command to dump yanked text as a crutch. Your approach sounds much more sensible.
reply
vanous
3 days ago
[-]
Glad you find it useful. Below is an example script to demonstrate the concept. It is awesomely powerful. In vim, select some text and do this:

  :'<,'>! ./example.py

  #!/bin/env python3
  #:'<,'>! ./example.py # ← this is how to use it
  import sys
  data = sys.stdin.readlines()
  for l in data:
    l = l.replace("a", "e").rstrip()
    print(l)
reply
xz18r
3 days ago
[-]
Also check out beancount-mode for emacs: https://github.com/beancount/beancount-mode
reply
xarcolade
3 days ago
[-]
I've been struggling to on-ramp and sustain using Emacs for a while now. The paradigm shift from vim for me is frustratingly vast. I know I just need to give it the same patience I gave vim many years ago :)
reply
FriarTech
3 days ago
[-]
Was in the same boat as you a couple of years ago. Now I use both daily. eMacs for GTD and vim for coding. I don’t like using a system without both installed :)
reply
djhworld
3 days ago
[-]
I really wanted to like the vim-beancount plugin but it's just too buggy for me, so I've always just come crawling back to beancount-mode in emacs. It's the only thing I use emacs for and I use evil mode for vim keybindings :)
reply
njt
3 days ago
[-]
Just wanted to chime in on my beancount workflow, which you may also want to check out.

I also use vim, and I use it a bit to edit my beancount files, but I mostly use fava, a most excellent web interface for beancount. In addition to having a built in editor (which does formatting and can catch errors and show you exactly where they are), it allows you to quickly add new transactions based on old ones — the dialog box allows you to choose from previously used accounts to speed up input. It’s a lifesaver. I don’t think I would have continued using beancount long term if it wasn’t for fava.

Just about the only negative thing I can say about fava is that it does formatting slightly differently from bean-format. I actually prefer it to beancount, but there are some cases where I prefer the native formatting (like when declaring/opening accounts). So for some months, I will switch from one to another. A minor nitpick, but it’s slightly annoying.

reply
johntash
3 days ago
[-]
Fava is great. My only problem is that I was lazy and haven't imported anything for at least 2 years, and now it feels too daunting to try and catch up.
reply