Category Archives: geek

On this page:

Emacs Basics: Call commands by name with M-x (with tips for better completion using ido or helm)

This entry is part 2 of 3 in the series Emacs Basics

Emacs has way too many keyboard shortcuts to memorize. Fortunately, you can call commands by name by typing M-x and the name of the command. M- stands for the Meta key. If your keyboard does not have a Meta key (and most don’t, these days), use Alt or Option. For example, on a PC keyboard, you can type Alt-x. Alternatively, you can replace Meta with ESC. M-x then becomes ESC x.

If you know the name of the command to execute, you can type it after M-x, and then press RET (the Return key, which is the same as the Enter key). For example, M-x find-file opens a file. M-x save-buffer saves the current file. You can use TAB to complete words. Use <up> and <down> to go through your command history.

What if you don’t know the name of the command to execute? You can use M-x apropos-command to search for the command using keywords. If you know the keyboard shortcut or you can find the command on a menu, you can also use M-x describe-key and then do the keyboard shortcut or select it from the menu.

If a command you execute has a keyboard shortcut, it will flash briefly at the bottom of your screen. For example:

You can run the command `find-file' with C-x C-f

Using TAB for completion can be a little slow. Here are two ways to make that and a whole lot of other things faster: ido and helm. To explore these approaches, you will need to add the MELPA package repository to your configuration. To set that up, add the following to the beginning of your ~/.emacs.d/init.el file.

(package-initialize)
(add-to-list 'package-archives '("melpa" . "http://melpa.milkbox.net/packages/") t)

Then use M-x eval-buffer to load the changes into your current Emacs, and use M-x package-refresh-contents to reload the list of packages.

Helm mode

This is what completion with Helm looks like:

2014-03-17 13_06_54-c__sacha_personal_organizer.org.png

Figure 2: Helm

Use M-x package-install to install the helm package. Then you can try it out with M-x helm-mode . After you start Helm mode, try M-x again. You can type in multiple words to search for a command, and you can use <up> and <down> to go through completions. Use M-p and M-n to go through your command history.

If you like it, here’s some code that you can add to your ~/.emacs.d/init.el file to load it automatically next time, and to tweak it for more convenience.

(require 'helm-config) 
(helm-mode 1)

Use M-x eval-buffer to load your changes.

If you change your mind and want to disable helm-mode, you can toggle it off with M-x helm-mode .

Ido, ido-hacks, smex, ido-vertical-mode, and flx-ido

Ido is like Helm, but it takes a different approach. Here’s what this combination will get you:

2014-03-17 12_40_40-MELPA.png

Figure 1: ido, smex, ido-vertical-mode, and flx-ido

If you want to give this a try, remove or comment out (helm-mode 1) from your ~/.emacs.d/init.el (if you added it), and disable helm-mode if you still have it active from the previous section.

To set Ido up, use M-x package-install to install ido, smex, ido-vertical-mode, ido-hacks, and flx-ido.

After the packages are installed, add the following code to your ~/.emacs.d/init.el .

(ido-mode 1)
(require 'ido-hacks nil t)
(if (commandp 'ido-vertical-mode) 
    (progn
      (ido-vertical-mode 1)
      (setq ido-vertical-define-keys 'C-n-C-p-up-down-left-right)))
(if (commandp 'smex)
    (global-set-key (kbd "M-x") 'smex))
(if (commandp 'flx-ido-mode)
    (flx-ido-mode 1))

Use M-x eval-buffer to load your changes, then try M-x again. You should now have much better completion. You’ll be able to call commands by typing in part of their names. Use <up> and <down> to go through the completion options, and use <left> and <right> to go through your history.

Try it for a week. If you like it, keep it. If you don’t like it, try the Helm approach.

Other tips

When you learn keyboard shortcuts, try to remember the names of the commands as well. You can do that with C-h k (describe-key). For example, M-x calls the command execute-extended-command. That way, even if you forget the keyboard shortcut, you can call the command by name.

If you forget the name of the command and you don’t know the keyboard shortcut for it, you can look for it in the menus or in the help file. You can open the help file with C-h i (info). You can also use M-x apropos-command to search through the commands that you can call with M-x.

Make your own cheat sheet with frequently-used keyboard shortcuts and commands to help you learn more about Emacs. Good luck!

Emacs Basics: M-x

Emacs Basics: M-x

You can download the MP3 from archive.org.

Emacs tweaks: Export Org checkboxes using UTF-8 symbols

UPDATE 2014-03-28: Newer versions of org have the org-html-checkbox-type variable, which you can set to unicode. Use M-x customize-variable org-html-checkbox-type to see if you have it.

This snippet turns - [X] into ☑ and - [ ] into ☐.

(defun sacha/org-html-checkbox (checkbox)
  "Format CHECKBOX into HTML."
  (case checkbox (on "<span class=\"check\">&#x2611;</span>") ; checkbox (checked)
        (off "<span class=\"checkbox\">&#x2610;</span>")
        (trans "<code>[-]</code>")
        (t "")))
(defadvice org-html-checkbox (around sacha activate)
  (setq ad-return-value (sacha/org-html-checkbox (ad-get-arg 0))))

To find this code, I searched ox-html.el for [. Eventually I found org-html-checkbox, which is directly called by org-html-format-list-item instead of being a function variable that you can change. So that meant I needed to override the behaviour of org-html-checkbox through defadvice. You can see above how I wrap advice around org-html-checkbox and replace the return value with my own function. For more about advice, read the Emacs Lisp Intro manual.

To find the hex codes for the UTF-8 characters, I searched Google for UTF-8 checkbox and found BALLOT BOX WITH CHECK. I used the hex code so that I didn’t have to worry about encoding issues. I tested it by updating one of my weekly reviews. Tada!

Inspired by Grant from Wisdom and Wonder.

Emacs Basics: Using the mouse

This entry is part 1 of 3 in the series Emacs Basics

You can download the MP3 from archive.org.

Transcript:

I’m Sacha Chua and this is an Emacs Basics episode on using the mouse. The best way to use Emacs is to master the keyboard shortcuts, but when you’re starting out, don’t worry about them yet. You might find yourself using the mouse a whole lot more than you used to, but over time, you will learn more and more keyboard shortcuts as you get used to Emacs. So let’s say that you’re just starting out. What are some of the things that you can do right away to get the hang of using Emacs?

The Emacs tutorial is a great place to start. You can get to that by clicking on the Emacs tutorial link on the splash screen. If you’ve done the tutorial before, it will offer to let you resume at that point. If you don’t have the splash screen handy, you can also get to the tutorial from Help > Emacs tutorial. Go through this and you’ll learn a lot of the common keyboard shortcuts that will come in handy.

The toolbar and the menu will also give you quick access to a lot of common commands. If you’d like to create a new file or open an existing file, you can click on the New file icon located in the top left. You can specify the file, and if the file doesn’t exist yet, it will create it. To save the file, click on the Save icon. If you’d like to close a file, just click on the X mark. You can open the file again using the toolbar icon.

To copy and paste, use your mouse to select a region of text, then copy or cut it. Then you can paste it wherever you want. You could also search for text. Click on that search button and start typing what you’re looking for. It will highlight the search results. Press Ctrl+s to search for the next instance. Press Enter to stop searching.

The menus also offer a lot of other commands. For example, you can insert a file. You can save the current buffer with another file name. You can split your windows so you can see more than one file at the same time. If you’d like to close a window and go back to having one file across your entire screen, you just have to use File > Remove other windows. To switch between the files you have open, use the Buffers menu. Explore the menus for other options.

One of the interesting things in Emacs is that you can copy or cut multiple things and then paste them without having to keep copying or cutting each time. For example, if I copy this, then Paste will paste that. But you could also access the things that you copied or cut previously. Just click on Edit > Paste from kill menu, then select the item you want to paste.

There are lots of other tools that are available in Emacs. The availability of these tools may depend on what else you’ve installed. Again, for more information, check out the Emacs tutorial or read the Emacs manual.

Have fun!

 

Emacs, Evernote (through enscript.exe), and Org links

I’ve given myself permission to spend an hour or two tickling my brain with various technical ideas and prototypes every day. This means Emacs geekery is going to turn up on my blog more often. =) Here’s a recent hack that I put together to make my weekly reviews a little easier.

I skim a lot of blog posts on my phone using the Feedly app. (See: How to read blogs efficiently with a feed reader.) I save the posts I want to follow up on or include in my weekly round-up. I have an If This Then That recipe that monitors my saved items and stashes them in Evernote with the roundup tag. If I come across other interesting pages while browsing on my computer, I use the Evernote Web Clipper to save those pages with the roundup tag as well.

In the past, I selected a range of notes to export from Evernote, saved them to a file, and used my sacha/evernote-extract-links-for-review function to extract the titles and URLs. I opened each of the pages with C-c C-o (org-open-at-point) to refresh my memory and follow up. I deleted lines that were no longer relevant. Since IFTTT had changed to rewriting the URLs instead of leaving the source URLs alone, I copied the original URLs and replaced the links that were in Org.

This is Emacs, though, and even that can be smoothened with a little scripting. Now I can use the code below to expand URLs and to open all the URLs in the region.

Link-related convenience functions

I picked this up from http://www.emacswiki.org/emacs/AlexSchroederConfigOrientalibombus .

(defun kensanata/resolve-redirect (url)
  "Resolve shortened URL by launching `curl --head' and parsing the result."
  (let* ((curl (shell-command-to-string
                (format "curl --silent --head %s" url)))
         (location (when (and (string-match "\\`HTTP/1\.1 301" curl)
                              (string-match "^Location: \\(.*\\)" curl))
                     (match-string 1 curl))))
    (or location url)))

