Category Archives: emacs

Resize your windows to the golden ratio

If you prefer to use a single large emacs frame with multiple windows inside, you might like the golden ratio package. This automatically resizes your windows so that the window containing the point is the largest (size determined by the mathematical golden ratio). This means that the window you are working in is nice and large but you can still see what is going on in the other windows.

Install the package with the following:

(use-package golden-ratio
  :ensure t
  :diminish golden-ratio-mode
  :init
  (golden-ratio-mode 1))

Whizz between windows with windmove

First up, a reminder: in Emacs’ terminology the thing we usually call a window in other apps (the main display of an app with the menus etc at the top) is called a frame, and the individual panes within that (usually displaying different buffers side by side) are called windows.

Once you have been using Emacs for a while, you might find it more efficient to have a single large frame with several windows open inside displaying different buffers. It is then really easy to move between them using the built-in library windmove.

Activate windmove by adding the following to your emacs config file:

(use-package windmove
  ;; :defer 4
  :ensure t
  :config
  ;; use command key on Mac
  (windmove-default-keybindings 'super)
  ;; wrap around at edges
  (setq windmove-wrap-around t))

Now you can hold down the super key (Command on a Mac) and press arrow keys to move to windows in any direction. Note that the default key is shift plus arrow, but this conflicts rather painfully with org-mode, so I prefer super.

Sorting an org-mode table

You can quickly sort tables in org-mode by using C-c ^ with the point inside a table. You’ll be prompted for a sorting type, where you can choose e.g. a for alphabetic or n for numeric. You can use capital letter versions of these options to reverse the sort.

The table is sorted based on the column that the point is in, and from the documentation:

The range of lines is the range between the nearest horizontal separator lines, or the entire table of no such lines exist.

Use org-mode tables and structures in emails and elsewhere

I love the way that org-mode allows you to add simple clean structures to your text, with lists and tables. You can get some of that functionality in other modes by using orgstruct-mode and orgtbl-mode, which are part of org-mode.

Enable these minor modes in any major mode for one-off use with M-x orgstruct++-mode or M-x orgtbl-mode and you can use the normal org-mode commands to create lists and tables. I find this especially useful in emails, so I use this code in my emacs config file to automatically enable these for message-mode

;; use org structures and tables in message mode
(add-hook 'message-mode-hook 'turn-on-orgtbl)
(add-hook 'message-mode-hook 'turn-on-orgstruct++)

Cancel all timers calling some function

This is a bit of a niche post but I recently found I had accidentally started a bunch of duplicate timers all running the same function. I hadn’t recorded the timer objects for them so couldn’t use cancel-timer to kill them. After a bit of searching I discovered the function cancel-function-timers which will cancel all timers that call a particular function.

Not something you’ll be using every day, but might be useful one day!

A persistent scratch buffer

The *scratch* buffer is useful for dumping temporary bits of text, or trying out bits of lisp code. By default the contents are not saved, but it can be handy to have the scratch buffer saved and restored when you start a new Emacs session. This is easy with the persistent-scratch package, just add the following to your emacs config file:

;; persistent-scratch
(use-package persistent-scratch
  :config
  (persistent-scratch-setup-default))

Add the system clipboard to the Emacs kill-ring

This post was originally titled “Prevent Emacs wiping the system clipboard”, which was a rubbish description of what this tip actually covers, so I renamed it! Apologies if you see it twice in your RSS reader.

I wrote previously about adding mouse selections in Emacs to the system clipboard, and here is another tip to integrate the system clipboard more nicely with Emacs. This comes from the fantastic Emacs operating system set of configuration files, which are full of gems like this (thanks to Irreal for pointing me to EOS).

By default, if you copy something to the system clipboard (e.g. some text in firefox) and then copy or kill some text in Emacs, then the text from firefox is lost. If you set the option below in your emacs config file then copying or killing text in Emacs will add the system clipboard text to the kill-ring so that you can find it when you cycle through your clipboard history in Emacs.

;; Save whatever’s in the current (system) clipboard before
;; replacing it with the Emacs’ text.
;; https://github.com/dakrone/eos/blob/master/eos.org
(setq save-interprogram-paste-before-kill t)

Copy and paste files with dired-ranger

I’ve written before about managing files in Emacs using dired. The package dired-ranger provides a useful extension to dired, allowing you to copy and paste files much like you can do in traditional GUI file explorers.

First, install dired-ranger with something like the following

(use-package dired-ranger
  :ensure t
  :bind (:map dired-mode-map
              ("W" . dired-ranger-copy)
              ("X" . dired-ranger-move)
              ("Y" . dired-ranger-paste)))

This also sets up some useful keybindings. Now in a dired buffer, you can mark multiple files and then hit W to copy them (really they are added to a copy ring). You could then optionally go to another directory and mark more files and hit C-u W to add those to the same entry in the copy ring as the previous files. This builds up a virtual collection of files that you can then copy or move. Now go to the target directory and hit X to move the copied files to that directory (i.e. they are deleted from their original location) or Y to copy the files to the target directory (the originals remain where they were).

You can achieve similar results using dired-dwim-target or Sunrise Commander, but this method clicks with me and is the one that I use.