Pandoc table editing in vim
I’ve been itching for a way to get more efficient at creating pandoc tables ever since I started writing my notes in pandoc. Perhaps the only thing I’ve ever been jealous of emacs about is a video I saw using it’s orgtable mode. I haven’t quite adapted vim to work as emacs shows in this video. I’m convinced that it’s possible but also believe that the spreadsheet functionality shown is overkill. I don’t need my tables to do any math anyway; I just need them to be fast and seamless. To accomplish this, I turned to tabular. I originally got the idea while watching a vimcast on tabular that seemed to be behaving similarly to the emacs video. It uses a gist written by Tim Pope that executes :Tabularize every time you enter a pipe symbol. The code is provided in the show notes of the video, so you can easily copy that to your .vimrc. You can use this create a basic table structure dynamically, which ends up looking something like this:
| Fruit | Amount | Price | Total |
| Apple | 2 | $2.00 | $4.00 |
| Banana | 3 | $1.00 | $3.00 |
| Kiwi | 5 | $0.50 | $2.50 |
Well, that’s nice and all, but this is not a pandoc table. I wrote a simple (probably imperfect) function to convert this into a standard pandoc table:
Fruit Amount Price Total
------ ------ ----- -----
Apple 2 $2.00 $4.00
Banana 3 $1.00 $3.00
Kiwi 5 $0.50 $2.50
It works by visually selecting the lines and executing :Tabularize one last time in case there is any remaining misalignment.
vnoremap <leader>t :call <SID>table()<cr>
function! s:table() range
exe "'<,'>Tab /|"
let hsepline= substitute(getline("."),'[^|]','-','g')
exe "norm! o" . hsepline
exe "'<,'>s/-|/ |/g"
exe "'<,'>s/|-/| /g"
exe "'<,'>s/^| \\|\\s*|$\\||//g"
endfunction
There is another type of pandoc table, called a grid table. You can easily convert the markdown table into a grid table using pandoc itself like this:
command! -range=% Rst :'<,'>!pandoc -f markdown -t rst
Just select the pandoc table and run :Rst to convert it into a grid table like this:
+----------+----------+---------+---------+
| Fruit | Amount | Price | Total |
+==========+==========+=========+=========+
| Apple | 2 | $2.00 | $4.00 |
+----------+----------+---------+---------+
| Banana | 3 | $1.00 | $3.00 |
+----------+----------+---------+---------+
| Kiwi | 5 | $0.50 | $2.50 |
+----------+----------+---------+---------+