OneStopList

Creatures

Mermaid in Visual Studio Code: How to Use It

Updated November 10, 2026

Yes, you can use Mermaid diagrams directly in Visual Studio Code by installing an extension that adds Mermaid language support, live preview, and export options. The most popular choice is the “Mermaid Editor” or “Markdown Preview Mermaid Support” extension, which lets you write Mermaid syntax in Markdown files and see the diagram rendered in the built-in preview pane.

What Is Mermaid?

Mermaid is a JavaScript-based diagramming and charting tool that uses text-based syntax to generate diagrams. Instead of dragging shapes in a GUI, you write a simple code block like graph TD followed by lines that define nodes and connections. The tool parses that text and renders it as an SVG or PNG image. It supports flowcharts, sequence diagrams, class diagrams, state diagrams, Gantt charts, pie charts, and more. It was created by Knut Sveidqvist and is now an open-source project with a large community. Many developers use it because it keeps diagrams version-controlled and easy to review in code reviews.

Why Use Mermaid in Visual Studio Code?

Visual Studio Code (VS Code) is a free, open-source code editor from Microsoft. It has a rich extension marketplace, and Mermaid support is one of its most useful additions for developers and technical writers. Using Mermaid in VS Code gives you several advantages:

  • Live preview: You can see your diagram update in real time as you type, which speeds up iteration.
  • Version control: Diagrams are stored as text, so they can be diffed, reviewed, and merged just like code.
  • Integration with Markdown: Mermaid blocks work inside Markdown files, so you can keep documentation and diagrams in one place.
  • Export options: Some extensions let you export the rendered diagram as PNG, SVG, or even PDF.
  • Syntax highlighting: The editor highlights Mermaid syntax, making errors easier to spot.

How to Set Up Mermaid in VS Code

Setting up Mermaid in VS Code is straightforward. Here are the typical steps:

  1. Open VS Code and go to the Extensions view (Ctrl+Shift+X on Windows/Linux, Cmd+Shift+X on macOS).
  2. Search for “Mermaid” in the marketplace.
  3. Choose an extension. Popular options include “Markdown Preview Mermaid Support” by Matt Bierner and “Mermaid Editor” by tomoyukikashiro. Both are well-maintained.
  4. Click Install on the one you prefer. Some extensions require a reload of the VS Code window.
  5. Create or open a Markdown file (.md).
  6. Write a Mermaid code block using triple backticks with the language set to mermaid.
  7. Open the Markdown preview (Ctrl+Shift+V or Cmd+Shift+V) to see the diagram rendered.

If you use the “Mermaid Editor” extension, you can also open a dedicated Mermaid file (.mmd) and get a side-by-side preview that updates live.

Writing Mermaid Syntax in VS Code

Mermaid syntax is intuitive. For a simple flowchart, you write:

graph TD
    A[Start] --> B{Is it working?}
    B -- Yes --> C[Great!]
    B -- No --> D[Debug]
    D --> B

This renders a top-down flowchart with a decision diamond. You can also create sequence diagrams:

sequenceDiagram
    participant User
    participant Server
    User->>Server: Login request
    Server-->>User: Success

And Gantt charts for project timelines:

gantt
    title Project Plan
    dateFormat YYYY-MM-DD
    section Design
    Research: 2024-01-01, 7d
    Prototype: after Research, 5d

The syntax is documented on the official Mermaid website, and the VS Code extensions often include autocomplete and error highlighting to help you.

Common Problems and How to Fix Them

Users often run into a few issues when using Mermaid in VS Code. Here are the most common ones:

  • Diagram not rendering: Make sure you have the preview open (not just the editor) and that your Markdown file uses the correct language tag mermaid in the code fence. Also check that the extension is enabled.
  • Syntax errors not obvious: If the diagram doesn’t render, look for red squiggles in the editor. The extension usually points to the line with the error. Common mistakes include missing spaces around node labels or using unquoted special characters like parentheses.
  • Preview doesn’t update: Some extensions only refresh on save. Check the extension settings for a “live update” option.
  • Export fails: If you need to export, the Mermaid Editor extension offers export to PNG/SVG. If you use Markdown Preview Mermaid Support, you might need to use the browser’s print function or a separate tool.

Mermaid vs. Other Diagram Tools in VS Code

Mermaid is not the only diagramming option for VS Code. Alternatives include PlantUML, Draw.io Integration, and ASCII art diagrams. Here’s a quick comparison:

  • Mermaid: Lightweight, easy to learn, and great for simple to medium-complexity diagrams. It renders quickly and integrates well with Markdown.
  • PlantUML: More powerful for UML diagrams (class, sequence, use case) and supports more diagram types, but the syntax is more verbose and requires Java to run.
  • Draw.io Integration: Allows you to edit .drawio files directly in VS Code, but it uses a graphical interface rather than text, so it’s less version-control friendly.
  • ASCII diagrams: Tools like AsciiDoc or Monodraw let you create text-based diagrams, but they lack the automated layout of Mermaid.

For most developers, Mermaid strikes the best balance between simplicity and functionality, especially if you already use Markdown.

Cost and Safety Considerations

Both Mermaid and the VS Code extensions mentioned are free and open-source. There are no hidden costs or premium tiers for the core functionality. However, if you use a cloud-based VS Code service like GitHub Codespaces, you may incur compute costs, but the extension itself is free. As for safety, Mermaid runs client-side in your browser or VS Code preview, so it does not send your diagram data to any server. The extensions are open-source and widely used, but always check the extension’s repository for any permissions it requests. Avoid installing extensions from unverified publishers.

Frequently Asked Questions

How do I install Mermaid in VS Code? Open the Extensions view, search for “Mermaid”, and install a popular extension like “Markdown Preview Mermaid Support” or “Mermaid Editor”. After installation, restart VS Code if prompted.

Can I use Mermaid in a regular .js file? No, Mermaid is not JavaScript code. It’s a separate markup language. You use it in Markdown files or dedicated .mmd files, not in source code files.

Why is my Mermaid diagram not showing in preview? Ensure you have the preview open (Ctrl+Shift+V) and that your code block is tagged as mermaid. Also, check the extension’s output panel for errors. If you’re using a custom theme, try the default light theme to rule out CSS conflicts.

Can I export my Mermaid diagram as an image? Yes, the “Mermaid Editor” extension allows exporting to PNG and SVG. For “Markdown Preview Mermaid Support”, you can right-click the preview and choose “Save as” or use a browser extension to capture the image.

Does Mermaid work with GitLab or GitHub? Yes, both GitHub and GitLab natively render Mermaid diagrams in Markdown files. So you can write the same Mermaid code in VS Code and it will display on those platforms without extra steps.

Is Mermaid suitable for large diagrams? Mermaid can handle moderately complex diagrams, but very large ones (hundreds of nodes) may become slow to render. For such cases, consider splitting the diagram into smaller parts or using a more powerful tool like PlantUML.

Related

How pages are sourced and corrected: Sources and editorial policy.