--- title: Format Markdown Columns in Vim tags: - writing - vim - markdown --- Got a wonky markdown table? ```markdown | Real Time | Game Time | |:--|:-| | 1 week | 1 month | | 3 weeks | 3 months| | 6 months | 2 years| | 1 year | 4 years| ``` Vim can fix it without plugins. Just use this snippet (type `:` to begin inputting a command): ```vim :vmap :!tr -s ' -' \|column -ts '\|' -o '\|'j:s/ /-/gk ``` Highlight the whole thing with `vap` then press `Control + t`. Instantly, it is aligned: ```markdown | Real Time | Game Time | |:----------|:----------| | 1 week | 1 month | | 3 weeks | 3 months | | 6 months | 2 years | | 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: ```vim vmap :!tr -s ' -' \|column -ts '\|' -o '\|'j:s/ /-/gk ```