Org-mode Hidden Gems - 02 Tables

Region operations on cells

Excel-like copy-pasting: C-c C-x M-w(copy)/C-w(cut)/C-y(paste).

Create table from region

Bound to C-c |, org-table-create-or-convert-from-region converts semi-formatted texts (esp. those copied from web) into Org table.

This command rely on detecting delimiter TAB or comma in region. If conversion fails, check any minor modes or settings that may turn TAB character into space in the background. In my case, it’s whitespace-cleanup-mode. This can get tricky if enabled along with auto-save-mode, as copied text might be auto-saved and TABs stripped before conversion. A workaround is to disable them temporarily.

Special Marking Characters for Tables

Advanced Features demonstrates how to use special marking characters to enable two desired features: assigning names to rows/columns/fields, and automatic recalculation of fields.

It is easier to follow the examples in Emacs with proper highlighting, than in HTML/Info in B&W, do give it a try. Here I copy the table to show it in action. Key take-aways:

  • Special marking characters shall occupy the first column
  • Name columns/rows in a human-readable way with !, ^, _ (No more counting, no more C-c ? to locate reference)
  • Control automatic recalculation with # and *
  • Define per-table parameters/constants in unused cells with $

I was confused by table references and haven’t used much of calculation features before, but this section is quite enlightening. I think I’ll use it more later.

Use Org Table in LaTeX/HTML

User /u/emacsomancer mentions this killer feature of using Org table in other major modes, notably, in LaTeX and HTML.

Command orgtbl-insert-radio-table prompts for a table name, and inserts source and target location. Edit Org table in source location, which is in the comment section of major mode, as usual. Upon hitting ubiquitous C-c C-c, Org will translate the table from source location, and put it into target location where it gets rendered.

HTML works as well.

It does not only alleviate the pain of editing table by hand but also preserve all essences of Org table: adding/removing rows/columns, reordering, adding boundaries, formulas, Calc integration, etc. I don’t know any other LaTeX editing environment provides similar table editing experience, and I’d list it as a strong reason to try Emacs for anyone using LaTeX professionally.

This nifty, wizard piece of functionality secludes itself in Appendix.5 Tables in Arbitrary Syntax. If you’re looking to use Org table in other plain text modes without translation, Orgtbl-mode is available.

That concludes Tables chapter.

Behind the Scene: Bulk Video Conversion with Dired

To support pause operation, I migrated all videos from original gif to webm, and was later reminded (by /u/Bombastry, thanks) that Safari can’t handle webm properly. I still wish to support all browsers, and found out mp4 as a better choice. Now I’ve got a bunch of webm in hand, how do I generate corresponding mp4?

First I need an ffmpeg command. It’s one search away:

ffmpeg -i input.webm -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" output.mp4

Works fine, how do I apply to each individual webm though? I don’t want to input one by one to terminal by hand. Neither do I feel like writing bash script (takes time to remember syntax), nor copy file names from dired and use rectangle editing hacks (lots of editing, you will see my attempt at the beginning of video).

Then I notice this line in dired-do-shell-command (bound to !):

If there is no ‘*’, but there is a ‘?’ in COMMAND, surrounded by whitespace, or
a ‘`?`’ this runs COMMAND on each file individually with the file name
substituted for ‘?’ or ‘`?`’.

Almost cryptic, but essentially it allows running a command on individual marked files. Combined with wdired, this turns out to be quite a smooth flow:

  1. Mark files: individually with m, or in one go with dired-mark-extension (bound to * ., you’ll need dired-x)
  2. Run command: ! (or & to run asynchronously), fill in command, using ? and `?` to refer to file name
  3. Fix suffixes: toggle on wdired (C-x C-q) and query-replace (M-%)

Not Org-mode related, just thought it’s a cool pipeline. Recorded as a macro, this could be a one-click bulk conversion tool for videos. Emacs does have some plain text magics!

PS: If you know how to refer to filename without the suffix, please let me know, this would simplify it even further (Update 2019-06-09: This is solved. See below for details) . It should be a common use case (same filename with different suffix in input/output), but I couldn’t find related settings.

Update 2019-06-07: Inspired by reddit thread on eshell awesomeness, we can use file-name-sans-extension in eshell to get the base name:

for f in *.webm {ffmpeg -i $f -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" $(file-name-sans-extension f).mp4}

The drawback is that, dired-do-shell-command ultimately uses shell-file-name, which only accepts file shells such as zsh, bash but not eshell (correct me if I’m wrong). So to use the command, we must start a new eshell session instead and jump around.

Update 2019-06-09: /u/osugisakae has kindly pointed out the one liner using basename command:

ffmpeg -i ? -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" `basename -s .webm `?``.mp4

The outer backtick runs basename command against inner backtick `?`, which represents the actual file name, then returns the base name after stripping .webm suffix. All we do then is simply add .mp4 afterwards.

Compared to the eshell approach above, we can stay comfortably in dired-land. This is a very powerful trick that applies universally. Thanks /u/osugisakae!

Org-mode Hidden Gems series: