I want emacs to automatically reset it's indentation level to 0 at one specific point in a file.

For example: Because I have nested-nested-nested-.. namespaces, in the C++-Files I would like to not to indent the whole body inside the namespaces.

#include <vector>    
namespace qw {
namespace impl {

using std::vector;

class index_impl {
    vector<string> entries;
    void add(const string &normalized, const string& original);
    string getBestMatch(const string& normalized) const;

} // namespace impl_multimap
} // namespace qw

This is only useful, of course, in these cases when there mainly one class to implement inside the nested namespaces. In header files this would be confusing. So I guess the best solution would be to have a special comment or something.

#include <vector>    
namespace qw {
namespace impl {
// -*- indent: 0 -*-

using std::vector;

Any other ideas would be ok, too.


Original Source.

Please put this line into your emacs file:

(c-set-offset 'substatement-open 0)

Detail method:

  1. Go to the line you want to indent

  2. Please type C-C C-O (not zero) and press Enter

  3. Now Type "0" and, press Enter for no extra indentation

  4. Please type Tab to reindent the line

  5. Future "{" will have the correct tab setting, until you restart emacs.

Additionally you can put this in your emacs file:


to see the lisp code.

Learn to format C++ source outside of emacs here.

  • This will switch the indentation to 0, always, won't it? That's not what I need. I am more thinking of a manual tweaking in rare cases. astyle could help, yes, but I want to stay inside emacs. – towi Aug 21 '16 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.