Emacs Emacs Workout

Mouse and Emacs

Okay, if you’re using Emacs properly you won’t be using the mouse. You know that you waste time moving your hand from the keyboard to the mouse and back again.

But there will be times when you have the mouse in your hand anyway, so whilst it’s there you might want to remember these handy shortcuts…


Ctrl and mouse 1 brings up the buffer list. Slide that mouse down context menu to quickly change buffer

Selecting Text

The following are worth remembering:
  • Double clicking on a parenthesis selects up to the matching parenthesis
  • Double clicking on a word selects it
  • Triple clicking selects a line

Mouse-1, Mouse-2, Mouse-3

I’m right handed. On my computer, Mouse-1 is left click, Mouse-2 is the scroll wheel and Mouse-3 is right click
Mouse-1 sets the point
Dragging the mouse not only sets the region, it also copies it to the Primary Selection, ready to be inserted by Mouse-2
Mouse-3 extends the current selection up to the point clicked. Mouse-3 a second time deletes the selection. This doesn’t work in all modes, however.
Emacs Emacs Workout


See the aardvark, a few lines down? It’s worth remembering that it’s often faster get to it by using C-s to search than by moving the cursor. Emacs expects you to search frequently, that’s why searching takes less key presses than C-x s, the save command.

Here’s a reminder of the search commands:

C-s C-w         Search for word after point
C-s C-s         Repeat last search
C-s C-y         Search line
C-s M-y         Search last kill
C-u C-s         Regexp Search
C-u C-r         Regexp Reverse Search

Emacs incremental search can occasionally be a pain. Don’t forget you can search non-incrementally for a specific word such as elephant using

M-s w <RET> elephant <RET>

Here’s the aardvark, by the way.

Emacs Emacs Workout

Calc Mode 1: Binary Numbers

Thanks to Andrew Hyatt for inspiring this entry

Here’s how to use Emacs to convert 8 numbers to binary. Why? Read on…

  1. Write the numbers: 127, 54, 32, 178, 199, 244, 3, 255
  2. Mark the region of the numbers and hit C-x * g. This grabs the numbers into calc mode as a vector
  3. In calc mode, hit d 2 to display the numbers in binary
  4. Still in calc mode, hit d z to show the leading zeroes. This shows them as 32 bit words
  5. Hit b w 8 to set the word size to 8 bits.
  6. Finally, and still in calc mode, hit y to yank the numbers back into the last buffer you were using.
  7. Add a table, tidy things up, and you’ve got a worksheet like the one below for a set of students and the answers for yourself.

Convert the 8 bit unsigned numbers to denary

  1. 01111111
  2. 00110110
  3. 00100000
  4. 10110010
  5. 11000111
  6. 11110100
  7. 00000011
  8. 11111111
Emacs Emacs Workout

Emacs Characters

Who would have thought that three dots could cause so many problems?

Most of the Aethernet Magazine writers use the ellipsis …

The Guardian Style Guide suggests leaving a space before and after the ellipsis.

In the Aethernet style guide the ellipsis is always followed by a space

“Is it… ?”

“Yes… ”

Preparing texts proves to be an interesting Emacs exercise.

It’s easy to search for three dots … , but there also exists a unicode character …
Now, as everyone knows, Emacs characters are saved as integers. You can insert any character on Emacs by hitting C-q and then the appropriate code in octal, in this case:

C-q 20046       gives …

If you want to enter the codes in denary, evaluate the following: (setq read-quoted-char-radix 10)

Evaluate the above and now…

C-q 8230 gives  …

Better yet, place the code in your .emacs file. Set the value to 16 if you want to quote in hexadecimal.

It’s easy to find a character’s value:

C-x = gives information about a character under the point.

For example, the ellipsis gives

