You want to insert a newline inside a VS Code find-and-replace operation and the editor just stares back at you. You press Enter — it triggers the replace. You type \n — nothing happens in normal mode. VS Code's vscode find and replace panel handles newlines differently depending on whether regex mode is active, which is why so many developers get tripped up. This guide covers every method — from the instant Ctrl+Enter trick to full regex patterns with capture groups, case modifiers, and multiline matching — plus the CRLF gotchas that quietly break Windows workflows.

Find & Replace Search ,\s* .* Aa Regex ON (blue = active) Replace \n ↵ Ctrl+Enter inserts newline here Replace All Blue .* = regex mode active. Ctrl+Enter = literal newline in replace field (not submit) With regex ON: \n in replace inserts a real line break
VS Code Find & Replace panel: the blue .* button activates regex mode. Inside the Replace field, Ctrl+Enter inserts a visible line break rather than triggering the replace action.

Why VS Code's Find & Replace Trips Up on Newlines

VS Code's find-and-replace widget (Ctrl+H on Windows/Linux, ⌘H on Mac) uses two completely separate engines depending on the mode toggle:

  • Plain text mode — literal character matching. The Enter key submits the replace action; there is no way to type a literal newline into the search or replace field.
  • Regex mode (click the .* button) — VS Code's regex engine, which is based on JavaScript's RegExp implementation. In this mode \n matches a newline in the search field, and \n inserts a newline in the replace field.

That single distinction explains nearly every "why isn't my vs code replace with newline working?" question — and the same gotcha trips up the vscode replace with new line searches you see on Stack Overflow. The sections below cover each method in detail, ordered from fastest to most powerful.

If your goal is to inspect what actually changed after a bulk replace, the VS Code file diff guide walks through every way to open the built-in diff viewer side-by-side.

Method 1: The Ctrl+Enter Shortcut (Quick & Dirty)

The fastest way to insert a literal newline into VS Code's replace field — without touching regex mode — is Ctrl+Enter (⌘+Enter on Mac). This works in both the search field and the replace field.

Steps

  1. Open Find & Replace: Ctrl+H (Windows/Linux) or ⌘H (Mac).
  2. Type your search term in the top field.
  3. Click inside the Replace field.
  4. Press Ctrl+Enter where you want the newline. VS Code inserts a visible line break inside the replace box.
  5. Click Replace All or step through matches individually.

When to use it

Use Ctrl+Enter when your search term is plain text (no regex needed), you want to split one line into two, and you need only one newline. It is the quickest path for simple tasks like turning a comma-separated header row into two separate lines.

Limitations

You cannot use Ctrl+Enter to search for existing newlines in the file — it only inserts newlines into the replace side. For finding and removing or replacing existing line breaks, you need regex mode (Method 2).

Ctrl + Enter Replace field first_name last_name ↵ newline How it works in plain-text mode Ctrl+Enter types a literal newline character into the box — Enter alone would trigger Replace All Works in BOTH Search and Replace fields. No regex mode required. Limitation: cannot search for existing newlines — use regex \n for that
Ctrl+Enter inserts a literal newline inside the Replace field without submitting the operation. The replace field expands to show a visible line break. Regular Enter would instead execute Replace All.

Method 2: Regex Mode with \n (The Reliable Way)

Enabling regex mode unlocks the full power of vscode replace with new line operations. VS Code's regex engine follows JavaScript RegExp semantics, so the standard escape sequences work as expected.

Enable regex mode

Click the .* button in the Find & Replace widget to toggle regex mode. The icon turns blue when regex is active. Regex mode can also be toggled via the default VS Code keyboard shortcut or custom keybindings if configured in settings.

Newline tokens

  • \n — matches/inserts a Unix newline (LF, 0x0A).
  • \r\n — matches a Windows CRLF sequence in the search field. Use with care (see the CRLF section below).
  • \r — carriage return only; rarely needed but valid in the search field.

Quick example: replace a comma with a newline

Scenario: you have a one-liner CSV header and want each field on its own line.

Search:  ,
Replace: \n

With regex on, every comma becomes a line break. No capture groups needed for this simple case.

How do I insert a newline in VS Code find and replace?

Short answer: enable regex mode (click the .* button), then type \n in the Replace field wherever you want a line break. If regex is off, use Ctrl+Enter instead — that inserts a literal newline directly into the replace box.

What's the difference between \n and \\n?

Inside VS Code's replace field, \n is the newline escape sequence interpreted by the regex engine. \\n (double backslash) escapes the backslash itself, producing the literal two-character string \n in your output — not a line break. Use \n (single) when you want a real newline.

Find & Replace Regex OFF \n .* (space) No matches — \n is literal text Result: searches for the 2-char string \n PLAIN MODE \n = literal backslash-n Find & Replace Regex ON \n .* Matches every line break in file Result: replaces newline (0x0A) with space REGEX MODE \n = actual newline character Same \n token — completely different behaviour depending on the .* toggle
Left: regex mode off — \n is treated as two literal characters and will not match newlines in your file. Right: regex mode on — \n matches actual line-feed bytes. The blue .* button is the only switch.

Method 3: Multiline Paste Workaround

Sometimes you already have a multiline snippet on your clipboard. VS Code's search field accepts multiline pastes even in plain-text mode — a lesser-known behaviour that can save time.

Steps

  1. Copy a multiline string to your clipboard (e.g., from another file or a terminal).
  2. Open Find & Replace (Ctrl+H).
  3. Click in the Search field and paste (Ctrl+V). VS Code displays the pasted content across multiple lines inside the widget.
  4. Fill in the Replace field (single or multiline).
  5. Click Replace All.

This workaround is handy when your search pattern is easier to compose in a scratch file than to escape in a regex. The limitation is that each paste must match the exact whitespace in the source file — tabs versus spaces matter.

Method 4: Global Find & Replace Across the Workspace

All four methods above work in the single-file widget. For workspace-wide operations, use Search across files: Ctrl+Shift+H (Windows/Linux) or ⌘⇧H (Mac). The same regex toggle (Alt+R) and Ctrl+Enter shortcut apply in the sidebar panel.

Scope controls

  • Files to include — glob patterns like **/*.ts or src/**.
  • Files to exclude — e.g., node_modules,dist.
  • Open editors only — click the book icon to restrict scope.

Global vs code find and replace with new line workflows are the norm when refactoring logging patterns, normalizing line endings across a project, or converting import styles across dozens of files at once.

After a global replace, diff the affected files to confirm every substitution looks correct. Pair regex find-and-replace with multi-cursor bulk edits for refactors that mix structural patterns and one-off tweaks — Ctrl+Z is fine for small undos, but a visual check beats undoing 300 replacements.

Real-World Regex Patterns

Here are practical patterns developers use every day. All require regex mode to be active.

Replace spaces in a newline to only the newline

Scenario: remove trailing spaces before a line break.

Search:  \s+\n
Replace: \n

How do I replace newlines with spaces?

Swap the positions — search for the newline, replace with a space:

Search:  \n
Replace: (single space)

How do I remove empty lines?

Empty lines are just consecutive newlines. Match a newline that is immediately followed by another newline (or only whitespace):

Search:  ^\s*\n
Replace: (leave empty)

CSV one-liner to vertical list

Turn alpha,beta,gamma,delta into one item per line:

Search:  ,\s*
Replace: \n

JSON array of strings to newline-delimited values

Strip ", [, ], and commas; put each value on its own line:

Search:  "[^"]*"(?:,\s*)?
Replace: ${0}\n

Note: the outer structure still needs manual cleanup, but this pattern pulls quoted strings out line by line. For comparing the before/after of a JSON transformation, the JSON diff guide shows the fastest approach.

Log line reformatting

Scenario: convert single-line log entries like [ERROR] 2026-04-26 Connection refused into two lines — timestamp on line one, message on line two.

Search:  (\[ERROR\]) (\d{4}-\d{2}-\d{2}) (.+)
Replace: $1 $2\n        $3
BEFORE 1 [ERROR] 2026-04-26 Conn refused 2 [ERROR] 2026-04-25 Timeout 30s 3 [ERROR] 2026-04-24 Disk full Search: (\[ERROR\]) (\d{4}-\d{2}-\d{2}) (.+) Replace: $1 $2\n $3 AFTER 1 [ERROR] 2026-04-26 2 Conn refused 3 [ERROR] 2026-04-25 4 Timeout 30s 5 [ERROR] 2026-04-24 6 Disk full $1 = [ERROR] $2 = date $3 = message \n inserts line break between $2 and $3 Each log entry now spans 2 lines Capture groups $1 $2 $3 preserve existing content; \n splits into two lines 3 log entries become 6 lines after Replace All
Before/after log reformatting: each single-line [ERROR] date message entry is split into two lines using capture groups $1, $2, $3 with a \n inserted between the date and message.

Capture Groups & Backreferences

Capture groups let you reference matched text in the replace field, making vs code replace with new line operations dramatically more powerful than simple substitution.

Syntax

  • (pattern) — capturing group; referenced as $1, $2, … in the replace field.
  • (?:pattern) — non-capturing group; useful for grouping without consuming a backreference slot.
  • $0 or $& — the entire match.

Example: split a function signature onto multiple lines

Input: function render(props, state, context) {
Goal: put each parameter on its own line. Note: to reference a captured group in the search field for backreferences, use \1, \2, etc. In the replace field, use $1, $2, etc.

Search:  function (\w+)\(([^)]+)\)
Replace: function $1(\n  $2\n)

$1 captures the function name; $2 captures the parameter list. The replace inserts newlines before and after the parameter block. You can extend $2 with another regex pass to split individual parameters if there are multiple.

Example: wrap import statements

Turn import { A, B, C } from 'module'; into named imports on separate lines:

Search:  import \{ ([^}]+) \} from ('([^']+)')
Replace: import {\n  $1\n} from $2

Add a second pass with ,\s*,\n to split individual imports. This pairs well with VS Code's code folding and formatting features to keep large import blocks readable.

Case Transformations (\L, \u, \U)

VS Code natively does not support case-transformation modifiers like \L, \U in find and replace, unlike some other editors. However, you can achieve similar results using workarounds or third-party extensions. This section is retained for reference with common patterns seen in other editors:

  • \u — Not supported natively in VS Code's regex engine.
  • \U — Not supported natively in VS Code's regex engine.
  • \l — Not supported natively in VS Code's regex engine.
  • \L — Not supported natively in VS Code's regex engine.
  • \E — Not supported natively in VS Code's regex engine.

Alternative: Use the Super Replace extension or split your replacements into multiple passes using different capture groups and patterns to achieve case transformations.

Workaround: snake_case to camelCase (multiple passes)

Since VS Code doesn't natively support \u, use manual find-and-replace passes or an extension:

// First pass: find and replace each specific variable pattern manually
// Or use the Super Replace extension which supports case modifiers

For complex case transformations, third-party extensions provide the functionality found in other editors.

Alternative: SQL keyword normalization

VS Code's JavaScript regex engine doesn't have built-in case modifiers, so SQL keyword normalization requires a different approach or an extension. For simpler patterns without case modifiers, use standard regex with explicit replacements.

CRLF vs LF: Platform Pitfalls

This is the section that resolves the most "my regex works on Mac but not Windows" reports. VS Code's regex engine sees the raw bytes on disk, so the line-ending encoding of the file you are editing changes how \n and \r\n behave.

What VS Code shows vs. what is on disk

Look at the bottom status bar. A file with Windows line endings shows CRLF; a Unix file shows LF. VS Code renders both identically in the editor — no visible difference — but the regex engine operates on the underlying bytes.

  • In a CRLF file, each line break is \r\n. A search for \n still matches, because VS Code normalises internally — but a search for \r\n is explicit and more reliable when you need to match the exact sequence.
  • In a LF file, each line break is \n. Searching for \r\n will find zero matches.

Converting CRLF to LF via find and replace

Search:  \r\n
Replace: \n

After Replace All, click the CRLF indicator in the status bar and select LF to also update VS Code's internal line-ending setting for the file. Save and commit. The Unix diff command guide explains how diff --strip-trailing-cr handles this on the command line.

Whitespace normalization tip

VS Code's built-in auto-save settings (files.eol in settings.json) can normalise line endings on save, preventing CRLF creep in cross-platform teams. Set it to "\n" for LF or "\r\n" for CRLF. This complements — but does not replace — the find-and-replace patterns above for fixing already-committed files.

Windows file bytes (CRLF) H e l l o \r \n W o r l d CRLF (0D 0A) Unix / macOS file bytes (LF only) H e l l o \n W o r l d LF only (0A) main.ts Ln 42, Col 8 UTF-8 CRLF Click to change line ending type CRLF files need \r\n in search; LF files need \n only Click the CRLF/LF indicator in the status bar to change the file's line ending
CRLF (Windows) vs LF (Unix) byte sequences. The VS Code status bar shows the current file's line ending — clicking it lets you convert between CRLF and LF. Regex patterns must match the actual bytes on disk.

Multiline Search Patterns

Can VS Code do multiline find and replace? Yes — with regex mode enabled, patterns can span multiple lines using a few different approaches.

Dot-all matching with [\s\S]

By default, the dot . does not match newlines. The standard workaround is [\s\S], which matches any character including newlines:

Search:  /\*[\s\S]*?\*/
Replace: (empty)

This deletes all block comments (including multiline ones) from a file. The *? makes the quantifier lazy — it stops at the first */ rather than the last one on the page.

Matching a block between two markers

Search:  BEGIN_BLOCK[\s\S]*?END_BLOCK\n
Replace: (empty)

Useful for stripping generated code sections, debug output blocks, or migration remnants.

Matching N consecutive empty lines

Collapse three or more blank lines into a single blank line:

Search:  (\n\s*){3,}
Replace: \n\n

Performance note

Multiline patterns with [\s\S]* on large files can be slow because the engine must backtrack across the entire buffer. Prefer lazy quantifiers (*?, +?) and anchor your patterns as tightly as possible with known start/end tokens.

Troubleshooting & Common Errors

