> For the complete documentation index, see [llms.txt](https://notes.eliasnorrby.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notes.eliasnorrby.com/vim/replace-line.md).

# Insert line above matched line

When used in a substitution command, `&` is replaced by the text that matches the search pattern. If we match a pattern from the beginning of a line, we can insert something above it by replacing the match with the new content, a line break and the matched pattern.

```ts
export class MyClass {
  property: string;
}
```

```vim
:%s/^export class/\/\/ This is a class\r&/
```

```diff
+ // This is a class
  export class MyClass {
    property: string
  }
```

Source: `:h &`
