Format Markdown Columns in Vim
Got a wonky markdown table?
1
2| Real Time | Game Time |
3|:--|:-|
4| 1 week | 1 month |
5| 3 weeks | 3 months|
6| 6 months | 2 years|
7| 1 year | 4 years|
Vim can fix it without plugins.
Just use this snippet (type : to begin inputting a command):
1:vmap <C-t> :!tr -s ' -' \|column -ts '\|' -o '\|'<Enter>j:s/ /-/g<Enter>k
Highlight the whole thing with vap then press Control + t.
Instantly, it is aligned:
1
2| Real Time | Game Time |
3|:----------|:----------|
4| 1 week | 1 month |
5| 3 weeks | 3 months |
6| 6 months | 2 years |
7| 1 year | 4 years |
NB: This method has problems with centre-aligned columns (using :---:).
Vim Run Commands
Put this in your ~/.vimrc to make the mapping permanent:
1vmap <C-t> :!tr -s ' -' \|column -ts '\|' -o '\|'<Enter>j:s/ /-/g<Enter>k