Configuring Emacs

Emacs is a powerful, extensible text editor which is available for just about any operating system. It can be run in console mode which makes it useful for editing scripts and config files through a terminal emulator.

However, configuring Emacs to behave in the way you want can be a daunting and time-consuming task. Here, we’ll look at setting up Emacs for development work.

The config file

To make persistent changes to the Emacs configuration, we need to first ensure that there is a config file in place.

On linux, the config file is located here:

~/.emacs.d/init.el

If this file doesn’t exist, we need to first create one. It can just be a blank file for now.

Indentation

By default, the indentation style used for C++ is ‘gnu’.

If you are familiar with Visual Studio, you might want to change the indentation style to ‘bsd’ (sometimes referred to as Allman style). To do this, add the following line to the config file:

(setq c-default-style "bsd")

You can also set the indentation size to, say, 4 with the following:

(setq c-basic-offset 4)

Tabs

By default, Emacs will display a tab as 8 characters. If you are entering code in Emacs, it will automatically replace 8 indentation spaces with a single tab. This means that your file is likely to be a bit of a hotchpotch of spaces and tabs and is unlikely to look consistent in other text editors.

The easiest solution is to use spaces in place of tabs. This is achieved with the following setting:

(setq-default indent-tabs-mode nil)

If you prefer tabs instead of spaces and want to use a tab size of 4, one way to achieve this is by entering the following two lines in the config file:

(setq tab-width 4)
(setq-default tab-width 4)

Note that we need to specify these settings in addition to the c-basic-offset parameter described above.

Syntax highlighting

By default, Emacs will apply syntax highlighting to C++ code. If you are using Emacs in console mode (particularly if you are using a terminal emulator) the colours might be hard to see.

To simply disable colour highlighting, use the following:

(global-font-lock-mode 0)

Alternatively, you can specify your own colours. For example, to change the colour used for comments to green, you can add the following to the config file:

(set-face-attribute 'font-lock-comment-face 
    nil :foreground "#37a639)

To find the name of the ‘face’ you wish to change, open some syntax-highlighted code in Emacs and move the cursor over the appropriate section of the code. Then enter the following:

M-x describe-face

Note that colours might not display as you expect over a terminal emulator such as putty.

Now that Emacs is configured, let’s look at some useful Emacs modes for developers.

Find in files

Suppose we want to search all source code files (e.g. *.cpp and *.h files) recursively to find instances of the word ‘banana’.

This can be done with the following command:

M-x rgrep

You’ll first be prompted for the search term. Enter the search term in lower case i.e. ‘banana’. (Note that if your search term is all lowercase, the search will be case insensitive. If the search term contains a mixture of upper and lowercase, the search will be case sensitive.)

You’ll then be prompted for the file types to look in:

Search for "banana" in files: 

Here you can specify the file types. At the prompt you will also see the default file types that Emacs will look in. The default will depend on the type of file you were viewing when you launched rgrep. If you launched rgrep from a cpp file, the default file types should be appropriate. If you are happy with the default file types, just hit enter.

To specify file types explicitly, use a space-separated list i.e. *.cpp *.h. Note that in order to enter a space, you actually need to type C-q then hit space.

Emacs will remember the file types the next time you perform an rgrep so the second time you perform a search you can just hit enter.

Finally, you need to specify the root folder for the search. By default, this will be the folder of the file currently being edited.

In the grep buffer, you can tab through the list of search results. Pressing enter will bring up the containing file in the other buffer and take you to the appropriate line.

Compiling with make

When compiling code, you can run make in an interactive Emacs mode. This makes it easy to navigate to errors and warnings shown in the compiler output.

To enter ‘compile’ mode, type:

M-x compile

and hit enter to run make. Just as with grep mode, you can tab through the results and hit enter to take you directly to the appropriate line of code.

To rerun the compilation from within ‘compile’ mode, just press ‘g’.