diff --git a/writing/vim/md_columns.md b/writing/vim/md_columns.md new file mode 100644 index 0000000..25f291c --- /dev/null +++ b/writing/vim/md_columns.md @@ -0,0 +1,52 @@ +--- +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 +``` +