Vue CodeMirror 6 Component

6.1.1 · active · verified Sun Apr 19

vue-codemirror is a Vue 3 component that provides a robust wrapper for CodeMirror 6, the modern code editor library. The current stable version is 6.1.1. This library is actively maintained, with frequent minor releases and bug fixes, indicated by its release history. A key differentiator is its strict adherence to Vue 3 and CodeMirror 6, making it completely incompatible with previous versions of CodeMirror (v5 and below) or Vue (v2). It leverages CodeMirror 6's native ES Module structure, meaning it no longer supports UMD modules for direct browser references, requiring a module bundler. Users must explicitly install additional CodeMirror language, theme, and state packages as needed, as they are not bundled.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the basic setup of the CodeMirror 6 editor component in a Vue 3 application, including v-model binding, applying language extensions and themes, and handling editor events like 'ready', 'change', 'focus', and 'blur'.

import { defineComponent, ref, shallowRef } from 'vue'
import { Codemirror } from 'vue-codemirror'
import { javascript } from '@codemirror/lang-javascript'
import { oneDark } from '@codemirror/theme-one-dark'

export default defineComponent({
  components: {
    Codemirror
  },
  setup() {
    const code = ref(`console.log('Hello, vue-codemirror!');\nfunction sum(a, b) { return a + b; }`);
    const extensions = [javascript(), oneDark];

    const view = shallowRef();
    const handleReady = (payload) => {
      view.value = payload.view;
      console.log('CodeMirror is ready:', payload);
    };

    const log = (event, value) => {
      console.log(`${event} event:`, value);
    };

    return {
      code,
      extensions,
      handleReady,
      log
    };
  },
  template: `
    <div style="height: 400px; border: 1px solid #ccc;">
      <codemirror
        v-model="code"
        placeholder="Code goes here..."
        :style="{ height: '100%' }"
        :autofocus="true"
        :indent-with-tab="true"
        :tab-size="2"
        :extensions="extensions"
        @ready="handleReady"
        @change="log('change', $event)"
        @focus="log('focus', $event)"
        @blur="log('blur', $event)"
      />
    </div>
  `
})

view raw JSON →