(defun sacha/resolve-urls-in-region (beg end)
  "Expand URLs between BEG and END."
  (interactive "r")
  (save-excursion
    (save-restriction
      (narrow-to-region beg end)
      (goto-char (point-min))
      (while (re-search-forward org-bracket-link-regexp nil t)
        (replace-match (save-match-data (kensanata/resolve-redirect
                                         (match-string 1))) t t nil 1))
      (goto-char (point-min))
      (while (re-search-forward org-link-re-with-space nil t)
        (replace-match (save-match-data (kensanata/resolve-redirect
                                         (match-string 0))) t t nil)))))

(defun sacha/open-urls-in-region (beg end)
  "Open URLs between BEG and END."
  (interactive "r")
  (save-excursion
    (save-restriction
      (narrow-to-region beg end)
      (goto-char (point-min))
      (while (re-search-forward org-plain-link-re nil t)
        (org-open-at-point)))))

Evernote-related extract

Evernote on Windows doesn’t have the same kind of scripting capabilities that Evernote on the Mac has, but it turns out you can still do a fair bit of scripting with the enscript tool.

(defun sacha/evernote-export-and-extract (start-date end-date)
  "Extract notes created on or after START-DATE and before END-DATE."
  (let ((filename "c:/sacha/tmp/Evernote.enex"))
    (call-process 
     "c:/Program Files (x86)/Evernote/Evernote/enscript.exe"
     nil t t
     "exportNotes"
     "/q" (concat
           " tag:roundup"
           " created:" (replace-regexp-in-string "-" "" start-date)
           " -created:" (replace-regexp-in-string "-" "" end-date))
     "/f" filename)
    (sacha/evernote-extract-links-for-review filename)))

