Search code examples
vimtabularvim-tabular

Creating simple tables in vim


Recently I've started discovering "deeper" vim and now I want to create a simple table without using any external plugins (I know it's bad, but I want to have some "know-how"). Let's say I want to have a table like this:

| name            | address                  | phone      |
|---------------------------------------------------------|
| John Adams      | 1600 Pennsylvania Avenue | 0123456789 |
|---------------------------------------------------------|
| Sherlock Holmes | 221B Baker Street        | 0987654321 |
|---------------------------------------------------------|

But how do I manage to make underscores till the end of the longest line and seperate columns with equal width? (In short way, no typing all by hand)


Solution

  • If you know the amount of the dashes needed then you can do:

    {number}i-<ESC>
    

    Where {number} is the amount of dashes needed. If you want to "learn" that automatically then you need to use VimL and strlen() function to first learn how many dashes are needed and then you can use append() to insert text below provided line.