2

I often work on several different codebases that use different indent styles (2 spaces vs 4 spacces vs tabs...). It is annoying to set c-basic-offset (or other variables, depending on mode) each time I open new file. Is there a package (or setting) that makes Emacs detect indentation style of opened file?

1

Check out dtrt-indent on melpa.

4

CC-mode comes with the cc-guess.el file which provides functions to do that.

E.g. M-x c-guess should do the trick.

  • Works well, but guessing takes several minute for bigger files. – Michał Zieliński Sep 9 '16 at 19:37
  • 1
    I think the docstring you get from C-h f c-guess RET should give you some hints about how to control its run-time, eg.g. by restricting the part of the file it scans. – Stefan Sep 9 '16 at 19:41
2

One way to do that for a specific file is to use file local variables:

A file can specify local variable values to use when editing the file with Emacs. Visiting the file checks for local variable specifications; it automatically makes these variables local to the buffer, and sets them to the values specified in the file.

The doc describes how you can specify them in a comment on the first line of the file, or as a “local variables list” near the end of the file.

For a whole code base, using Per-Directory Local Variables is certainly more appropriate.

Sometimes, you may wish to define the same set of local variables to all the files in a certain directory and its subdirectories, such as the directory tree of a large software project. This can be accomplished with “directory-local variables”.

Again, the linked doc (or info doc from within emacs) describes how to define them.

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.