(defun sacha/evernote-extract-links-for-review (filename)
  "Extract note names and URLs from FILENAME.
     The file should be an ENEX export."
  (interactive (list (read-file-name "File: ")
                     (org-read-date)
                     (org-read-date)))
  (let (list)
    (with-temp-buffer
      (insert-file-contents filename)
      (goto-char (point-min))
      (while (re-search-forward "<title>\\(.+?\\)</title>\\(.*?\n\\)*?.*?href=\"\\(.*?\\)\"" nil t)
        (setq list
              (cons
               (cons
                (match-string-no-properties 1)
                (match-string-no-properties 3)) list))))
    (setq list
          (mapconcat (lambda (x)
                       (concat "- [["
                               (kensanata/resolve-redirect (cdr x))
                               "][" (car x) "]]: ")) list "\n"))
          (if (called-interactively-p 'any)
              (insert list)
            list)))

Let’s see how this new workflow goes. =) If you’re curious, you can check out the rest of my weekly-review-related code in my Emacs configuration.

Drafting a baby-steps guide to managing your tasks with Org Mode for Emacs

Org mode for Emacs is powerful and flexible, which can make it intimidating for newcomers. After helping several people with essentially the same problem–an unmanageably large heap of tasks–I thought about what might help people get the hang of the key features of Org Mode slowly.

Here are some general ideas. Start by writing your tasks down. Group them into projects. Once you get the hang of that, schedule your tasks. You might find yourself overestimating what you can do in a day, so reschedule or get rid of tasks as needed.

2014-02-08 A path toward taming your TODO list

2014-02-08 A path toward taming your TODO list

Here’s a visual overview of how you can apply that to Org, starting with simple outlines and moving on to scheduling.

2014-02-16 Org TODO basics

2014-02-16 Org TODO basics

I’ve started to put together an outline/draft for A Baby Steps Guide to Managing Your Tasks with Org Mode, which you can find at sach.ac/baby-steps-org-todo. Comments and questions welcome!

New free/pay what you want resource: Sketchnotes 2013; also, Emacs Dired rocks

cover

Get your copy of the Sketchnotes 2013 collection

Since people found my collection of sketchnotes from 2012 handy, I’ve put together a categorized collection of sketchnotes from 2013 as well. Enjoy! =)

Behind the scenes

This was how I made the 2012 collection:

  1. Create a Microsoft Powerpoint presentation. Fill it with high-res images. Resize and position all the images. Use AutoHotkey to save myself time and avoid going crazy.
  2. Create a spreadsheet with titles and page numbers. Add captions with liberal use of AutoHotkey.
  3. Create a manual table of contents and link to all the images. Mostly use AutoHotkey, except for the part where if you create a link to a slide number that consists of repeated numbers (ex: 55 or 66), you have to select it a different way, because typing “55″ gets you #51 (and “555″ gets you #52, etc).
  4. Save as PDF.

There was a lot of manual fiddling around involved in making that collection, so I’m experimenting with a different approach that may be useful. For Sketchnotes 2013, I wanted to see if there were ways I could simplify the packaging process while enabling people to do other things with the files.

Here’s what I did:

  1. I used Emacs dired-mode’s C-x C-q (dired-toggle-read-only) to go into editable mode, which allowed me to easily edit all the filenames to include #keywords. I used C-x C-q to save the changes.
  2. Then I used Emacs dired’s % m to select multiple files by regular expressions and R to move the files into a specified directory.
  3. Tada! Neatly organized files. I packaged it up as a ZIP and put it on Gumroad.
  4. Since Dropbox also allows you to share folders, I created a public link to the folder that had my organized sketches. That way, people can download a single directory if they want to, instead of downloading all 250+ MB.

It still might be interesting to make a PDF, especially if I can make one that can be published through something like CreateSpace. More packaging… =)