Files
lk/writing/vim/format_md.md

1.5 KiB

title, tags, requires
title tags requires
Reformat a Markdown Table
writing
vim
markdown
writing/vim.md

This markdown table is badly messed up:

| File  | Category |
|:------|:---------|
| calendar.md           | tex|
| tex_packages.md           | tex|
| completion.md           | vim|
| csv_to_md.md           | vim|
| format_md.md           | vim|

Highight from the top with V6j, then run column to fix the output:

:!column -ts'|' -o '|'

It displays like this:

:'<,'>!column -ts'|' -o '|'
| File                      | Category |
|:------                    |:---------|
| calendar.md               | tex      |
| tex_packages.md           | tex      |
| completion.md             | vim      |
| csv_to_md.md              | vim      |
| format_md.md              | vim      |

That's better, but the header is broken. Fix is by replacing spaces with dashes.

:s/ /-/g

The lines have too much whitespace. You can fix this with the 'truncate' command, to squeeze repeating spaces or dashes.

tr -s ' -' |column -ts '|' -o '|'
| File            | Category |
|:----------------|:---------|
| calendar.md     | tex      |
| tex_packages.md | tex      |
| completion.md   | vim      |
| csv_to_md.md    | vim      |
| format_md.md    | vim      |

Keyboard Shortcut

Put this in your ~/.vimrc to map 'Control + t' to reformat markdown tables in visual mode.

vmap <C-t> :!tr -s ' -' \|column -ts '\|' -o '\|'<Enter>j:s/ /-/g<Enter>k