Char: … (8230, #o20046, #x2026, file ...)

There are easier ways to insert non-keyboard characters using C-x 8

C-x 8 ' e prints é
C-x 8 `e prints è
C-x 8 ^ e prints ê
C-x 8 " u prints ü
C-x 8 / / prints ÷
C-x 8 C prints © copyright

The above are just a few examples

C-x 8 C-h to see all characters

If you want to hurry (dépêcher in French) you might get tired of typing C-x 8 all the time. The following allows you to omit the C-x 8 section

M-x set-input-method latin-9-prefix

Okay, what now… ?

Emacs Emacs Workout

Repeating Commands

Repeating Commands

C-x z   Repeats the last command.  Hitting z repeatedly keeps on repeating that command.

Once you know this trick, many other apparently complicated Emacs commands suddenly become clear. For example, why is the command to widen a window the extremely time consuming C-x }? That takes four keypresses on my keyboard, as I have to hold down shift to access the }. Surely it’s faster to use a mouse?
Not if you remember C-x z. Try it now:

C-x 3   Split into two vertical windows
C-x }   Widen the window one space
C-x z   Repeat the last command.  Now hit z until the window is wide enough.

Granted, this is still pretty complicated at first sight. If you’ve stumbled across this page whilst looking for something else you’re probably wondering why on Earth anyone would want to use Emacs, but that’s to miss the point. C-x z is another useful command that you can chain together with others to produce new results.

Whilst we’re here, here’s some useful window commands to remember

C-x -   Shrink the window to the size of the buffer text, if it's smaller
C-x +   Make windows the same size
C-x 4 0 Kill buffer and window
C-M-v   Scroll other window down
S-C-M-v Scroll other window up


Emacs Emacs Workout

Regexp Builder

Emacs has an interactive regex mode that shows matches as you type.

To go into the mode, M-x regexp-builder.  Type C-c C-q to exit the mode.

Watch out for escape characters.  Emacs requires you to escape \, so type \b for \b

Copy the following text into emacs and then M-x regexp-builder

Lots of Grey things
50 shades of Grey
Earl Grey Tea
Lady Grey
Graybeard the pirate
Greyhound buses
"You're looking grey," he said.
"That's the greyest greyhound I ever saw," said Earl Grey.
Grey1: Dark Gray
Grey35: Mid grey
Grey44: Battleship grey
Grey100: The colour of TV tuned to a dead channel

In regexp-builder, type…

Grey to find all the Greys
Gray to find all the Grays
Gr[ae]y to find all the Greys and Grays
\\bGray\\b to find Gray on its on and not part of another word
^Gray to find lines beginning with Gray
grey$ to find lines ending in grey
grey\\(hound\\)? to find all appearances of grey and greyhound
grey[0-9] to find all the shades of grey ending with a digit
grey[0-9]\\{2\\} to find all the shades of grey with exactly two digits

Follow the link for some Regexp Exercises to try out

Read more about Regexps at the Emacs Wiki

Emacs Emacs Workout


I never use the rectangle commands as often as I should.

Here’s my shopping list:

  • Bread
  • Cheese
  • Milk
  • Yoghurt

I want to make the above into a list. There are all sorts of ways I could so this, one way is to use the string-rectangle command. Set (or highlight) the region to zero width in front of the list and then C-x r t – to insert a dash before each of the words.

You can use the kill-rectangle command to remove unwanted bullet points (particularly useful when importing text from PDFs)

Set the region to cover the bullet symbols and then C-x r k to kill the rectangle. Don’t forget you can C-x r y yank that rectangle back.

C-x r o or open-rectangle is a quick way to indent a block of text to where you want it, especially if you can’t remember the other commands.

And lastly C-x r c will clear a rectangular space. I can’t remember ever using that one, to be honest…

If you find pressing C-x r a keystroke too far, read this post on remapping C-x r  to C-`

Summary of Commands

C-x r k    Kill rectangle
C-x r y     yank last killed rectangle
C-x r c     Clear rectangle
C-x r o     open rectangle and shift text right
C-x r t spoons  fill rectangle with spoons
C-x r r q   Copy rectangle to register q
C-x r i q   Insert rectangle from register q
Emacs Emacs Workout

Dired Trick #1

Many people say that once you get the hang of Dired, you never use anything else. How true is this?

From a personal perspective I’d say that was true, but the key is in the first part of the sentence once you’ve got used to using Dired…

Here’s real world use for Dired.

C-x d to start Dired and navigate to a directory.

Here’s a directory into which a CD has been ripped. The track names haven’t been recognised.

/home/*****/Music/Ludwig van Beethoven/Beethoven- Symphonies Nos. 1 & 2:
total used in directory 108088 available 32870008
drwxrwxrwx 2 ***** ***** 4096 Jun 8 2009 .
drwxrwxrwx 4 ***** ***** 4096 Jan 10 2009 ..
-rwxr--r-- 1 ***** ***** 19510080 Sep 21 2010 01 Track 1.mp3
-rwxr--r-- 1 ***** ***** 12268125 Sep 21 2010 02 Track 2.mp3
-rwxr--r-- 1 ***** ***** 7567075 Sep 21 2010 03 Track 3.mp3
-rwxr--r-- 1 ***** ***** 11410580 Sep 21 2010 04 Track 4.mp3
-rwxr--r-- 1 ***** ***** 20195615 Sep 21 2010 05 Track 5.mp3
-rwxr--r-- 1 ***** ***** 19836565 Sep 21 2010 06 Track 6.mp3
-rwxr--r-- 1 ***** ***** 7501945 Sep 21 2010 07 Track 7.mp3
-rwxr--r-- 1 ***** ***** 12329080 Sep 21 2010 08 Track 8.mp3

I could rename each track using a GUI by right clicking on each one and changing the name. Dired mode is faster

C-x C-q to toggle read only. Now simply type the new names of the tracks directly into the buffer. C-c C-c when you’ve finished.

If you want to be more efficient, move the point to the T in Track 1 and press M-z 1 to zap-to-char 1, deleting everything up to including the 1, leaving the .mp3 intact. Now type in the new track name.

If you want to be really efficient, of course, you could use search and replace…

Related Posts

Dired Tricks #2

Emacs Emacs Workout

Emacs Snooker

The key to snooker isn’t where the coloured balls go, it’s where your white ball stops. You should always be thinking about setting up your next shot. Something similar holds when using Emacs. Where will the point (cursor) be when you’ve completed your current operation?

Consider the following commands:
M-u Convert to uppercase to the end of the word
M-l Convert to lowercase to the end of the word
M-c Capitalize word

Note that the point jumps to the end of the word when each command is called. This makes it easy to jump through a string of words, changing case or capitalizing as you wish.

Now consider the following

M– M-u Convert previous word to uppercase
M– M-l Convert previous word to lowercase
M– M-c Capitalize previous word
M– means (Alt) and –

Note that the point remains where it is, at the end of the word you’ve just converted. You can’t jump back through a series of words converting as you go.

It’s like the difference between hitting C-o and hitting Enter. At first glance they both appear to do the same thing, but look at the point…