Take the 2-minute tour ×

I am using a common Codemirror editor for several languages (Groovy, Python, Beanshell, XSLT). Depending on the language selected in a drop down, I set the respective mode for the editor.

How do I apply auto-indentation for the languages above?

share|improve this question

1 Answer 1

CodeMirror modes can provide indentation for their respective languages, and most does (e.g. python mode). This can be verified by inspecting the mode's source to check if indent(state, textAfter) has been implemented. From the docs:


After applying a mode that defines indent(state, textAfter), and assuming smartIndent is set to true (which is the default value), auto indentation should be provided transparently, no extra effort on your side.

If a mode does not support smart indentation out of the box, you can always implement it yourself. See the section on writing CodeMirror modes in the docs.

share|improve this answer
I saw that python.js has a method called indent(stream, state, type). I am not sure what these parameters called stream and state are. Could you please tell me? –  Parag A Apr 12 '13 at 17:21
please read my answer again. –  Eliran Malka Apr 12 '13 at 19:47
I read it twice but not able to get the soln. All I understood is python.js contains code for indentation. But not sure how to use it. –  Parag A Apr 12 '13 at 22:41
You use it simply by using the editor - as you write code into the editor, each new line will be indented following the rules dictated by the mode. Please read the Codemirror docs on writing modes, as suggested, it will shed more light on things. Are you looking for post factum code formatting capabilities as well? –  Eliran Malka Apr 13 '13 at 1:56
I am. Specifically, a given CodeMirror mode doesn't always use the same number of spaces for indention as existing code. Even if it were possible to tweak the mode, it would need to be done for every mode. And while editing, I haven't found an easy (e.g. single key) way to automatically reindent the current line to however CodeMirror wants to do it. For instance, in emacs I can hit "tab" on a line and it will indent properly, in CodeMirror it just adds a level of indention, which is not what I want. –  Michael Apr 3 at 20:14

Your Answer


