26

Here is (part of) a table I use in some documentation:

+---------------+------------------------+---------------------------------------+
| TASK NAME     | WHEN                   | DESCRIPTION                           |
+---------------+------------------------+---------------------------------------+
| db:seed       | On every update        | Data required for the application to  |
|               |                        | run; you should always be able to     |
|               |                        | safely execute this multiple times    |
|               |                        | for an update.                        |
| db:truncate   | Manually (dangerous!)  | TRUNCATE all tables but postal_codes  |
| postal_codes  | Once                   | Load the postal_codes table           |
+---------------+------------------------+---------------------------------------+

The problem is this is very awkward to edit; if I change one thing, I need to (manually) re-align everything, which is a PITA

Are there easier ways to manage this? I don't care about the exact above layout as such, but would like to keep the wrapping of the 3rd column...

  • 1
    An aside, if this is reStructuredText, you might consider looking into list-table (recently I converted many tables to this, because ascii tables are hard to maintain and diff poorly) – ideasman42 Feb 5 '15 at 7:11
32

I am the author of the plugin vim-table-mode. I've spent quite a lot of time building & improving this, especially with community feedback and it caters exactly to this use case.

A short quickstart: You enter 'table mode' with the :TableModeEnable command, you can then simply type:

||
| MOVIE | QUOTE|
||
| Blade Runner | Like tears in the rain |
| Monty Python's quest for the holy grail | Are you suggesting that coconuts migrate? |
||

And vim-table-mode will automatically take care of the alignment for you on-the-fly, so that it will look as:

|-----------------------------------------+-------------------------------------------|
| MOVIE                                   | QUOTE                                     |
|-----------------------------------------+-------------------------------------------|
| Blade Runner                            | Like tears in the rain                    |
| Monty Python's quest for the holy grail | Are you suggesting that coconuts migrate? |
|-----------------------------------------+-------------------------------------------|

When editing the table later, vim-table-mode will also take care of the alignment for you.

You can leave 'table mode' with :TableModeDisable.


Its core is about easily creating tables which will be auto formatted as you type. It offers a lot of mappings to move around and manipulate tables as well (a particularly useful one is <Leader>tdc to remove an entire column). All you have to do is enable table mode and these will kick in and you can just type away without worrying about formatting.

It has many more features than displayed here, such as the ability to right align columns with a little hint on the header. It also has a neat spreadsheet formula engine that allows one to define formulas & perform computations like a regular spreadsheet would.

It's built to be extremely flexible & configurable so you can change several aspects of the tables & borders that are created.

I have also created a few screencasts trying to showcase its powers. This screencast is the most recent one, but please note that there have been many improvements since.

  • 5
    Thanks; I've edited your answer a bit to add some practical usage information (it read a little bit like an ad before) ... One thing I wasn't able to do with vim-table-mode is the wrapping of the 3rd column as in the example in my question ... Is this possible? – Martin Tournoij Feb 7 '15 at 0:46
  • @Carpetsmoker thanks for the edit. Well you can wrap it manually, there's no way to do that automatically. – Dhruva Sagar Feb 7 '15 at 5:47
6

If you can use sed and column, a command-line solution that comes close would be:

!sed 's/[- ]*\([+|]\)/'$'\x01''\1/g' | column -ts $'\x01' | sed '/^[-+ ]*$/s/ /-/g'

You could combine this with visual selection and '<,'> or with line numbers.

Cons:

  • Uses sed and column. Vim is incidental. The sed commands can probably be changed to vim :substitute commands, but column is crucial.
  • The row separators (---...-) get padded, but I have suspicions on its reliability.
  • I don't know how portable $'\x01' is.

Sources:

  • Minor glitches aside (which can probably be fixed), this looks promising ... The one thing I can't get done is the wrapping of the 3rd column though :-/ – Martin Tournoij Feb 7 '15 at 0:19
4

If you don't want to install any plugins and you want to edit tables keeping the alignment:

-- use the built in VREPLACE mode, activated by gR.

(Make sure you has vim latest version, and check if it's compiled with +vreplace feature, typing vim --version on the terminal)

enter image description here (my statusline is set to show REPLACE for both REPLACE and VREPLACE modes)

If you want to know more about the REPLACE and VREPLACE modes, check this article.

3

I've created the command-line tool tablign for this purpose; it works from within vim as well. Simply install with

[sudo -H] pip3 install tablign

mark the table in vim and call

:'<,'>:!tablign

(vim fills in the first few characters for you if you've marked the table.)

It can't do fixed column widths yet, but that could be added if need arises.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.