Wednesday, October 08, 2008

When developing in python I often find "string.ljust"-ing variables important for readability. Since my editor is Vim, this is a quick one:

#!/usr/bin/env python
import sys
import re

ADJUSTMENT = sys.argv[1] if len(sys.argv) >= 2 else 20

def ljust_line(line):
try:
left, right = line.split('=')
except ValueError:
return line
else:
return '='.join([left.ljust(int(ADJUSTMENT)), re.sub(r'^\s*', ' ', right)])

for line in sys.stdin.readlines():
sys.stdout.write(ljust_line(line))
Note: Python 2.5 or greater is needed

Basicly this script left justifies python variables up to 20 chars (or to the value specified as first argument).

Usage:
copy the script to /usr/bin/ljust
chmod +x /usr/bin/ljust


In visual mode on Vim, select the text and filter through the script: !ljust

Labels: ,