@emmetio/codemirror6-plugin

中文文档
0.4.2发布于 2026/10/30MITTypeScriptES Module近 30 天下载 15.4万

CodeMirror 6 extension that adds Emmet support to text editor.

原始使用文档
文档目录6 节
  1. How to use
  2. Expanding abbreviations
  3. Abbreviation mode
  4. Notes on document syntax
  5. Available commands
  6. License

Emmet extension for CodeMirror 6 editor

CodeMirror 6 extension that adds Emmet support to text editor.

Extension development is sponsored by CodePen and Replit


How to use

This extension can be installed as a regular npm module:

npm i @emmetio/codemirror6-plugin

The plugin API follows CodeMirror 6 design: it’s an ES6 module and provides a number of exported extensions which you should import and add into your EditorState instance.

In most cases, this package exports Emmet actions as StateCommand, which should be used as follows:

import { EditorState, EditorView, basicSetup } from '@codemirror/basic-setup';
import { html } from '@codemirror/lang-html';
import { keymap } from '@codemirror/view';

// Import Expand Abbreviation command
import { expandAbbreviation } from '@emmetio/codemirror6-plugin';

new EditorView({
    state: EditorState.create({
        extensions: [
            basicSetup,
            html(),

            // Bind Expand Abbreviation command to keyboard shortcut
            keymap.of([{
                key: 'Cmd-e',
                run: expandAbbreviation
            }]),
        ]
    }),
    parent: document.body
});

Expanding abbreviations

Emmet extension can track abbreviations that user enters in some known syntaxes like HTML and CSS. When user enters something that looks like Emmet abbreviation, extension starts abbreviation tracking (adds emmet-abbreviation class to a text fragment). WHen abbreviation becomes complex (expands to more that one element), it displays abbreviation preview:

Emmet abbreviation example

To enable abbreviation tracker, you should import abbreviationTracker function and add its result to editor extensions:

import { EditorState, EditorView, basicSetup } from '@codemirror/basic-setup';
import { html } from '@codemirror/lang-html';
import { abbreviationTracker } from '@emmetio/codemirror6-plugin';

new EditorView({
    state: EditorState.create({
        extensions: [
            basicSetup,
            html(),
            abbreviationTracker()
        ]
    }),
    parent: document.body
});

Abbreviation tracker is context-aware: it detect current syntax context and works only where abbreviation expected. For example, in HTML syntax it works in plain text context only and doesn’t work, for example, in attribute value or tag name.

To expand tracked abbreviation, hit Tab key while caret is inside abbreviation, or hit Esc key to reset tracker.

Abbreviation mode

In case if abbreviation tracking is unavailable or you want to give user an opportunity to enter and expand abbreviation with interactive preview, a special abbreviation mode is available. Run enterAbbreviationMode command to enter this mode: everything user types will be tracked as abbreviation with preview and validation. To expand tracked abbreviation, hit Tab key while caret is inside abbreviation, or hit Esc key to reset tracker.

Notes on document syntax

Currently, CodeMirror API doesn’t provide viable option to get document syntax to allow plugins to understand how to work with document. So you have to specify document syntax manually in Emmet plugin. You can do so via emmetConfig facet or as an option to abbreviationTracker:

import { EditorState, EditorView, basicSetup } from '@codemirror/basic-setup';
import { html } from '@codemirror/lang-html';
import { keymap } from '@codemirror/view';

import { emmetConfig, abbreviationTracker } from '@emmetio/codemirror6-plugin';

const editor1 = new EditorView({
    state: EditorState.create({
        extensions: [
            basicSetup,
            html(),
            // Option 1: specify document syntax as config facet
            emmetConfig.of({
                syntax: 'css'
            }),
            // Option 2: pass syntax as config value of abbreviation tracker
            abbreviationTracker({
                syntax: 'jsx'
            })
            ...
        ]
    }),
    parent: document.body
});

Note that syntax is most important option Emmet: it controls how abbreviation is parsed in document (wether it’s markup, stylesheet or JSX syntax) and style of generated output (HTML or XML style, CSS or SASS dialect and so on).

Some common syntaxes:

  • html, xml: HTML or XML document; in html also tries to detect inline CSS fragments.
  • jsx for JSX syntax. By default, requires abbreviation to start with < in order to skip false-positive abbreviation capturing for variables and functions, also modifies output to match JSX specs (e.g. rename class attribute to className etc.)
  • css, scss, sass, stylus: various options of stylesheet abbreviations and output.
  • haml, jade, pug, slim: supported markup preprocessors.

Default syntax is html.

Available commands

The following commands are available in current package:

License

MIT

版本与发布记录

@emmetio/codemirror6-plugin 共发布 19 个版本,首次发布于 2021/12/23;过去 12 个月发布了 2 个版本。

版本发布日期距今
0.4.2latest2026/10/3014 天前
0.4.12026/06/055 个月前
0.4.02024/09/242 年前
0.3.42024/09/232 年前
0.3.32024/09/182 年前
0.3.22024/08/052 年前

关于 @emmetio/codemirror6-plugin 的常见问题

@emmetio/codemirror6-plugin 是什么?

CodeMirror 6 extension that adds Emmet support to text editor.

如何安装 @emmetio/codemirror6-plugin?

在项目目录运行 npm install @emmetio/codemirror6-plugin,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@codemirror/view(^6.29.1)、@codemirror/state(^6.4.1)、@codemirror/commands(^6.6.0)、@codemirror/lang-css(^6.2.1)、@codemirror/language(^6.10.2)、@codemirror/lang-html(^6.4.9)、@codemirror/autocomplete(^6.17.0)、@codemirror/lang-javascript(^6.2.2)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@emmetio/codemirror6-plugin 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@emmetio/codemirror6-plugin 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@emmetio/codemirror6-plugin 可以商用吗?

@emmetio/codemirror6-plugin 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@emmetio/codemirror6-plugin 的下载量有多少?

根据 npm 官方下载统计,@emmetio/codemirror6-plugin 在 2026/10/14 – 2026/11/12 这 30 天内被下载 153,632 次,日均约 5,121 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@emmetio/codemirror6-plugin 还在维护吗?

最新版本 0.4.2 发布于 2026/10/30(14 天前);至今共发布 19 个版本,其中过去 12 个月发布了 2 个;首次发布于 2021/12/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。