Help:Extension:CodeMirror

PD Note: When you edit this page, you agree to release your contribution under the CC0. See Public Domain Help Pages for more info. PD

The CodeMirror extension is an editor that provides syntax highlighting. It supports a variety of languages, notably MediaWiki wikitext, JavaScript, CSS, JSON and Lua. This can make editing easier by using colors and shading to visually separate plain text from other syntax such as links, templates, and other wikitext markup.

Usage

Enabling

CodeMirror can be enabled by enabling the Beta feature Improved Syntax Highlighting, then enabling Enable syntax highlighting in the Editing section of your preferences. In WikiEditor (also known as the 2010 editor, enabled with the Enable the editing toolbar preference), it can also be toggled on and off using the highlighter button " Syntax" in the toolbar. In the 2017 editor, you can toggle CodeMirror by clicking the Page options button in the toolbar, then selecting Syntax highlighting.

Keyboard shortcuts

Some keyboard shortcuts do not apply to the 2017 editor, which has its own set of keyboard shortcuts.

You can review keyboard shortcuts by opening the help dialog with Ctrl+⇧ Shift+/.

List of keyboard shortcuts
Shortcut Shortcut (macOS) Description
Navigation
Ctrl+ Alt+ Move the cursor one word to the left.
Ctrl+ Alt+ Move the cursor one word to the right.
Alt+ Alt+ Move the selected lines up one line.
Alt+ Alt+ Move the selected lines down one line.
⇧ Shift+Alt+ ⇧ Shift+Alt+ Duplicate the selected lines and keep the selection on the top copy.
⇧ Shift+Alt+ ⇧ Shift+Alt+ Duplicate the selected lines and keep the selection on the bottom copy.
Escape Escape Reduce multiple selections to the main range, or to a cursor if there is only one selection.
Text styling
Ctrl+b ⌘ Command+b Style the selected text in bold.
Ctrl+i ⌘ Command+i Style the selected text in italics.
Ctrl+k ⌘ Command+k Insert a link.
Ctrl+⇧ Shift+6 ⌘ Command+⇧ Shift+6 Style the selected text as computer code.
Ctrl+⇧ Shift+5 ⌘ Command+⇧ Shift+5 Style the selected text with a strikethrough.
Ctrl+, ⌘ Command+, Style the selected text as subscript
Ctrl+. ⌘ Command+. Style the selected text as superscript
Ctrl+u ⌘ Command+u Style the selected text as underlined
Paragraph formatting
Ctrl+[ ⌘ Command+[ Decrease indentation for the selected lines.
Ctrl+] ⌘ Command+] Increase indentation for the selected lines.
Ctrl+7 Ctrl+7 Make selected text preformatted.
Ctrl+8 ⌘ Command+8 Format as a blockquote.
Ctrl+1-6 ⌘ Command+1-6 Add a heading of the given level (1 through 6).
Searching
Ctrl+f ⌘ Command+f Open the search panel.
Ctrl+g ⌘ Command+g Find the next occurrence of the search string.
⇧ Shift+Ctrl+g ⇧ Shift+⌘ Command+g Find the previous occurrence of the search string.
Ctrl+d ⌘ Command+d Select the next occurrence of the current selection or word, creating multiple selections.
Ctrl+Alt+g ⌘ Command+Alt+g Open the goto line panel
Insert
Ctrl+↵ Enter ⌘ Command+↵ Enter Insert a blank line below the current line.
Ctrl+⇧ Shift+k ⌘ Command+⇧ Shift+k Insert a reference.
Ctrl+/ ⌘ Command+/ Insert a comment or make the selected text into a comment.
Code folding
Ctrl+⇧ Shift+[ ⌘ Command+Alt+[ Fold the currently selected code.
Ctrl+⇧ Shift+] ⌘ Command+Alt+] Unfold the selected code.
Ctrl+Alt+[ Ctrl+Alt+[ Fold all code.
Ctrl+Alt+] Ctrl+Alt+] Unfold all code.
Other
Ctrl+⇧ Shift+/ Ctrl+⇧ Shift+x Show the keyboard shortcuts help dialog.
Ctrl+⇧ Shift+, ⌘ Command+⇧ Shift+, Open the CodeMirror preferences panel.
Ctrl+⇧ Shift+x ⌘ Command+⇧ Shift+x Toggle viewing the editor in right-to-left and left-to-right.
Cursor modifiers
Ctrl+click ⌘ Command+click Add an additional cursor or selection to the area clicked.
Ctrl+click on page titles ⌘ Command+click on page titles Open links in the wikitext code.
Alt+click and drag Alt+click and drag Make a rectangular selection.