Regex not working? Is regex mode (.*) active? NO Press Alt+R YES Is the file CRLF (status bar)? YES Use \r\n not \n NO Pattern crosses line boundaries? YES Use [\s\S] not dot (.) NO Check pattern syntax: unclosed groups, wrong escaping, or scope mismatch Follow each branch top-to-bottom to isolate your specific problem
Troubleshooting decision tree: start at the top and follow each branch to isolate why a regex find-and-replace is not working in VS Code. The three most common causes are regex mode being off, a CRLF file needing \r\n, and a multiline pattern needing [\s\S].

"\n not inserting a newline in replace"

Almost always caused by regex mode being off. Click the .* button or press Alt+R to activate it. In plain mode, \n is a literal two-character string, not an escape sequence.

"Replace All ran but nothing changed"

  • Check the file's line ending (CRLF vs. LF). Your search pattern may target \n but the file uses \r\n.
  • Check that regex mode is active — the .* button should be highlighted.
  • Make sure the cursor is not inside a string that breaks the pattern (e.g., an unclosed regex group).
  • Verify the scope: single-file vs. workspace-wide. The global panel (Ctrl+Shift+H) has separate regex/case settings from the single-file widget (Ctrl+H).

"Regex works in grep but not in VS Code"

VS Code uses JavaScript's RegExp engine, not PCRE (Perl Compatible Regular Expressions). Key differences:

  • No lookbehind of variable length in older V8 versions (fixed in recent releases).
  • No (?s) dot-all flag inline — use [\s\S] instead.
  • No \K (keep-left) — restructure using capture groups.
  • Backreferences in the search field use \1; in the replace field they use $1.

"My capture group shows up as literal $1"

This happens when regex mode is off. The replace field only expands $1, $2 etc. when regex is active. Turn on regex mode (Alt+R) and try again.

"Case modifier \U not working"

VS Code's regex engine (based on JavaScript's RegExp) does not natively support case modifiers like \u, \L, \U, \l, \E. These are supported in other editors (IntelliJ, Sublime Text) but not in VS Code. For case transformation, use the Super Replace extension or split your replacements into multiple passes.

Frequently Asked Questions

How do I insert a newline in VS Code find and replace?

Enable regex mode by clicking the .* button (or pressing Alt+R), then type \n in the Replace field wherever you want a line break. If regex mode is off, press Ctrl+Enter (⌘+Enter on Mac) inside the Replace field — that inserts a literal newline character without triggering Replace All. This is the fastest answer to the most common vs code find and replace with new line question.

Why doesn't \n work in VS Code's replace field?

If \n is appearing as literal backslash-n instead of a line break, regex mode is turned off. Click the .* icon in the Find & Replace widget (it turns blue when active) or press Alt+R. In plain-text mode VS Code treats \n as two literal characters, not an escape sequence — see the official VS Code Find & Replace docs for the full toggle reference.

How do I replace newlines with spaces in VS Code?

Turn on regex mode, search for \n and replace with a single space character. For Windows CRLF files, search for \r\n instead. Check the file's line ending in the VS Code status bar (bottom-right) before running Replace All so the pattern matches the actual bytes on disk. This is the same approach the vs code replace with newline workflow uses, just inverted.

How do I remove all empty lines in VS Code?

Enable regex mode and search for ^\s*\n with the Replace field left empty. This pattern targets any line that contains only optional whitespace followed by a newline and removes it. To collapse three or more blank lines into a single blank line instead, use search (\n\s*){3,} replace \n\n. The \s token is a standard JavaScript RegExp character class that matches any whitespace character.

Can VS Code do multiline find and replace across files?

Yes. Press Ctrl+Shift+H (⌘⇧H on Mac) to open the Search across files panel, enable regex with Alt+R, and use [\s\S]*? to match content spanning line boundaries. Use the Files to include / exclude glob filters to scope the workspace search to specific directories or file types — global vs code replace with new line operations are how teams normalise logging patterns or import styles across hundreds of files in one pass.

Method Best For Speed Regex Needed Limitations
Ctrl+Enter (no regex) Quick one-off replacements in a single file Fastest No Replace field only; cannot search for existing newlines
Regex \n mode Finding and replacing existing line breaks with patterns Fast Yes Must enable regex toggle; CRLF files may need \r\n
Multiline paste Matching a literal multiline block you already have on clipboard Medium No Whitespace must match exactly (tabs vs spaces); no pattern flexibility
Global find & replace (Ctrl+Shift+H) Bulk changes across many files in the workspace Slower on large repos Optional Separate regex/case settings from single-file widget; review each changed file

Compare Files Visually After Your Find & Replace

Once you've cleaned up your code with regex, see exactly what changed. Diff Checker shows every line addition, deletion, and modification side-by-side — supports 20+ languages with syntax highlighting.

Get Diff Checker Free