Quick Vim tip: convert piped table to pandoc/RST/Emacs grid table
Powered by the tabular.vim extension
This post is a few years old
This post has not been updated for some years. It may no longer be up to date. Opinions may have changed.
Inspired by a snippet by Conner
McDaniel, I added a little
snippet to my .vimrc that can convert a piped table to a
grid table used by pandoc/RST/Emacs.
It goes from this:
Fruit | Price | Advantages
Bananas | $1.34 | - built-in wrapper
Oranges | $2.10 | - cures scurvy
To this:
+---------------+---------------+--------------------+
| Fruit | Price | Advantages |
+===============+===============+====================+
| Bananas | $1.34 | - built-in wrapper |
+---------------+---------------+--------------------+
| Oranges | $2.10 | - cures scurvy |
+---------------+---------------+--------------------+
Add this to your .vimrcfile:
function! s:tablePandoc() range
exe "'<,'>Tabularize /|"
let hsepline= substitute(getline("."),'[^|]','-','g')
exe "norm! o" . hsepline
exe "'<,'>s/-|/ |/g"
exe "'<,'>s/|-/| /g"
exe "'<,'>s/^| \|\s*|$\||//g"
exe "'<,'>!pandoc -f markdown -t rst"
endfunction
command! -range=% TablePandoc :call <SID>tablePandoc()
Now, make a visual selection and type :TablePandoc. It
uses tabular.vim
to align the pipe characters. Example:

If you have additional tips for using Pandoc from Vim, please let me know.
P.S.: This post was copied from my old blog, I’ll likely won’t edit it any more.