Features

For technical reasons, some features are unavailable in the 2017 editor.

CodeMirror preferences

MediaWiki version:
1.44

Most features documented here can be disabled or enabled by individual users. To open the preferences panel, press Ctrl+⇧ Shift+, (or ⌘ Command+⇧ Shift+, on macOS). If you use WikiEditor, you can also get to your CodeMirror preferences by clicking on the button in the Advanced section of the toolbar. Your CodeMirror preferences will be remembered across different devices and sessions.

Some preferences are only available in the "full" preferences dialog. This is accessible by clicking on full preferences link in the preferences panel, or directly by pressing Alt+⇧ Shift+,.

Editing MediaWiki Wikitext often has different needs than editing proper "code" such as JavaScript or Lua. For this reason, preferences are stored separately for wikitext and non-wikitext, allowing you to customize the two experiences differently.

Bracket matching

Bracket matching

When the cursor lies directly next to a bracket, the feature highlights the corresponding matching bracket.

Code folding

MediaWiki version:
1.43
Code folding on w:Barack Obama.

Code folding allows you to fold template parameters, extension tags, and other code blocks. This can make managing large, complex templates considerably easier.

Usage

When a cursor or selection covers any part of supported syntax, a tooltip icon will appear just above the text. Clicking it will fold the code, leaving only the template or tag name.

Folded code will have the template or tag name followed by the ellipsis icon . Clicking on it will unfold the code back to its original state.

Keyboard shortcuts
Shortcut Shortcut (macOS) Description
Ctrl+⇧ Shift+[ ⌘ Command+Alt+[ Fold the selected code.
Ctrl+⇧ Shift+] ⌘ Command+Alt+] Unfold the selected code.
Ctrl+Alt+[ Ctrl+Alt+[ Fold all code.
Ctrl+Alt+] Ctrl+Alt+] Unfold all code.

Customization

If you'd like to keep the keyboard shortcut for folding templates, but hide the visual component, you can do so by adding the following to your personal CSS:[1]

.cm-tooltip-fold {
	display: none;
}

Autocompletion

MediaWiki version:
1.44
Example showing autocompletion of parser functions.

