Skip to content

Emacs’ cc-mode’s built-in styles gallery

May 15, 2009

I, like a lot of programmers, have a preferred style of formatting my source code that makes me happy. So therefore, it was important to me that Emacs understood and indented my code correctly.

Whilst you can manually set each of the individual levels of offsets to apply, it’s usually much simpler to use one of the built-in styles that comes with CC mode. Even if you don’t find one that completely matches your personal style, it is easier to start with one that most closely matches and make slight adjustments, than to start completely from scratch.

What I’d have found useful at the time, would have been an example of what each of the built-in styles looked like. (Sure, it’d not have been too hard to manually set a buffer to each style and re-apply the indentation to use the new style.)

To this end, I present a gallery of examples of the built-in CC mode styles. At the very least, I hope it’ll be a useful reference to people new to Emacs.

To use the style in your current (cc-mode) buffer, use the command C-c . or M-x c-set-style, and then the name of the style. This will not automatically re-format your code. To do so, use the commands C-x h M-C-\ (this puts the region around the entire buffer, and then runs the indent-region command).

To set the style permanently, add the following to your .emacs file:

(add-hook 'c-mode-common-hook '(lambda () (c-set-style "STYLE-NAME")))

GNU
/**
*This is a comment
*/
public class Example
{
private static final int REPEAT_COUNT = 10;

public static void main(String[] args)
{
// Another comment

for (int i = 0; i < REPEAT_COUNT; i++) { if ((i % 2) == 0) { System.out.print("Hello "); } else { System.out.println("World!"); } } switch(REPEAT_COUNT) { case 10: System.out.println("10"); break; default: break; } } }[/sourcecode] K&R
/**
*This is a comment
*/
public class Example
{
private static final int REPEAT_COUNT = 10;

public static void main(String[] args)
{
// Another comment

for (int i = 0; i < REPEAT_COUNT; i++) { if ((i % 2) == 0) { System.out.print("Hello "); } else { System.out.println("World!"); } } switch(REPEAT_COUNT) { case 10: System.out.println("10"); break; default: break; } } }[/sourcecode] BSD
/**
*This is a comment
*/
public class Example
{
private static final int REPEAT_COUNT = 10;

public static void main(String[] args)
{
// Another comment

for (int i = 0; i < REPEAT_COUNT; i++) { if ((i % 2) == 0) { System.out.print("Hello "); } else { System.out.println("World!"); } } switch(REPEAT_COUNT) { case 10: System.out.println("10"); break; default: break; } } }[/sourcecode] Stroustrup
/**
*This is a comment
*/
public class Example
{
private static final int REPEAT_COUNT = 10;

public static void main(String[] args)
{
// Another comment

for (int i = 0; i < REPEAT_COUNT; i++) { if ((i % 2) == 0) { System.out.print("Hello "); } else { System.out.println("World!"); } } switch(REPEAT_COUNT) { case 10: System.out.println("10"); break; default: break; } } }[/sourcecode] Whitesmith
/**
*This is a comment
*/
public class Example
{
private static final int REPEAT_COUNT = 10;

public static void main(String[] args)
{
// Another comment

for (int i = 0; i < REPEAT_COUNT; i++) { if ((i % 2) == 0) { System.out.print("Hello "); } else { System.out.println("World!"); } } switch(REPEAT_COUNT) { case 10: System.out.println("10"); break; default: break; } } }[/sourcecode] Ellemtel
/**
*This is a comment
*/
public class Example
{
private static final int REPEAT_COUNT = 10;

public static void main(String[] args)
{
// Another comment

for (int i = 0; i < REPEAT_COUNT; i++) { if ((i % 2) == 0) { System.out.print("Hello "); } else { System.out.println("World!"); } } switch(REPEAT_COUNT) { case 10: System.out.println("10"); break; default: break; } } }[/sourcecode] Java
/**
*This is a comment
*/
public class Example
{
private static final int REPEAT_COUNT = 10;

public static void main(String[] args)
{
// Another comment

for (int i = 0; i < REPEAT_COUNT; i++) { if ((i % 2) == 0) { System.out.print("Hello "); } else { System.out.println("World!"); } } switch(REPEAT_COUNT) { case 10: System.out.println("10"); break; default: break; } } }[/sourcecode]

Obligatory first post

April 27, 2009

They say start as you mean to go on, so let’s seeā€¦ Ah, I know.

(message "Hello World")