<!--
Sitemap:
- [What is Vocs](/introduction/what-is-vocs): Learn why Vocs exists and when to use it
- [Getting Started](/introduction/getting-started): Install Vocs and create your first documentation site
- [Project Structure](/introduction/project-structure): Overview of the structure of a Vocs project
- [Writing Docs with AI](/introduction/writing-docs-with-ai): Use an AI agent to create and maintain Vocs documentation
- [Markdown Extensions](/writing/markdown-extensions): Features and syntax of Markdown in Vocs
- [Code & Syntax Highlighting](/writing/syntax-highlighting): Rich markup and annotations for code
- [Twoslash](/writing/twoslash): Add type-aware annotations to code examples
- [Code Snippets](/writing/code-snippets): Include and reuse code in Markdown
- [Markdown Snippets](/writing/markdown-snippets): Include other Markdown files in MDX
- [React in Markdown](/writing/react): Compose MDX pages with React components
- [Mermaid Diagrams](/writing/mermaid): Render diagrams from text using Mermaid
- [Assets](/writing/assets): Manage images, fonts, icons, and other docs assets
- [Frontmatter](/writing/frontmatter): Configure page metadata, layouts, search, and UI visibility
- [Navigation](/features/navigation): Keep docs navigation synced with routes
- [Search](/features/search): Built-in client-side search powered by MiniSearch
- [Layouts](/features/layouts): Choose and customize page shells for your docs
- [Slots](/features/slots): Inject custom components into the docs shell
- [Theming](/features/theming): Customize colors, typography, spacing, logos, and code themes
- [Tailwind CSS](/features/tailwind): Use Tailwind utilities in Vocs pages and components
- [Dynamic OG Images](/features/dynamic-og-images): Generate social preview images from page metadata
- [Page Feedback](/features/feedback): Collect page-level feedback from readers
- [Redirects](/features/redirects): Preserve old URLs and route legacy paths to new locations
- [API Routes](/features/api-routes): Add server-rendered endpoints to your docs site
- [Agent Support](/features/agent-support): Serve documentation in machine-readable form for AI agents
- [Ask AI](/features/ask-ai): Built-in AI assistant menu on every page
- [MCP Server](/features/mcp-server): Expose your docs and source code to AI assistants
- [Changelog Generation](/features/changelog-generation): Fetch release notes and render a changelog page
- [Site Configuration](/reference/site-config): Reference for options accepted by defineConfig
- [Frontmatter Reference](/reference/frontmatter): All frontmatter fields accepted by a Vocs MDX page
- [Components](/reference/components): Reference for the public React components exported from Vocs
- [Hooks](/reference/hooks): Reference for the React hooks exported from Vocs
- [Changelog](/changelog): Release history for Vocs
-->

# Code Snippets \[Include and reuse code in Markdown]

Code snippets in Vocs come in two forms:

* virtual file snippets defined in your Markdown code
* physical file snippets loaded from your file system

Use snippets when you want to reuse code across examples, keep docs examples close to source files, or compose larger examples from smaller regions.

For code formatting, highlighting, annotations, terminal output, and code groups, see [Code & Syntax Highlighting](/writing/syntax-highlighting).

## Virtual File Snippets

Define a virtual file with `filename="..."`, then include it in another code block.

::::steps

### Create the snippet

````mdx [example.mdx]
```ts filename="client.ts"
import { http, createPublicClient } from 'viem'
import { mainnet } from 'viem/chains'

export const client = createPublicClient({
  chain: mainnet,
  transport: http(),
})
```
````

### Import the snippet

````mdx [example.mdx]
#### Use Actions

```ts
// [\!include client.ts]
const blockNumber = await client.getBlockNumber()
```
````

### Output

#### Use Actions

```ts
// [!include client.ts]
const blockNumber = await client.getBlockNumber()
```

::::

## Physical File Snippets

Include code from files on disk with a root-relative `~/` path.

::::steps

### Create the snippet

```ts [src/snippets/example.ts]
// [!include ~/snippets/example.ts]
```

### Import the snippet

````mdx [example.mdx]
```ts
// [\!include ~/snippets/example.ts]
```
````

:::info
The `~` prefix resolves from your configured `srcDir`. With the default Vocs structure, `~/snippets/example.ts` resolves to `src/snippets/example.ts`.
:::

### Output

```ts
// [!include ~/snippets/example.ts]
```

::::

## Regions

Use `// [!region name]` and `// [!endregion name]` markers when you only want to include part of a source file.

:::code-group

```ts [src/snippets/example.ts]
// [\!region setup]
import { createPublicClient, http } from 'viem'
import { mainnet } from 'viem/chains'

export const client = createPublicClient({
  chain: mainnet,
  transport: http(),
})
// [\!endregion setup]
```

````mdx [example.mdx]
```ts
// [\!include ~/snippets/example.ts:setup]
```
````

:::

The rendered snippet only includes the selected region:

```ts
// [!include ~/snippets/example.ts:setup]
```

## Find and Replace

Use `/find/replace/` syntax to modify included code while rendering the page.

:::code-group

<div data-title="Preview">
  ```ts
  // [!include ~/snippets/example.ts:setup /mainnet/optimism/]
  ```
</div>

````mdx [example.mdx]
```ts
// [\!include ~/snippets/example.ts:setup /mainnet/optimism/]
```
````

:::

## Twoslash Virtual Files

Virtual files also work with [Twoslash](/writing/twoslash), so examples can import from each other and still get type information.

::::code-group

<div data-title="Preview">
  :::code-group

  ```ts twoslash [example.ts]
  import { hello } from './hello.ts'

  console.log(hello('Vocs'))
  ```

  ```ts twoslash [hello.ts] filename="hello.ts"
  export function hello(name: string = 'World') {
    return `Hello, ${name}!`
  }
  ```

  :::
</div>

````md [Markdown]
:::code-group
```ts twoslash [example.ts]
import { hello } from './hello.ts'

console.log(hello('Vocs'))
```
```ts twoslash [hello.ts] filename="hello.ts"
export function hello(name: string = 'World') {
  return `Hello, ${name}!`
}
```
:::
````

::::