This feature provides autocompletion of magic words, parser extension tags, and URL protocols. If you enter {{#, you should see a list of all parser functions that start with #. Similarly, typing double underscores (__) will show a list of double-underscored magic words. You can continue to type to narrow your search. Use the arrow keys to select the desired text, and hit the ↵ Enter or Tab ↹ key to make your selection. Autocompletion does not work for templates, or for magic words or parser functions that do not begin with # or __ by default. To get autocompletion in that case, press ⇧ Shift+↵ Enter after you've typed {{ and the first few characters of the template, magic word or parser function you're looking for. The same shortcut also works to enable autocompletion of wikilinks, including file links.

Linting

MediaWiki version:
1.45
Example showing the linter in wikitext

CodeMirror includes a linter. This performs static code analysis in real time, surfacing errors or other potential problems with your code without you having to save. This works for all languages supported by CodeMirror, including wikitext.

Linting is available as the Lint the code option in your CodeMirror preferences. When enabled, you will see a status bar at the bottom of the editor listing the number of errors, warnings, and informational diagnostics. Issues are underlined in the source text. Hovering over them or moving the cursor to the issues will reveal more information, and in some cases the linter may offer a button to quickly fix the issue. On the opposite side of the status bar, you are shown the numbers for the current line number, character, and selection length.

See Help:Extension:CodeMirror/Wikitext linting for the list of the available lint rules for wikitext.

Note that while the linter is a useful tool to assist with fixing various issues, it is not an authority and may have stylistic opinions not shared by all editors.

MediaWiki version:
1.44

You can quickly open links specified in the wikitext. Do this by holding down the Ctrl key (or ⌘ Command on macOS) and clicking on a page title inside of link syntax, such as [[Main Page]]. Clicking this case would open Main Page in a new tab.

This feature also works for template names and file names.

Line numbering

Note how the line numbering knew to account for the lines contained in the collapsed templates.

When enabled, line numbers are automatically displayed in all editors. On the Wikimedia wikis, it is only enabled for specific namespaces of specific wikis.[2]

Line numbers will take into account any hidden lines, such as those through code folding.

Highlight the active line

MediaWiki version:
1.44

In your CodeMirror preferences, you can select Highlight the active line to enable this feature, which can make it easier to tell which line of text the cursor is on, especially on large pages.

Line wrapping

MediaWiki version:
1.44

Line wrapping is the default behaviour in most editors. Sometimes you may want to disable this, especially when writing complex wikitext or other code. You can disable line wrapping in your CodeMirror preferences.

Multiple cursors and selections

You can add additional cursors or selections by holding the Ctrl key (or ⌘ Command on macOS) and clicking on the desired content or cursor placement. After doing so, any changes you make will be applied to all the selections at the same time.

You can also press Ctrl+d (or ⌘ Command+d on macOS) to select the next occurrence of the current selection or word, which create multiple selections. This can be useful as a quick find and replace feature.

Highlight references

The "Highlight the contents of <ref> tags" preference will make all contents within <ref>...</ref> tags be highlighted with a pale green background color. This helps differentiate reference content from prose.

In addition, the "Fold all <ref> tags by default" preference will make all <ref>...</ref> tags folded when first starting an editing session. Note this feature can be potentially slow, as it needs to scan the entire document. The slowness will only occur once when initially opening the editor.

Highlight special characters

By default, CodeMirror will highlight non-printable control characters and other characters such as non-breaking spaces. Control characters appear as a red dot, while non-breaking spaces are displayed as a faint gray dot. Hovering over these characters will show a tooltip stating which character it is.

If desired, you can disable this feature in your CodeMirror preferences.

Highlight whitespace

Enabling "Highlight whitespace" will highlight the whitespace characters with a gray dot, or a wide arrow for the tab character.

The "Highlight trailing whitespace" setting will highlight spaces, tabs and other invisible characters at the end of a sentence.

Colorblind mode

For users who have difficulty distinguishing between colors, CodeMirror offers a colorblind-friendly mode. This feature can be activated on wikis with CodeMirror enabled with Enable colorblind-friendly scheme for syntax highlighting when editing wikitext in your preferences.

With the colorblind-friendly scheme active, the colors of the following page elements change:

Headings, symbols, signatures, section names, magic words#E4A400
Templates#9C3A00
HTML tags, references, mathematical expressions#56B4E9
Variables#009E73

In addition, background highlights and colors are removed from all text and objects. Bolding and underlining behavior remains unaffected.

Support for the 2017 wikitext editor

Due to various technical conflicts, CodeMirror support in the 2017 wikitext editor is limited.

Features such as code folding that change the appearance of the wikitext cannot feasibly be supported in the 2017 editor, because the 2017 editor needs focus on its editor for users to be able to use its toolbar and other contextual tools. Similarly, CodeMirror needs focus on its editor for its features to work.

To get around this, the CodeMirror editor is overlaid on top of the 2017 editor, with both editors running in parallel. For this reason the two editors need to visually match each other exactly, so only features that involve basic color changes such as the highlighting of references can be supported.

The 2017 editor also requires the viewport to be the full length of the document. This can cause significant performance issues, since CodeMirror has to highlight the entire document instead of only what is currently visible to the user. As a temporary solution, CodeMirror is automatically disabled on very large pages in order to prevent browser crashes and other performance problems that could hinder the editing experience.

Long-term, the hope is to rework the 2017 editor to be more compatible with CodeMirror.

Color and style customization

See Help:Extension:CodeMirror/Color and style customization.

Notes

  1. Your personal CSS is at Special:MyPage/common.css. You can also make your changes take effect globally if GlobalCssJs is installed. On Wikimedia wikis, your global CSS is at meta:Special:MyPage/global.css.
  2. Controlled by the $wgCodeMirrorLineNumberingNamespaces configuration setting. See Extension:CodeMirror for more information.