Menu

Category Archives: beginner

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.

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.

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.

When you use one of Emacs’ help commands, another window will open with the documentation. You can use C-M-v and C-M-V to scroll the other window down and up respectively to read the help, without having to switch to that window.

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.

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.

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).

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.

By default, if you highlight text in Emacs and then start typing, the highlighted text is not overwritten as in many other editors. This is easy to change by adding the following to your emacs config file:

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!

A few people have asked about the theme I use in Emacs, as seen in the sreenshots and code snippets I use. It is zenburn, and I find it very easy on the eye, but also has good contrast and a nice colour palette for syntax highlighting.

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.