Skip to content

Tab Handling

By default, the Tab key moves focus to the next UI element (standard Compose behavior). You can override this to indent code instead.

Tab extension

The demo defines a function that returns either an indent-with-tab extension or a literal-tab-insert extension:

    fun tabExtension(m: TabMode): Extension = when (m) {
        TabMode.INDENT -> keymapOf(indentWithTab)
        TabMode.INSERT -> keymapOf(
            KeyBinding(
                key = "Tab",
                run = { session ->
                    session.insertAt(session.state.selection.main.head, "\t")
                    true
                }
            )
        )
    }
  • TabMode.INDENT uses indentWithTab from the :commands module, which maps Tab to indentMore and Shift-Tab to indentLess.
  • TabMode.INSERT inserts a literal tab character at the cursor.

Accessibility note

Capturing Tab prevents keyboard-only users from tabbing out of the editor. Consider providing an alternative escape mechanism (e.g., Escape to release focus) if your editor is part of a larger form.


Based on the CodeMirror Tab Handling example.