sourav.d
sourav.d

Reputation: 120

Emacs indentation for multi-level nesting of C code

I'm completely new to emacs (mostly used vim and eclipse/netbeans etc.) I was playing with multi-level nesting of C code and wrote a sample code in emacs to test how it indents codes where nesting is way too deep (not real life code though).

int foo()
{
    if (something) {
        if (anotherthing) {
            if (something_else) {
                if (oh_yes) {
                    if (ah_now_i_got_it) {
                        printf("Yes!!!\n");
                    }
                }
            }
        }
    }
}

This looked exactly like this as I typed in emacs and saved it. But opening it on a different text editor showed the actual text saved is this:

int foo()
{
    if (something) {
    if (anotherthing) {
        if (something_else) {
        if (oh_yes) {
            if (ah_now_i_got_it) {
            printf("Yes!!!\n");
            }
        }
        }
    }
    }
}

So I was wondering is there any way in emacs to save the text the way it actually displays?

My current c-default-style is set to "linux".

EDIT:

Ok, I was using Notepad++/Vim to view the file saved by emacs and it showed that "wrong" indentation, but looks like, opening with good old notepad (or even doing a cat file.c) shows the correct indentation as displayed by emacs. Will try the other approaches mentioned here. Thanks!

Upvotes: 1

Views: 161

Answers (1)

Chris Barrett
Chris Barrett

Reputation: 3385

Try using spaces instead of tabs for indentation. Add the following to your init.el:

(setq-default indent-tabs-mode nil)

This will make all buffers use spaces by default. You will want to add the following exception for makefiles:

(add-hook 'makefile-mode-hook (lambda () (setq indent-tabs-mode t)))

Upvotes: 3

Related Questions