Convert Figma logo to code with AI

codemirror logocodemirror5

In-browser code editor (version 5, legacy)

26,810
4,963
26,810
430

Top Related Projects

A browser based code editor

26,716

Ace (Ajax.org Cloud9 Editor)

25,033

A modern and intuitive terminal-based text editor

8,481

An experimental next-generation Electron-based text editor

43,470

Quill is a modern WYSIWYG editor built for compatibility and extensibility

19,483

Lexical is an extensible text editor framework that provides excellent reliability, accessibility and performance.

Quick Overview

CodeMirror 5 is a versatile, open-source code editor implemented in JavaScript for the browser. It is highly customizable and feature-rich, supporting various programming languages and markup formats. CodeMirror 5 is widely used in web-based development environments and content management systems.

Pros

  • Extensive language support with syntax highlighting and code folding
  • Highly customizable with a wide range of add-ons and themes
  • Excellent performance, even with large documents
  • Well-documented API and active community support

Cons

  • Learning curve for advanced customization and configuration
  • Version 5 is in maintenance mode, with version 6 being the latest major release
  • Some features require additional plugins, increasing complexity
  • May be overkill for simple text editing needs

Code Examples

  1. Basic initialization:
var myCodeMirror = CodeMirror(document.body, {
  value: "function myScript() {\n\treturn 100;\n}",
  mode:  "javascript"
});
  1. Setting options after initialization:
myCodeMirror.setOption("theme", "monokai");
myCodeMirror.setOption("lineNumbers", true);
  1. Listening for changes:
myCodeMirror.on("change", function(cm, change) {
  console.log("The editor content changed!");
});
  1. Getting and setting content:
var content = myCodeMirror.getValue();
myCodeMirror.setValue("New content");

Getting Started

To use CodeMirror 5 in your project, follow these steps:

  1. Include the necessary files in your HTML:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/codemirror.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/mode/javascript/javascript.min.js"></script>
  1. Create a textarea or div element in your HTML:
<textarea id="myTextarea"></textarea>
  1. Initialize CodeMirror in your JavaScript:
var editor = CodeMirror.fromTextArea(document.getElementById("myTextarea"), {
  lineNumbers: true,
  mode: "javascript",
  theme: "default"
});

This will create a basic CodeMirror instance with line numbers, JavaScript mode, and the default theme. You can customize it further by adding more options or calling methods on the editor object.

Competitor Comparisons

A browser based code editor

Pros of Monaco Editor

  • More feature-rich out-of-the-box, including advanced IntelliSense and code completion
  • Better performance for large files and complex syntax highlighting
  • Tighter integration with TypeScript and JavaScript ecosystems

Cons of Monaco Editor

  • Larger bundle size, which may impact load times for web applications
  • Steeper learning curve due to its extensive API and configuration options
  • Less flexibility for customization compared to CodeMirror's modular approach

Code Comparison

Monaco Editor:

monaco.editor.create(document.getElementById('container'), {
    value: 'function hello() {\n\tconsole.log("Hello world!");\n}',
    language: 'javascript'
});

CodeMirror 5:

CodeMirror(document.getElementById('container'), {
    value: 'function hello() {\n\tconsole.log("Hello world!");\n}',
    mode: 'javascript'
});

Both editors offer similar basic setup, but Monaco Editor provides more advanced features and configuration options out-of-the-box. CodeMirror 5 has a simpler API, making it easier to get started with basic functionality. The choice between the two depends on the specific needs of your project, considering factors like performance requirements, desired features, and integration with existing tools and workflows.

26,716

Ace (Ajax.org Cloud9 Editor)

Pros of Ace

  • More extensive language support and syntax highlighting options
  • Better performance for large documents and real-time collaborative editing
  • More customizable and extensible through plugins and modules

Cons of Ace

  • Steeper learning curve for implementation and customization
  • Larger file size and potentially higher resource usage
  • Less frequent updates and maintenance compared to CodeMirror

Code Comparison

Ace:

var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.session.setMode("ace/mode/javascript");
editor.setValue("function foo() {\n    console.log('Hello, world!');\n}");

CodeMirror:

var editor = CodeMirror(document.getElementById("editor"), {
    value: "function foo() {\n    console.log('Hello, world!');\n}",
    mode: "javascript",
    theme: "monokai"
});

Both Ace and CodeMirror are powerful code editors for web applications. Ace offers more advanced features and better performance for complex scenarios, while CodeMirror provides a simpler API and easier integration. The choice between them depends on specific project requirements, such as the need for advanced language support, performance considerations, and ease of implementation.

25,033

A modern and intuitive terminal-based text editor

Pros of micro

  • Lightweight and fast terminal-based text editor
  • Cross-platform support (Linux, macOS, Windows)
  • Extensive plugin system for customization

Cons of micro

  • Limited GUI features compared to web-based editors
  • Smaller community and ecosystem than CodeMirror
  • Less suitable for web-based applications

Code comparison

micro (Lua plugin example):

function onBufferOpen(buf)
    if buf:filename():match("%.md$") then
        buf:setOption("filetype", "markdown")
    end
end

CodeMirror (JavaScript mode configuration):

CodeMirror.defineMode("mymode", function() {
  return {
    token: function(stream) {
      // Mode implementation
    }
  };
});

