1. 39
  1.  

  2. 7

    I have a script that does something similar, but it is only UNIX-dependent, not editor-dependent.

    This is it:

    $ cat -vT fix_table.sh
    COLUMN_SEPARATOR='|'
    
    sed 's/'"$COLUMN_SEPARATOR"'/&^\/g' |
    column -t -s "$COLUMN_SEPARATOR" |
    sed 's/^\/'"$COLUMN_SEPARATOR"'/g' |
    sed 's/[ ]\{2\}'"$COLUMN_SEPARATOR"'/'"$COLUMN_SEPARATOR"'/g'
    

    Note that I change the file separator (shown as ^\ in the sed entries) to the $COLUMN_SEPARATOR, so it handle empty columns properly—as far as I remember.

    $ ascii FS
    ASCII 1/12 is decimal 028, hex 1c, octal 034, bits 00011100: called ^\, FS
    Official name: File Separator
    

    If you feel it is useful, feel free to copy and use it: treat it as “public domain”.

    EDIT 01: you will have to change the ^\ of the sed entries with the file separator.

    EDIT 02: No: I concatenate the file separator because column removes the | as column separator. So I mark the places the column separators are (supposed to be), then column makes all beautiful, except it removes the column separators, then I put then back.

    1. 5

      I’m a die-hard Vim user but I open markdown files with Emacs to align the tables. I think I might have configured org-mode to do that at some point and never found anything as good for Vim? I’m still completely useless with Emacs for everything else so I generally just open the file (in evil-mode too of course) align the table and get out again.

      1. 4

        Should you also want a solution in Vim, Tabular.vim works very well. For example :Tab /, aligns on comma characters.

        http://vimcasts.org/episodes/aligning-text-with-tabular-vim/

        But going via Emacs works too, of course. (I strongly suspect there is nothing it cannot do.)

        1. 2

          Thanks for the tip, org-mode seems to just automatically handle aligning and adjusting the entire table completely though.

      2. 5

        The core feature here is & — aligning all selections. Any editor which supports multiple cursors (not necessary a modal one) should have an equivalent — that’s indeed the most elegant way to specify what is to be aligned.

        1. 4

          Everytime I need a table in Markdown I just write a perl script to generate the HTML directly and use that in my document.

          1. 4

            Yes. Markdown is cool, but you hit the walls very quickly. The solution is to bail out to HTML. In the wise words of da Share z0ne: if it sucks, hit da bricks.

          2. 4

            Obligatory thanks to @mawww for an amazing editor design.

            1. 3

              Helix’s table alignment heuristics is one of the things that really sold me on it.

              1. 3

                Alternatively in VIM or NeoVIM reformat pipe style tables in Markdown my shipping it out to pandoc for cleanup:

                vip:!pandoc -t markdown-simple_tables+pipe_tables
                

                This can be made into a key binding to update the table formatting with a single stroke.

                Obviously this could be adapted to any editor that can filter content through an external program.

                There are ways to do the same multiple-cursor stuff and edit table formatting manually in VIM, but sometimes shipping out to external tools is just more pragmatic and less error prone.

                1. 3

                  Same thing in Kakoune: <a-i>ps\|<ret>&x2,s<space><ret>r-

                  1. 2

                    Hah! I looked at that line noise and thought I didn’t understand anything there despite using Helix all day every day. Then realized I knew everything except the final &.

                    Thanks man :)

                    1. 2

                      When I use my editor KeenWrite to edit R Markdown documents, I keep the data in a machine-readable CSV file and import it using an R function call cvs2md:

                      https://youtu.be/XSbTF3E5p7Q?list=PLB-WIt1cZYLm1MMx2FBG9KWzPIoWZMKu_&t=184

                      This allows for dynamic documents that pull data in from a single source of truth. As a side benefit, I don’t have to concern myself with formatting Markdown tables.