5

Vim has the neat Tabular plugin that allows you to quickly align some text. I use it a lot to align blocks of code along a certain character (mostly things like = and =>). But it also does a very good job at ad-hoc tables. Given something like

|Name|Rank|No.|
|Stan Ridgway|Private First Class|8797|
|John Rambo|Private|889897|
|George S. Patton|General|0879797|

it's easy to get it to end up like this:

| Name             | Rank                | No.     |
| Stan Ridgway     | Private First Class | 8797    |
| John Rambo       | Private             | 889897  |
| George S. Patton | General             | 0879797 |

Select it (or simply place the cursor on the first line), then execute :Tabularize /|.

As I'm currently trying to cross-pollinate Emacs and Vi a bit, stealing from both sides, looking for matching plugins, etc., I'd like to do the same in Emacs. Now, for the aforementioned =/=> stuff, align works very well, and even does most things pretty automagically.

And for the more complicated stuff, there's align-regexp. I would dare a guess that it's probably just the matter of getting the right regexp to feed to it (probably in the C-u prefixed extended version).

Any suggestions in this direction or other functions/packages?

2
  • Note that the :Tabularize command requires the Tabularize plugin.
    – naught101
    Oct 10, 2013 at 2:49
  • If you're trying to cross-pollinate Emacs and Vi, I highly recommend Evil, the most complete Vim emulation package for Emacs. You'll still need to find separate plugins for Vim and Emacs, but you'll be able to use the Vim keybindings in both. Feb 18, 2014 at 16:58

2 Answers 2

5

Type M-x orgtbl-mode, then C-c C-c

| Name             | Rank                |     No. |
| Stan Ridgway     | Private First Class |    8797 |
| John Rambo       | Private             |  889897 |
| George S. Patton | General             | 0879797 |
3
C-u M-x align-regexp \(\s-*\)| RET RET 0 RET y

Regarding the regexp: \(\s-*\) is there by default, so you only need to type the |
Regarding the 0: it means no extra trailing space, ie: |longest-field-in-column|

Quoting from Alignment Commands

Repeating align-regexp

Arguably, for daily use, it’s better to define some adhoc align command, e.g.

  (defun align-repeat (start end regexp)
    "Repeat alignment with respect to 
     the given regular expression."
    (interactive "r\nsAlign regexp: ")
    (align-regexp start end 
        (concat "\\(\\s-*\\)" regexp) 1 1 t))
3
  • Hmm, this properly formats the table, but the table ends up indented quite a lot (first | of each line starting in column 39, which is the max length of the input lines).
    – mhd
    Jul 21, 2012 at 15:32
  • Do you mean the one-liner (align-regex), or (defun align-repeat? I get indentation equal to a cell's right-margin width (3rd parameter), which is zero for the example in the answer. The example for (defun align-repeat gives me one space indentation, because the 3rd parameter is 1 ... I'm using GNU Emacs 23.1.1 on Ubuntu 10.04
    – Peter.O
    Jul 21, 2012 at 16:09
  • I get the indentation with both versions. Sure, the align-repeat changes the padding within the table, but in either way the first character on every line is in column 39. (tested with Emacs 22.1/24.)
    – mhd
    Jul 21, 2012 at 16:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .