Emacs Word Commands in C and Java Modes


Emacs word-related commands are modified in C and Java modes to treat identifiers as "words".

The Problem

You Use the Emacs Commands:

Command Action
M-f Move forward by one word
M-b Move backward by one word
M-d Delete word ahead of the cursor
M-DEL Delete word behind cursor
M-t Transpose words
M-@ Mark word
M-u Upcase word
M-l Downcase word
M-c Capitalize word

They work beautifully with ordinary text but not so great in C or Java modes. For example, the ARROWS in the image below show where the cursor will skip to if you use M-f to skip forward starting at the first arrow.


The problem here is that C-mode doesn't recognize "_" as part of a "word".

The solution

There is an easy fix for this: simply add the following lines to your .emacs file:

[gist 6207651]

The image below shows where the cursor will skip to after the hook is installed.


All word-related will recognize "_" as part of a "word" in C and Java modes.

Problem solved.

Using Emacs for Fun and Profit: align-regexp


Column alignment of text in Emacs is discussed.


You're a neat freak about your code. Unaligned '=' signs bother you. For example

RGBA zx_plane_color = {RED,    0.1};
RGBA zy_plane_color = {GREEN,  0.1};
RGBA xy_plane_color = {BLUE,   0.1};
RGBA plane_grid_color = {GREY,   0.5};
RGBA background_color = {WHITE,  1};


The Emacs align-regexp command can handle situations like this and more. Aligning the above code is easy

Keystrokes Emacs Action
M-x align-regexp Emacs responds with Align regexp:
= Declarations are aligned.

The result is

RGBA zx_plane_color   = {RED,    0.1};
RGBA zy_plane_color   = {GREEN,  0.1};
RGBA xy_plane_color   = {BLUE,   0.1};
RGBA plane_grid_color = {GREY,   0.5};
RGBA background_color = {WHITE,  1};

Unfortunately, the default is to align only on the first occurence of the regexp. In many cases, you want to format tabular data such as

12, 34, 23123,     3939
2343, 344,
343, 33, 232,          2323,  zzzz

To align multiple occurences, enter the following commands

Keystrokes Emacs Action
C-u M-x align-regexp Emacs responds with Complex align using regexp: \(\s-*\).
Erase the \(\s-*\) and replace with \(\s-+\). Emacs responds with Parenthesis group to modify (justify if negative) : 1
Press ENTER Emacs responds with Amount of spacing (or column if negative) : 1
Press ENTER Emacs responds with Repeat throughout line? (y or n)
Type Y then ENTER Text is aligned on commas.

The result is

12,   34,  23123, 3939
2343, 344,
343,  33,  232,   2323, zzzz

Notice that the number of items in each row can differ.

align-regexp can handle much more complex situations than the ones covered here but I believe that these describe the two most common cases in general.

Using Emacs for Fun and Profit: Making the Modeline Useful


The Emacs mode line is somewhat cryptic. A brief introduction to mode line customization in Emacs is given.

The Default Modeline

The Emacs mode line is an area below the contents of a buffer displaying information such as current line number of the cursor, buffer name, and major/minor modes. Here is the default modeline (in Emacs 24.2.1)


Some of it makes sense. The line and column numbers are shown, as is the buffer name and current modes. Not so clear though are the mysterious "U:---" and "All". What do they mean?

If I open a file and page down several times, the word "All" changes to "39%" which is the distance that I have moved down into the file.


What about that mysterious -:-- which was U:**- in the scratch buffer's modeline? The Emacs documentation tells us that the left part of the modeline has the form



  • CS is the coding system (possibly prefixed with a character designating the current input method).
  • The character(s) immediately after CS indicate the end-of-line convention. This can be \ or (DOS) if the DOS/WINDOWS carriage return + linefeed convention is used to indicate end-of-line.

  • CH is -- if the buffer is unmodified,

    • ** if the buffer has been edited,
    • %% if the buffer is read-only and unmodified,
    • %* if the buffer is read-only and modified
  • The character following CH is a dash (-) unless the default directory for the buffer is on a remote machine, in which case it is a @.
  • FR is the frame name and is seen only on text terminals.

If you're like me, then you probably won't remember this in your day-to-day use of Emacs. Fortunately the modeline can be customized like anything else in Emacs. I'll explain some of the customizations that I'm using for my mode line shown below (the vertical text explains each part of the modeline)


After reading this you should know enough to customize your Emacs modeline to your own preferences.

Here is the code to generate this modeline (note: there is an additional function defined below named last-dir that you'll need as well

[gist 5842596]

The Emacs lisp variable mode-line-format on the first line determines what gets placed onto the modeline.

The predefined variable, mode-line-buffer-identification, will show the current buffer name in bold text. Clicking on the buffer identification with mouse-1 will cause Emacs to switch to the next buffer in its buffer list and mouse-2 the previous buffer.

Quoted strings will be displayed verbatim.

The current line and column are displayed by


Percent characters followed by a single-letter code in literal strings are a signal for Emacs to substitute a whole range of useful information. Some examples:

  • %I - Abbreviated size of buffer (e.g. 32k, 2M, etc.)
  • %p - percentage of buffer text above the top of the window.
  • %P - percentage of buffer text below the bottom of the window.

Others can be found here

In our case, we are using %l and %c to specify line and column. Additionally, you can designate a field width in C-style by placing a number after the '%'.

Emacs also allows arbitrary pieces of lisp code to be evaluated when constructing the mode line. Here the real power of mode line customization can be seen. A simple example can be seen with the call to last-dir which takes the default directory path and peels off all but the last directory.

[gist 5842984]

Finally the variable mode-line-modes is used to display the major and minor modes which are active for the current buffer. Clicking on the major mode name with mouse-1 will typically display a menu of available commands for that mode. Clicking on the minor mode name with mouse-1 will display a pop-up menu allowing you to turn off the minor mode or display help. Clicking on the major or minor mode name with mouse-2 will display a menu of minor modes to choose from.

GitHub – toddkfisher

Todd Fisher

San Mateo, CA

Civil engineer. Ex programmer.