Sublime Text tips for Markdown table editing
I edited a bunch of MultiMarkdown tables in Sublime Text 3 yesterday, and by about halfway through I’d developed a pretty good system. I thought I’d document a couple of tricks for my own future reference, and for anyone else it might help.
Setup
First, you want MarkdownEditing if you’re working in Sublime. It’s the package I built long ago, now cared for by Ali Ayas and Felix Hao, and being actively updated by the community.
Next, you need the Table Editor package. If you edit tables in any of the humane text formats, this is a godsend. It allows you to tab between columns, wrapping to the next row at the end, auto format the entire table every time you finish an edit, and swap, add and delete columns and rows.
Now, with those two packages, editing is almost as simple as just working in a spreadsheet.
Note: Keyboard combinations mentioned in this post are Mac-specific, but available on other platforms with the usual meta key substitutions.
Building tables
To start a table, just lay out the header row:
| Column 1 | Column 2 | Column 3
I’m working in MultiMarkdown, so the next row also needs to be a divider row, which Table Editor makes as simple as a pipe and a dash:
| Column 1 | Column 2 | Column 3
|-
Hitting TAB on the second line will generate:
| Column 1 | Column 2 | Column 3 |
|----------|----------|----------|
| | | |
Converting lists
My specific task was to turn a series of bullet lists into columns of a table. I could have written out fancy regex scripts to automate this, but there were enough variances that it was worth figuring out faster ways to do it by hand.
I knew how many lists the table needed to hold, so setting up the header row was easy. I knew how long the longest of the lists would be, so I could add rows of empty cells ahead of time. With Table Editor’s ability to tab through cells, this is handy.
Table Editor has commands for adding rows, but I’ve disabled a few of them because the shortcuts interfere with my normal editing commands (Option-Right, for example). The Control-Shift-Down command to insert a new row above is still active, so once the table skeleton is set up, I can just add the rows with repeated keystrokes.
You can also use some of Sublime’s great editor commands, which also come in handy in the next part. To add a certain number of rows to the end of the table quickly, just select that number of lines, use Command-Shift-L to split the selection into lines, then type a “|” and hit tab. Table Editor will add the missing dividers for the number of columns in the header row.
If you use Vintage mode (the Vim emulation in Sublime) you can make this even faster by just typing the number of rows followed by “j” before doing the multiple selection split (I don’t think Vintage mode handles repeat on “o” and “i” commands).
Method 1
I have three bullet lists (with no nesting) that I want to insert into the table as columns. There are a couple of ways to do this with about the same amount of efficiency.
First, clear out the bullet markers (*,+,-). Either search and replace, or select the lists, split the lines (Command-Shift-L), move to home (Command-left), cursor to after the list marker, and delete to home (Command-Del). If your lists are interspersed with other elements, you can use multiple selections to do them all at once without affecting other parts. Just hold down command and make the block selections with the mouse, then perform the same steps.
First, you can skip the empty column creation and do this:
- Select the longest list, or the one for the first column if they’re all the same length
- Break the selection into lines
- Use Command-Left/Right to add pipes at the beginning and end of every line
- Select another list
- Break the selection into lines (important)
- Copy or cut the second list with the multiple selections
- Select the first list and break the selection again
- Move the cursor to where the new column is going
- Add a pipe as needed if inserting into the middle of the table
- Hit Command-V to paste
- Hit TAB to reformat the table with the new data
Pasting “broken” selections into multiple cursors is a very handy trick that proves useful in all kinds of situations.
Method 2
Another method is to use the “empty columns” trick above, and then use Ctrl-Shift-Up/Down to create multiple cursors in the necessary number of cells in a column before pasting a list from a broken selection.
This method is more useful when doing more piecemeal editing of cell blocks.
For visual learners
I get nothing out of watching other people do things in videos (I’m a 100% tactile learner), but I’ve found that some people make more sense of a visual presentation than they do from my attempts at textual explanation. Thus:
Backstory: a tale of sleep deprivation woe
In case you’re curious, the reason I was working on this was because I’d had a sleepless night and decided to take a day off from my usual projects. I was dinking around and decided to fork Justin Blake’s tool for generating LICENSE files in open source projects.
I wanted to add the ability to get a quick summary of each license (licgen info mit
). I used Marky to rip the overview page from choosealicense.com to Markdown and then split it up into summary files. Then I wanted to shorten the height of the output for a summary, so I made the “Required/Permitted/Forbidden” lists that Marky had generated into tables like this.
It was at that point that I realized that in my haze I had entirely forgotten that GitHub’s new licenses API does all of this already and went back to bed. My fork is finished and working, but what’s the point?
At least I honed my Markdown table editing process.