beginner

Use your digits and a personal key map for super shortcuts

Lots of commands in Emacs behave differently if called with a prefix argument C-u. For example, by default C-s runs the command isearch which searches for text as you type. If you use a prefix argument C-u C-s then isearch searches with regular expressions instead of literal string text.

You can also pass numerical arguments to commands, which for many simple commands will cause the command to repeat that number of times, so for example C-u 3 C-p passes the argument 3 to the command C-p which then runs previous-line three times. What is more, you can also pass these numerical arguments with the form C-3 C-p, where C-3 is equivalent to C-u 3. Of course this works for any digit 0-9, not just 3!

By default, Emacs uses the combinations C-3, M-3, C-M-3 all for this purpose, again for all digits 0-9. The thing is, I simply never use them in this way, and this is a huge waste of really useful keyboard shortcuts you could be using for other things.

To reclaim them, add the following to your emacs config file, with credit to this reddit post:

;; unset C- and M- digit keys
(dotimes (n 10)
  (global-unset-key (kbd (format "C-%d" n)))
  (global-unset-key (kbd (format "M-%d" n)))
  )

You can then still access the digit prefixes with C-M-3 and similar but you now have all the C- and M- digit combinations to play with.

One great way to make use of this is to set up your own key map using one of these newly freed-up combinations. I set C-1 as the prefix key for my own map:

;; set up my own map
(define-prefix-command 'bjm-map)
(global-set-key (kbd "C-1") 'bjm-map)

Then I assign some keys to that map, for example

(define-key bjm-map (kbd "m") 'mu4e)
(define-key bjm-map (kbd "g") 'bjm/open-gcal-agenda)

Now I can use C-1 m to open mu4e for my email, and C-1 g to view my google calendar.

Advertisement

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.

Use bookmarks to jump to files or directories

In Emacs you can bookmark files and directories (and lots of other things) so that you can quickly jump to them (similar to a browser’s bookmarks).

The basics are easy. Use C-x r m to make a bookmark to the file or directory you are currently visiting. You’ll be prompted for an optional name for your bookmark. For example, I use names starting dir- for bookmarks to directories so that they all appear together in the bookmark list.

You can use C-x r b to go to a bookmark, and you’ll prompted for the name of the bookmark. Use C-x r l to list all of the bookmarks.

There is a bookmarks+ package which adds extra features to the normal bookmarks, but I’ve not found that I need those extras so far.

Find and open files from anywhere with helm-for-files

Helm is an extremely powerful package that provides a search interface that narrows the matches as you type. What makes it powerful is the range of back-ends that you can access through helm. I don’t use it for a lot of things (I slightly prefer ivy in general), but one of my favourites is helm-for-files. This gives you a search interface where you start typing the name of a file and it narrows a list of files from multiple useful sources: currently opened files; recent files; bookmarked files; files in the current directory; and files anywhere on your system using your system’s locate command. Basically, it find any file anywhere on your system, showing you the most likely matches first. Hitting RET to select a file opens it in Emacs as usual.

Here is how I install and configure helm for this purpose

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; helm                                                                   ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(use-package helm
  :ensure t
  :init
  (progn
    (require 'helm-config)
    ;; limit max number of matches displayed for speed
    (setq helm-candidate-number-limit 100)
    ;; ignore boring files like .o and .a
    (setq helm-ff-skip-boring-files t)
    ;; replace locate with spotlight on Mac
    (setq helm-locate-command "mdfind -name %s %s"))
  :bind (("C-x f" . helm-for-files)))

Since I use a Mac, I tell helm to use mdfind which is the command line interface to the spotlight indexer in place of the normal locate command. Finally, I bind the command to C-x f to serve as a memorable alternative for C-x C-f (the default interface for opening files).

Macro Counters

I’ve posted about using keyboard macros to record and play back repetitive tasks. Macros include a counter which lets you insert numerical values that increment each time the macro is called.

For example, go to a new line and start a macro with C-x ( and then hit C-a to move to the start of the line, C-x C-k C-i to insert the macro counter (initially zero) and then RET to go to a new line and C-x ) to stop the recording. Run the macro a few times with C-x e and then just e to repeat the macro and you’ll get something like this:

0
1
2
3
4

The counter starts at zero every time you define a new macro. To set it to another value, use C-x C-k C-c before defining or invoking a macro.

Uniquify your buffer names

If you open more than one file that has the same name (say test.txt), then by default Emacs will add a number to the end of the buffer name to distinguish them, so you would see test.txt <1> and test.txt <2> and so on. This is not very useful as it is easy to lose track of which file is which.

Luckily it is easy to fix with some simple tweaks (I’ve taken these from the configuration files for prelude). Add these to your emacs config file and your buffer names will be made unique by adding just enough of the path to the file. So you might see docs/test.txt and scratch/test.txt. Much nicer!

;; meaningful names for buffers with the same name
;; from prelude
;; https://github.com/bbatsov/prelude
(require 'uniquify)
(setq uniquify-buffer-name-style 'forward)
(setq uniquify-separator "/")
(setq uniquify-after-kill-buffer-p t)    ; rename after killing uniquified
(setq uniquify-ignore-buffers-re "^\\*") ; don't muck with special buffers

Super-efficient movement using avy

One of the revelations I came to after a while of using emacs is that you can use searching (or swiping) to efficiently move to another place in the visible buffer. In other words, you can see the place that you want the cursor to be so you do a search for a word close to that position to move the cursor there – not because you want to find that word.

The package avy gives an even more efficient way to do this. There are a few options, but with the configuration below, I look at the place I want the cursor to be, hit M-s and type the first character of a word close to that position, and then the short string that appears in order to select the word that I want and the cursor jumps there. Once you get used to it, it almost feels like you can move the cursor just by looking where you want it to go!

In the animation below I want to move the cursor to the start of the word “formed” near the bottom of the window, so I hit M-s and then f. avy then overlays letter combinations on all words starting f and I type “la” to move to the word I want.

avy.gif

Here is my configuration code for avy

(use-package avy
  :ensure t
  :bind (("M-s" . avy-goto-word-1)))