A Few of my Org Mode Customizations

Org mode is already a part of my life. It’s only natural I’d like to perform various customizations to my liking. Fortunately customizability is exactly Emacs’ core philosophy which means I get to do it with freedom. The following are just a few of them which I think might be helpful to others.

Paste rich format text from web pages into Org mode

When I first started to use Org mode there was a glaring deficit to me, which is the inability to paste any formatted text from web pages. If everything pasted becomes plain, much of the original information would be lost, and this would be a great hindrance for anybody wanting to use Org mode exclusively. Luckily this is Emacs where everything is possible. After I posed the question on StackExchange, user @mankoff was quick to come up with a solution using perl and pandoc. Note that this version of code is tested on OS X, but for other systems the difference should only be the utility to call in order to retrieve text from system clipboard, which in this case is osascript.

(defun kdm/html2org-clipboard ()
  "Convert clipboard contents from HTML to Org and then paste (yank)."
  (kill-new (shell-command-to-string "osascript -e 'the clipboard as \"HTML\"' | perl -ne 'print chr foreach unpack(\"C*\",pack(\"H*\",substr($_,11,-3)))' | pandoc -f html -t json | pandoc -f json -t org --no-wrap"))

There are actually a couple of points worthy of further attention. First is that pandoc automatically hard-wraps its output at 70 characters per line. I normally just soft-wrap my notes so I added the option --no-wrap command there.

Then note that pandoc automatically uses Non-breaking space instead of normal space when converting formatted inline text(bold, italics, code etc.), which is not recognized by orgmodeby default. Non-breaking space has to be added to org-emphasis-regexp-components in order for those texts to be formatted correctly in orgmode. The following is my code for that. There seems to be two space characters each line because non-breaking space looks exactly the same as normal space as rendered here, but Emacs recognizes it with a underline.

;; This one is for the beginning char
(setcar org-emphasis-regexp-components " \t('\" {")
;; This one is for the ending char.
(setcar (nthcdr 1 org-emphasis-regexp-components) "- \t.,: !?;'\")}\\")

With this function copy-and-pasting in Org mode supports rich format text as other note-taking apps.

Set non-monospace font for normal notes

(Update Jan 2021: There is now a package mixed-pitch which does a great job on this. I recommend checking it out: https://gitlab.com/jabranham/mixed-pitch)

Nobody likes to read normal notes in monospace. But the source code snippets better be aligned. Is there a way to achieve both? Definitely. The variable-pitch-mode in Emacs is built just for that.

  1. Remember to first set variable-pitch face in your configuration. Just M-x customize-face on variable-pitch would do. I use Helvetica Neue as I think it’s easiest on the eyes when you stare at it all day. It’s easy to change when you want. The following code was added by customize after I set the font:
'(variable-pitch ((t (:family "Helvetica Neue" :height 160))))
  1. Add hook to turn on variable-pitch-mode when the target major modes are enabled. Note I set org-code etc. to be fixed-pitch so that they’re still monospace.
  (defun set-buffer-variable-pitch ()
    (variable-pitch-mode t)
    (setq line-spacing 3)
     (set-face-attribute 'org-table nil :inherit 'fixed-pitch)
     (set-face-attribute 'org-code nil :inherit 'fixed-pitch)
     (set-face-attribute 'org-block nil :inherit 'fixed-pitch)
     (set-face-attribute 'org-block-background nil :inherit 'fixed-pitch)

  (add-hook 'org-mode-hook 'set-buffer-variable-pitch)
  (add-hook 'eww-mode-hook 'set-buffer-variable-pitch)
  (add-hook 'markdown-mode-hook 'set-buffer-variable-pitch)
  (add-hook 'Info-mode-hook 'set-buffer-variable-pitch)

There is a nice post offering a more detailed configuration and claiming to preserve more font attributes such as org table coloring. However for general purposes the simpler configuration should suffice.

Monospace together with normal font:


Hiding Text-Formatting Markers

This is probably more of a matter of personal taste but I don’t really like seeing text-formatting markers all around. Why display /text/ when you can just have text. Turns out there’s a nice option in Org mode: org-hide-emphasis-markers which helps exactly in that. Just M-x customize-variable and then set org-hide-emphasis-markers to t. I didn’t remember seeing this option when I was reading org manual and only stumbled upon it later, so I thought listing it out might help.

Inserting timestamp for each note.

One potentially useful feature of normal note-taking apps is the ability to have the notes timestamped automatically. In Org mode one possible way of doing so is to utilize org-insert-heading-hook. You can define a function insert-inactive-timestamp as

(defun insert-inactive-timestamp ()
(org-insert-time-stamp nil t t nil nil nil))

I am fine with having the timestamp in the header directly. So I just have

(add-hook 'org-insert-heading-hook 'insert-inactive-timestamp)

There’s another solution in an Org mode mail list discussion which inserts the timestamp into the body:

(defun bh/insert-heading-inactive-timestamp ()

(add-hook 'org-insert-heading-hook 'bh/insert-heading-inactive-timestamp)

Automatic pull/push for MobileOrg

Traditionally there’s been open source projects called MobileOrg to bring org mode’s functionality to your smartphones. Both Android and iOS versions utilize the same interface to interact with Emacs, which requires manually “pushing” and “pulling” changes from Emacs. There are a few timer-interval based auto-sync solutions out there. However, as I normally don’t use my phone to take notes with my laptop on, just simply pulling on Emacs initialization and pushing on shutting down works surprisingly well,

(add-hook 'after-init-hook 'org-mobile-pull)
(add-hook 'kill-emacs-hook 'org-mobile-push)

Note: Recently there has been an Android app called Orgzly which directly syncs your .org files instead of going through the push/pull interface defined for MobileOrg. I find it more refined and actively developed than the previous MobileOrg Android. It’s not open-source, but currently it feels a better app and I’ll be using it for now.

Automatically start the next pomodoro with org-pomodoro

I was used to automatically starting the next pomodoro when using other apps, as you’ll often forget to do it manually when a break finishes. There was an issue in org-pomodoro which prevented this from being configured, but now it’s fixed and you can just use org-pomodoro-break-finished-hook to achieve it:

(add-hook 'org-pomodoro-break-finished-hook
  (lambda ()
    (org-pomodoro '(25)))

I’m only starting to have fun with Org mode so I might find some other interesting customizations later and list them here.

From OneNote/Evernote to Org Mode

(Note: This post is more a kind of rambling personal recount/opinion, so if you’re already an Org mode user and are just interested in some Org mode customizations you might want to see this.)

I’m obsessed with note-taking. I’ve lost count of the number of diary books I wrote as a kid. And back when I was into reading newspapers, I would spend hours each night, carefully cutting and pasting newspaper articles into notebooks, accompanied with my comments; I easily accumulated hundred of pages of paperclips in this fashion. Thus it’s only natural that I immediately set out to find the perfect note-taking app for me after getting into the world of programming. However, the process was unexpectedly fraught with setbacks and angst, until I finally met Emacs Org mode, which is everything a programmer(or possibly any serious note-taker) could ever dream of. I’m amazed by how my daily efficiency massively improved upon using Org mode, how other note-taking apps are just kind of reinventing the wheel, and how I didn’t get to know it earlier. It’s not only for programmers either. I’ve seen many people using Emacs and Org mode for their scientific research, creative writing, personal journal, or whatever purpose out there.

My first note-taking app was OneNote. It was great - it had multiple layers of organization comprising of Notebook, Section, Page, and even Subpage. I gladly wrote a whole lot of notes on books/movies/interesting stuffs I would find on the web. It was an integral part of my life when I first learnt English and prepared for university application. However at that time Microsoft was known to be stubborn to not offer their products outside of Windows. So when I switched to Mac as many programmers would, OneNote was just out of the picture.

Evernote was all the rage at that time. However I didn’t have much fondness for it from the very beginning. It has always seemed to me that Evernote pays more attention to marketing and appealing to “business” users rather than spending serious efforts in the product itself. For a starter, its note organization was amateurish compared with OneNote’s multi-layered approach. It just doesn’t make much sense if all I can do is to throw various notes into one “Notebook” if I were reading several multi-chaptered books, and each chapter has various subtopics. The dealbreaker for me, however, was its incredibly sluggish and unresponsive synchronization. I, being an obsessive-compulsive note-taker, had easily accumulated more than ten thousand notes. Every once in a while Evernote would just automatically initiate its sync progress, freezing the UI in the process and leaving me unable to type anything for 15 seconds. Hell, any half-decent undergrad programmer would tell you that letting a background operation hang the UI is a beginner’s mistake. How could a company with hundreds of millions of dollars in funding not even get such basics straight?

After my unhappy years with Evernote, finally, as a part of its strategy shift, Microsoft began to offer an OS X version of OneNote. I thought it would mark the return of my happy time with note-taking. Wrong. What I didn’t realize was that programming has long been a part of my life. I constantly need to note down fragments of code, reason with them and write down my own observations; I may also take notes with mathematical notations for more theoretical contents (mostly in LaTeX). Besides, I sometimes need to compose and record my own code artifacts, to review and maybe reuse them later. Both Evernote and OneNote were doing a terrible job in that regard. OneNote only offered a simple “Code” formatting, without any syntax highlighting nor language support, and if you paste anything from the Internet, the format was usually broken in one way or another. Evernote didn’t even offer option for you to spontaneously type in code at all, though the pasting support was better. Simply put, it’s hard for me to imagine any programmer-notetaker being happy with the limited support provided.

I set out to search for viable solutions. Markdown-based note-taking software was the first thing that came to my mind. There were a few excellent apps for markdown note-taking, such as Mou, MacDown, and Kobito. They all do a decent job in providing syntax highlighting support and flexible markup. The problem was with structural organization - They were mostly nothing more than single-file editors. Searching, comparing and archiving contents quickly became a nightmare.

Then I came across an app called Quiver, which is actually a decent enough app considering it’s solely a one-man effort. It has an Evernote-esque interface. But what sets it apart is its support for syntax highlighting, Markdown and LaTeX, as it utilizes AceEditor in its core; It even supports Vim/Emacs keybindings. I was happy with it for a while, until I finally got to use Org mode after I picked up Emacs through the excellent Spacemacs project. I was immediately hooked and wondered why I didn’t get to use it sooner:
Its original functionality set already outclasses everything that’s out there: With org-babel you edit source code snippets directly in Emacs major modes for each language seamlessly. If you want, you can even just run the snippet on-the-fly. With org-clock and org-pomodoro coupled with TODO you have a personal project-management + time-tracking suite much better integrated and flexible than the countless (closed-sourced) solutions out there; I no longer have to endure the craziness of using one app to keep track of my tasks, another to log my time, and yet another to take notes related to those tasks. With tools like helm-ack I can search my whole notes repository in a flash with good old ack which is simply more efficient than search functionalities in shiny modern apps. But that’s not even the most important part: No matter how good a program is, there are always things that don’t suit you 100% and on which you want to tweak, which Org mode allows you to do, a lot. This has made me a much happier Org user.

Coming to think about it, I guess one big reason why it had not occurred to me to try out Org mode at all despite it being around for a long time is really that I’ve been a Vim guy. From what I read around the web, I got this notion that Vim and Emacs are just two distinct things that you both try out and then decide to stick with one of them, incompatible with anything that the other camp has to offer. It’s not that I didn’t try out Emacs before, I did and thought the text editing experience was clumsy compared with Vim, and the conceptual overhead of learning another system is huge. However, following this line of thought I just got lazy and didn’t even try out various Vim emulation tools out there such as the excellent evil, not to mention Org mode; this proved to be a huge mistake. To a large part I’m grateful to the open-source project Spacemacs, which offers an Emacs Kit focused on Vim emulation. You can immediately start to use it as if it were Vim, just with awesome Emacs features such as Org mode supported natively (with Vim-like keybindings). Later you’ll naturally feel the thirst to tweak its behaviors here and there, thus gradually becoming proficient with all the quirks and inner mechanism of Emacs. It also has another set of configuration focused on holy-mode for those who don’t use Vim. Whether you’re familiar with Vim or not, I definitely recommend you to check it out as a nice entry point to the wonderful world of Emacs and Org mode.

What a kind of makes me uneasy is the perceived tendency where Vim and Emacs are just simply being heralded less and less by programmers of the new generation, while in fact they’re still unparalleled in many aspects. I know my university is nothing compared with top-quality CS schools, but the fact that barely anybody around me uses Vim and Emacs daily, and that a large portion of students have absolutely zero idea what those things are, still staggers me. Also I had also been a SublimeText user for some time, but still, I realized it doesn’t really compare to either native Vim or Emacs. Vim offers unparalleled editing efficiency and freedom which no emulation I’ve seen so far has been able to 100% bring. As of why Emacs is important, a perhaps inappropriate analogy I’ve come up with is owning a car. Emacs embodies this philosophy where you want full ability to customize your own beloved vehicle. You may power up its engine, change its window, paint it a different color… Anything imaginable, you do it with your own hands instead of having to choose from some limited factory preset or ever resorting to any external hands, which will likely inevitably fall short of your expectations one way or another. You are the mechanic here, and I bet many a mechanic’s ultimate dream would be a fully-powered, wholly customized car of his own. Emacs is just such a car, and its whole design is transparent and open to contributions so if you think you might improve it here and there in its next generation you can just do it. Not to mention it’s also free. The only thing that I currently see might come close is Atom, though with the community and accumulated packages for Emacs I don’t see myself using Atom anytime soon.

I’ve read in quite a few places how “true hackers use Emacs”. I didn’t quite buy into it when I only used Vim. But now I’d say I kind of understand what it means, as I already can’t live without Emacs. Its general esotericism might be one of the hindrances for it and Org mode to attain deserved level of public following proportional to its power, but actually there are already a few nice starter kits out there besides the aforementioned Spacemacs which eases the learning curve. You may find them listed at Awesome Emacs, an “Awesome” style community-driven list of Emacs resources.