CodeMirror Formatting Addon
raw JSON → 1.0.0 verified Sat Apr 25 auth: no javascript
A standalone, modular packaging of the original CodeMirror formatting addon (v1.0.0), designed to simplify inclusion in module-based projects. Originally extracted from the official CodeMirror v2 demo page, this package provides basic code formatting capabilities (auto-indent and comment toggling) for CodeMirror editors. The release cadence is low; the package has been stable since 2016. Unlike the built-in formatting in CodeMirror v5+, this addon is compatible with older CodeMirror versions (2.x) and does not require a full migration. It ships as a single JavaScript file and is intended for use with browser module loaders or bundlers.
Common errors
error Error: Cannot find module 'codemirror-formatting' ↓
cause Package not installed or not resolved by bundler.
fix
Run npm install codemirror-formatting or add it to package.json.
error Uncaught TypeError: Cannot read properties of undefined (reading 'commands') ↓
cause CodeMirror is not loaded before the formatting addon is imported.
fix
Ensure CodeMirror is imported and available globally (e.g., import 'codemirror' before 'codemirror-formatting').
error 'indentMore' is not a known command ↓
cause The formatting addon has not been applied properly, or using CodeMirror v5+ which has its own commands.
fix
Verify that the addon is imported and that you are using a compatible CodeMirror version (2.x–4.x).
Warnings
breaking This addon only works with CodeMirror versions 2.x~4.x. It does not support CodeMirror v5+ (which has built-in formatting). ↓
fix Upgrade to CodeMirror v5+ and use its built-in formatting commands (e.g., indentMore, indentLess).
gotcha The addon modifies CodeMirror.commands globally. If using multiple editor instances, all will be affected. ↓
fix Ensure this import is only done once in your application.
deprecated This package has not been updated since 2016 and may have compatibility issues with modern bundlers. ↓
fix Consider using the official CodeMirror formatting addon from @codemirror/format or codemirror/addon/comment/comment.js.
gotcha The 'autoIndent' command may not work as expected if the CodeMirror mode does not support indentation. ↓
fix Test with your specific mode or use a mode that defines indentUnit.
Install
npm install codemirror-formatting yarn add codemirror-formatting pnpm add codemirror-formatting Imports
- default (formatting module) wrong
import formatting from 'codemirror-formatting'correctimport 'codemirror-formatting' - CodeMirrorCommands (if using named import) wrong
import { indentMore } from 'codemirror-formatting'correctimport 'codemirror-formatting'; // then use CodeMirror.commands.indentMore, etc. - CommonJS require
require('codemirror-formatting') - TypeScript types wrong
import type {} from 'codemirror-formatting'correct/// <reference types="codemirror-formatting" />
Quickstart
// Ensure CodeMirror is loaded first. Then import the formatting addon.
// It automatically extends CodeMirror.commands.
import CodeMirror from 'codemirror';
import 'codemirror/lib/codemirror.css';
import 'codemirror/mode/javascript/javascript';
import 'codemirror-formatting';
const editor = CodeMirror(document.getElementById('editor'), {
mode: 'javascript',
lineNumbers: true
});
// Use the formatting commands:
editor.execCommand('indentMore'); // Indent selection
editor.execCommand('indentLess'); // Unindent selection
editor.execCommand('autoIndent'); // Auto-indent selection or line
editor.execCommand('toggleComment'); // Toggle line comment