30

I like to write in Markdown and often find myself needing tables. Are there any good ways of editing Markdown's pipe tables in Emacs? I am referring to this kind of syntax:

| Header | Header | Right |
|--------|--------|------:|
|  Cell  |  Cell  |  $10  |
|  Cell  |  Cell  |  $20  |

I first tried Emacs' table mode which is nice, but is designed for "grid tables" which are not supported by Markdown (let's say in Github's Markdown).

There is also org-mode's table mode which can be used as a minor mode. This comes pretty close; but the intersections are now replaced by + characters and there is no support the alignment colon. So org-tblmode first gives me something like this:

| Header | Header | Right |
|--------+--------+-------|
| Cell   | Cell   | $10   |
| Cell   | Cell   | $20   |

which I then need to manually edit to the following (editing intersection characters and adding alignment colon):

| Header | Header | Right |
|--------|--------|------:|
| Cell   | Cell   | $10   |
| Cell   | Cell   | $20   |

Is there some may that org-tblmode can also handle this? What else do you use/suggest for editing Markdown's pipe tables in Emacs?

0

6 Answers 6

12

markdown-mode has the markdown-do command (bound to C-c C-d). When point is in a table, it pads all the columns so they're aligned.

https://github.com/jrblevin/markdown-mode#usage

1
  • 2
    In my case, the TAB key works, too.
    – Glenn
    Commented Feb 25, 2022 at 1:59
11

OrgMode has nice function orgtbl-to-generic for converting orgmode' tables to alien formats. I found simple example defining custom convertor based on orgtbl-to-generic: https://gist.github.com/yryozo/5807243. Also see explanation of ORGTBL RECEIVE/SEND features here: http://dynamic-thinking.blogspot.ru/2009/11/orgtbl-mode.html. You need to define custom orgtbl-to-gfm function in Emacs and place it to your autoload then you may use minor mode orgtbl-mode for editing tables.

Small restriction: only right and left column alignments supported because Emacs OrgMode inside self doesn't supports central alignment.

See above sample org-mode source:

<!---
#+ORGTBL: SEND sample orgtbl-to-gfm
| Column 1      | Column 2 |
|---------------+----------|
|               | <l>      |
| Sample line 1 | 100      |
| Sample line 2 | 200      |
-->

And the result in markdown converted from the org-mode source:

<!--- BEGIN RECEIVE ORGTBL sample -->
| Column 1 | Column 2 |
|---|---|
| Sample line 1 | 100 |
| Sample line 2 | 200 |
<!--- END RECEIVE ORGTBL sample -->

Both them placed in the same file.

2
  • Adding samples was much appreciated. Commented Mar 1, 2014 at 0:04
  • 1
    @CharlesMerriam I added samples to the answer. Commented Jul 1, 2014 at 14:57
10

This is what I use to deal with tables in markdown mode. May be a bit of a hack but works well enough for me. It adds a local hook to a markdown buffer which, on save, replaces "-+-" with "-|-" in the entire buffer.

(require 'org-table)

(defun cleanup-org-tables ()
  (save-excursion
    (goto-char (point-min))
    (while (search-forward "-+-" nil t) (replace-match "-|-"))
    ))

(add-hook 'markdown-mode-hook 'orgtbl-mode)
(add-hook 'markdown-mode-hook
          (lambda()
            (add-hook 'after-save-hook 'cleanup-org-tables  nil 'make-it-local)))
0
4

Bind the function to a key for a right-aligned conversion of a region:

(defun markdown-regexp-right (beg end)
  (interactive "r")
  (replace-regexp "-\|[^-]" "-:|\n" nil beg end)    
  (replace-regexp "-\\+-" "-|-" nil beg end)
)

This will replace -+- with -|- and replace -| with :| in the right alignment case.

Note that \n is included, because this makes sure the other -|- don't get changed to -:|, but only -| when it is followed by a new-line.

8
  • I appreciate your input but unfortunately when I try this code it crashes Emacs for me :/ With the second line commented out it seems to work, so maybe it is endlessly looping and causing a stack overflow.. Commented Jan 14, 2013 at 8:46
  • Does it do the job otherwise? Commented Jan 14, 2013 at 9:55
  • Oh, and are you calling it on a region? Commented Jan 14, 2013 at 9:55
  • Yes I call it on a region, and without the (replace-regexp "-\|[^-]" "-:|\n" nil beg end) line it works fine (it just replaces the intersections) Commented Jan 14, 2013 at 10:02
  • Anything else that you think should be added? Commented Jan 14, 2013 at 22:38
3

Org-mode is so enticingly close that I believe this is a case for its minor mode orgtbl with arbitrary syntax to convert. A line containing :-, -: or :-: would thus have its colons removed, and a second line with <l>, <c> and <r> added to inform org-mode of the alignment. The translation back shouldn't be too hard then. Sadly I'm not good enough at Emacs to write it right now. Incidentally, pandoc's markdown parser does accept + as generated by org-mode.

Having looked a bit more at it, I believe the existing hooks in org-mode require a bit much cruft around the text, and some editing functions are in order. Since this is my first attempt at emacs lisp, my emacs lisp to convert between orgtbl and markdown is nothing short of horrid, but I have managed to convert a test table.

0

Markdown-mode package provides table support for GitHub flavored markdown.

Markdown-mode is part of Spacemacs (and probably other community configurations). Note that if using markdown and org layers in Spacemacs, orgtbl will hijack some key bindings, i.e. TAB and format tables using org syntax. Disabling orgtbl-mode removed the orgtbl highjack.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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