Code Blocks

Syntax highlighting, line highlighting, colored diffs, focus, and line numbers

VitePress uses Shiki for syntax highlighting with powerful code block features.

Syntax Highlighting

Specify language after opening backticks:

```js
export default {
  name: 'MyComponent'
}
```

Supports all languagesarrow-up-right available in Shiki.

Line Highlighting

Highlight specific lines:

```js{4}
export default {
  data() {
    return {
      msg: 'Highlighted!'  // Line 4 highlighted
    }
  }
}
```

Multiple lines and ranges:

Inline highlighting with comment:

Focus

Blur other code and focus specific lines:

Focus multiple lines:

Colored Diffs

Show additions and removals:

Errors and Warnings

Color lines as errors or warnings:

Line Numbers

Enable globally:

Per-block override:

Start from specific number:

Code Groups

Tabbed code blocks:

Import Code Snippets

From external files:

With highlighting:

Specific region:

With language and line numbers:

In code groups:

File Labels

Add filename labels to code blocks:

Key Points

  • Use // [!code highlight] for inline highlighting

  • Use // [!code focus] to focus with blur effect

  • Use // [!code ++] and // [!code --] for diffs

  • Use // [!code error] and // [!code warning] for status

  • :line-numbers and :no-line-numbers control line numbers per block

  • @ in imports refers to source root

  • Code groups create tabbed interfaces

Last updated

Was this helpful?