COSC402 - Emacs tips

S2 2017

Contents

Some Emacs tips

Here are some useful tips to increase your productivity in Emacs.

Being kind to your eyes

You may not have noticed it, but a dark background shines much less light into your eyes, allowing the iris to retract (relax), meaning you get less eye-strain. Thus, setting a dark desktop background as well as a dark background in your editor/common applications can make it much more comfortable to work. You don’t want the text to contrast too much, so rather than making Emacs white-on-black, I would suggest something like grey90-on-black. Pay attention also to your screen brightness; I have mine turned all the way down (iMac 24", and minimum brightness is still reasonably bright).

You can set all this in Emacs customise mode: from the Options menu, select Customise Emacs > Browse Costumisation Groups. Using the [+] icons, drill down into Faces > Basic Faces > Default. Check the Foreground and Background options, and use “grey90” for the Foreground and “black” for the Background. You can then try it out by clicking on State > Set for Current Session. You can use State > Revert to Save if you don’t like your changes. Use State > Save for Future Sessions when you are happy with it.

You can also use this to change the default font size. Temporary changes can be affected with Shift-Click on a buffer, then selecting one of the fonts shown (as as Misc > fixed).

The Emacs customisation facility is very comprehensive, and there are ways to browse and search around it; some things are easier using it, other things are better done by editing your ~/.emacs file. By the way, the Customise facility edits your ~/.emacs, so you will want to close it before Customise is run.

Kill (C-x k) the customise buffers when you are finished. Clicking on the Finish button doesn’t close the buffer, rather it “buries” it, meaning it puts it at the end of the buffer list…seems like a design flaw.

Keybindings

Emacs has a lot of awkward key-bindings that can be rather awkward to use. It can be useful to (re)bind some commands, especially the ‘compile’, ‘recompile’ and ‘next-error’ commands. You can do this by setting a global (applies to all modes) short-cut. You can make these permanent by putting them in your ‘~/.emacs’ file.

;;; Make compilation easier
(global-set-key (kbd "<f5>") 'recompile)
(global-set-key (kbd "<S-f5>") 'compile)
(global-set-key (kbd "<f6>") 'next-error)
(setq compilation-window-height 15)

(setq compilation-finish-function
      (lambda (buf str)
        (if (string-match "exited abnormally" str)
            (message "compilation errors, press F6 to visit")
          ; no errors, make the compilation window go away after 1.5 sec
          (run-at-time 1.5 nil 'delete-windows-on buf)
          (message "No compilation errors!"))))

These configuration commands also change how the compilation window appears: by default it takes up half the frame and lingers on; I have changed them to take up no more than 15 lines, and disappear after 1.5 seconds. The ‘*compilation*’ buffer is still present, and F6 will still take me to any warnings (by default warnings do not cause an “exited abnormally” situation unless you use the ‘-Werror’ option to your C compiler.

Speedbar

Speedbar is a useful (albeit typically ugly) way of navigating around your files; although I’ve generally not used it much myself because I find ‘C-x C-f’ to be a bit faster for me (that might have something to do with the depth of my directory trees). A friend today put me onto a feature that about Speedbar that I have been wanting for some time: the ability to navigate functions, which is something most serious editors provide, so I figure Emacs must be able to do it easily…somehow. Here’s a screenshot, showing how:

Using speedbar to navigate functions

Basically, ➊ use ‘M-x speedbar’ to get the speedbar frame; ➋ navigate to the file in speedbar and double-click on the icon (it must have a ‘+’ icon); then ➌ click on the part of the file you want to go to.

It doesn’t have to be limited to functions (in a number of different languages), it can be used for section titles in HTML and very likely other things as well, like LaTeX.

Note: you may likely find that some files are not shown. By default, speedbar only shows files that it can delve into (to extract headings etc), so common files like “.css” and “.txt” files are not shown by default; you will need to right click and select “Show all Files”. Alternatively, you can use the Customise facility of Emacs to change the default, as shown in the following animation.

Using Customise to change speedbar defaults

Once you have made the change, kill the buffer with ‘C-x k’.

Buffer switching

You can improve Emacs default buffer management by using ‘iswitchb-mode’ which makes it easier to navigate various buffers from the mini-buffer.

;; Make it easier to navigate buffers
(iswitchb-mode t)
(add-to-list 'iswitchb-buffer-ignore "*Messages*")
(add-to-list 'iswitchb-buffer-ignore "*Completions")
(add-to-list 'iswitchb-buffer-ignore "*ftp ")
(add-to-list 'iswitchb-buffer-ignore "^[tT][aA][gG][sS]$")

If I type ‘C-x b’ I can now type a substring of the buffer name I want: for example, if I have a bunch of ‘*.h’ files open and a few ‘*.c’ files, I might type ‘.c’ (it doesn’t have to be the extention, it can match anywhere). I can use the Tab key to complete also. There are other, fancier improvements, such is ‘ido-mode’ (interactive-do) but I haven’t looked at that yet.