micro is a terminal-based text editor written in Go, while CodeMirror is a versatile code editor implemented in JavaScript for web browsers. micro excels in terminal environments and offers a lightweight, fast editing experience. CodeMirror, on the other hand, provides a rich set of features for web-based code editing and has a larger community and ecosystem.

The code examples demonstrate the different approaches to extensibility. micro uses Lua for plugins, allowing users to customize behavior easily. CodeMirror uses JavaScript for mode definitions and extensions, which is more suitable for web environments but may require more setup for standalone use.

8,481

An experimental next-generation Electron-based text editor

Pros of xray

  • Built with Rust, offering potential performance benefits
  • Designed for modern, high-performance text editing
  • Focuses on a modular architecture for extensibility

Cons of xray

  • Less mature and less widely adopted than CodeMirror 5
  • Limited documentation and community support
  • Development has been discontinued (archived repository)

Code Comparison

xray (Rust):

pub struct Buffer {
    text: Text,
    version: Version,
    line_ending: LineEnding,
}

CodeMirror 5 (JavaScript):

CodeMirror.defineMode("javascript", function(config, parserConfig) {
  var indentUnit = config.indentUnit;
  var statementIndent = parserConfig.statementIndent;
  var jsonldMode = parserConfig.jsonld;
  // ... (more code)
});

Summary

xray was an experimental text editor core written in Rust, aiming for high performance and modern architecture. However, its development has been discontinued. CodeMirror 5, on the other hand, is a well-established, widely-used JavaScript library for in-browser code editing. While xray had potential performance advantages due to its Rust implementation, CodeMirror 5 offers greater stability, extensive documentation, and a large community of users and contributors. The code comparison shows the different approaches: xray's low-level Rust structures versus CodeMirror 5's JavaScript-based mode definitions.

43,470

Quill is a modern WYSIWYG editor built for compatibility and extensibility

Pros of Quill

  • More modern architecture with better support for custom formats and modules
  • Easier to customize and extend with a modular design
  • Better handling of rich text content and formatting

Cons of Quill

  • Smaller community and ecosystem compared to CodeMirror
  • Less suitable for code editing and syntax highlighting
  • May have a steeper learning curve for complex customizations

Code Comparison

Quill:

var quill = new Quill('#editor', {
  modules: { toolbar: true },
  theme: 'snow'
});

CodeMirror:

var editor = CodeMirror.fromTextArea(document.getElementById("editor"), {
  lineNumbers: true,
  mode: "javascript"
});

Both libraries offer easy initialization, but Quill's API is more concise and focused on rich text editing, while CodeMirror's API is geared towards code editing with features like line numbers and syntax highlighting.

Quill is better suited for rich text editing and content creation, offering a more modern and customizable approach. CodeMirror, on the other hand, excels in code editing scenarios with its extensive language support and syntax highlighting capabilities. The choice between the two depends on the specific requirements of your project and the type of editing experience you want to provide to your users.

19,483

Lexical is an extensible text editor framework that provides excellent reliability, accessibility and performance.

Pros of Lexical

  • Modern architecture with a focus on extensibility and customization
  • Built-in accessibility features and support for collaborative editing
  • Optimized for performance with a lightweight core

Cons of Lexical

  • Relatively new project with a smaller ecosystem compared to CodeMirror
  • Steeper learning curve due to its unique architecture
  • Limited language support out of the box

Code Comparison

Lexical:

import {$getRoot, $createParagraphNode, $createTextNode} from 'lexical';

editor.update(() => {
  const root = $getRoot();
  const paragraph = $createParagraphNode();
  const text = $createTextNode('Hello world');
  paragraph.append(text);
  root.append(paragraph);
});

CodeMirror:

var editor = CodeMirror.fromTextArea(document.getElementById("editor"), {
  lineNumbers: true,
  mode: "javascript"
});

editor.setValue("Hello world");

Summary

Lexical offers a modern, extensible architecture with built-in accessibility and collaboration features, while CodeMirror has a more established ecosystem and simpler API. Lexical's unique approach may require more learning but provides greater flexibility, whereas CodeMirror offers easier integration and wider language support out of the box.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

CodeMirror 5

NOTE: CodeMirror 6 exists, and is more mobile-friendly, more accessible, better designed, and much more actively maintained.

Build Status

CodeMirror is a versatile text editor implemented in JavaScript for the browser. It is specialized for editing code, and comes with over 100 language modes and various addons that implement more advanced editing functionality. Every language comes with fully-featured code and syntax highlighting to help with reading and editing complex code.

A rich programming API and a CSS theming system are available for customizing CodeMirror to fit your application, and extending it with new functionality.

You can find more information (and the manual) on the project page. For questions and discussion, use the discussion forum.

See CONTRIBUTING.md for contributing guidelines.

The CodeMirror community aims to be welcoming to everybody. We use the Contributor Covenant (1.1) as our code of conduct.

Installation

Either get the zip file with the latest version, or make sure you have Node installed and run:

npm install codemirror@5

NOTE: This is the source repository for the library, and not the distribution channel. Cloning it is not the recommended way to install the library, and will in fact not work unless you also run the build step.

Quickstart

To build the project, make sure you have Node.js installed (at least version 6) and then npm install. To run, just open index.html in your browser (you don't need to run a webserver). Run the tests with npm test.

NPM DownloadsLast 30 Days