Fun with fonts

I’ve been playing around with some different fonts to see how they look in Emacs. For a long time I’ve been using DejaVu Sans Mono, but I felt like a change. It’s easy to switch the font for the current frame, just use M-x set-frame-font and enter the name of an installed font, or put a line like this

(set-frame-font "DejaVu Sans Mono-14" nil t)

in your scratch buffer and put the cursor at the end of the line, and use C-x C-e to run eval-last-sexp which evaluates that bit of code. This will instantly change the appearance of the current frame.

Here are some of the fonts I’ve been trying out (I installed them using the Font Book on my Mac):

(set-frame-font "DejaVu Sans Mono-14" nil t)
(set-frame-font "Fantasque Sans Mono-16" nil t)
(set-frame-font "Source Code Pro-14" nil t)
(set-frame-font "Monaco-14" nil t)
(set-frame-font "Cousine-14" nil t)

I’ve decided to go with Google’s Cousine font at the moment, so I add the following to my emacs config file to make the choice permanent:

(setq default-frame-alist '((font . "Cousine-14")))

mu4e-delay is dead, long live mu4e-send-delay

A while ago I wrote mu4e-delay, a package (based heavily on gnus-delay), to add a customisable delay to outgoing mail so that a sent email could be “undone” before the delay period had passed. This was spectacularly useful for me, but now Benny Andresen has put together the superior mu4e-send-delay, which improves on my package in several ways.

The key features of mu4e-send-delay are (from the github page):

  • mu4e context support
  • Saves scheduled mails to mu4e-drafts-folder
  • Uses an emacs timer to check Drafts if a mail is scheduled to be sent now
  • Allows easy edit of the X-Delay header in mu4e-compose-mode
  • Displays scheduled time in mu4e-view
  • Doesn’t send if mail is currently being edited
  • Works with attachments

The last four points are all improvements over the original mu4e-delay, with the last two being the most important. I’d encourage any mu4e-delay users to switch over to mu4e-send-delay. I have, and I’ve not looked back!

Transpose a table in org-mode

I recently needed to transpose a table in org-mode and spent a few minutes trying to come up with a keyboard macro to do it before it occurred to me that there might be a command to do this already. And of course there was: M-x org-table-transpose-table-at-point. Here it is in action:


It’s great when there’s a command that does exactly what you want!

Open files with the system default application

I’ve mentioned crux before; it’s a package providing a set of general-purpose useful commands. One that I use all the time is crux-open-with, which opens the file currently being visited (or the file at the point in a dired buffer) using the system default application for that filetype. It works on Mac or Linux, by using the open or xdg-open commands respectively.

I bind the command to C-c o, using the following code (which also binds the previously mentioned crux-move-beginning-of-line)

(use-package crux
  :bind (("C-c o" . crux-open-with)
         ("C-a" . crux-move-beginning-of-line)))

Don’t kill-buffer, kill-this-buffer instead

Here’s a tiny tip. By default C-x k runs the command kill-buffer which prompts you for which buffer you want to kill, defaulting to the current active buffer. I don’t know about you, but I rarely want to kill a different buffer than the one I am looking at, so I rebind C-x k to kill-this-buffer which just kills the current buffer without prompting (unless there are unsaved changes).

Just add this to your emacs config file

(global-set-key (kbd "C-x k") 'kill-this-buffer)

Star and unstar articles in elfeed

Following on from a couple of previous posts on elfeed, the excellent feed reader for Emacs, I wanted to share some code to enable the starring and unstarring of articles for future reference.

This code is closely based on this article, so all credit should go in that direction. My only addition is to add the ability to unstar an article.

;; code to add and remove a starred tag to elfeed article
;; based on

;; add a star
(defun bjm/elfeed-star ()
  "Apply starred to all selected entries."
  (interactive )
  (let* ((entries (elfeed-search-selected))
         (tag (intern "starred")))

    (cl-loop for entry in entries do (elfeed-tag entry tag))
    (mapc #'elfeed-search-update-entry entries)
    (unless (use-region-p) (forward-line))))

;; remove a start
(defun bjm/elfeed-unstar ()
  "Remove starred tag from all selected entries."
  (interactive )
  (let* ((entries (elfeed-search-selected))
         (tag (intern "starred")))

    (cl-loop for entry in entries do (elfeed-untag entry tag))
    (mapc #'elfeed-search-update-entry entries)
    (unless (use-region-p) (forward-line))))

;; face for starred articles
(defface elfeed-search-starred-title-face
  '((t :foreground "#f77"))
  "Marks a starred Elfeed entry.")

(push '(starred elfeed-search-starred-title-face) elfeed-search-face-alist)

This code sets up the required functions to add and remove the “starred” tag from articles, and sets a face colour to indicate articles that have been starred.

I bind these to the keys “*” to add a star and “8” to remove the star (easy to remember):

;; add keybindings
(eval-after-load 'elfeed-search
  '(define-key elfeed-search-mode-map (kbd "*") 'bjm/elfeed-star))
(eval-after-load 'elfeed-search
  '(define-key elfeed-search-mode-map (kbd "8") 'bjm/elfeed-unstar))

Now, entering the search filter “+starred” in elfeed will show the starred articles, and as described previously you can save a bookmark to that filter (I called mine “elfeed-starred”) and assign a key like “S” to jump directly to the starred articles:

;;shortcut to jump to starred bookmark
(defun bjm/elfeed-show-starred ()
  (bookmark-jump "elfeed-starred"))
(define-key elfeed-search-mode-map (kbd "S") 'bjm/elfeed-show-starred)

Update 2016-09-27

Commenter Galrog has a much simpler implementation. I’ve copied it here so the formatting is nicer:

(defalias 'elfeed-toggle-star
  (elfeed-expose #'elfeed-search-toggle-all 'star))

(eval-after-load 'elfeed-search
  '(define-key elfeed-search-mode-map (kbd "m") 'elfeed-toggle-star))

Counsel-yank-pop with a tweak

Abo Abo’s counsel package (part of swiper/ivy) puts together a set of replacements for Emacs commands that leverages the power of the ivy completion library.

One of my favourites is counsel-yank-pop which replaces the standard clipboard history (kill-ring in Emacs terminology) with an ivy-powered version. You can then type search strings to filter your clipboard history dynamically.

I use the following code to configure counsel-yank-pop to replace the standard yank-pop on M-y. The only thing I missed about the vanilla yank-pop was that repeatedly pressing M-y cycled through the entries. The counsel version doesn’t do this by default but this is easy to add by binding M-y to ivy-next-line in the ivy-minibuffer-map.

(use-package counsel
  (("M-y" . counsel-yank-pop)
   :map ivy-minibuffer-map
   ("M-y" . ivy-next-line)))

Automatically copy text selected with the mouse

I’m not much of a mouse user, but a colleague using emacs for OS X wanted to replicate the normal X11 behaviour that linux users are familiar with – text selected with the mouse is automatically copied to the system clipboard. It turns out to be as easy as adding one line to your emacs config file:

(setq mouse-drag-copy-region t)

From the help for the variable (C-h v mouse-drag-copy-region)

If non-nil, copy to kill-ring upon mouse adjustments of the region.

In other words, highlight a block of text and it is copied to the kill-ring (Emacs’ internal clipboard). This also copies to the system clipboard on the Mac so you can then paste to other apps.