Replace with a newline in VS Code find and replace
Codemia Knowledge Hub ·
To replace text with a newline in VS Code, open Find and Replace with Ctrl+H (Windows/Linux) or Cmd+Option+F (Mac), enable regex mode with the .* button, then use \n in the search or replace field. The same approach inserts newlines, removes them, or restructures text.
Replacing text with a newline, step by step
- Press
Ctrl+H(Windows/Linux) orCmd+Option+F(Mac) to open Find and Replace - Click the
.*button on the right side of the search field to enable regex mode (this is required for\nto work) - In the Search field, type the text you want to find
- In the Replace field, type
\nwhere you want a newline - Click the replace button (single) or replace all button
The regex toggle is the step most people miss. Without it, VS Code treats \n as the literal characters backslash and n.
Common newline replacements
Convert comma-separated values to separate lines
Search: ,\s*
Replace: \n
Before:
After:
Convert semicolons to newlines (SQL, CSS, etc.)
Search: ;\s*
Replace: ;\n
Before:
After:
Remove blank lines
Search: \n\n+
Replace: \n
This replaces two or more consecutive newlines with a single newline, collapsing all blank lines.
Join multiple lines into one line
Search: \n
Replace: (a single space)
Every newline becomes a space, turning a multi-line block into a single line.
Add a newline after every opening brace
Search: \{
Replace: {\n
Before:
After:
Split a JSON array into separate lines
Search: ,\s*"
Replace: ,\n"
Before:
After:
Regex patterns for newlines
| Pattern | Meaning | Example Use |
\n | Newline character (line feed) | Insert or match newlines |
\r\n | Carriage return + line feed (Windows line endings) | Match Windows-style line breaks |
\r | Carriage return only | Match old Mac-style line breaks |
\n+ | One or more newlines | Collapse multiple blank lines |
\n\s*\n | Blank line (newline, optional whitespace, newline) | Find empty lines with whitespace |
$ | End of line (in regex mode) | Match position at line end |
^ | Start of line (in regex mode) | Match position at line start |
Capture groups combined with newlines
Wrap parts of the search in parentheses () and reference them in the replace field with $1, $2, etc. to rearrange text while inserting newlines.
Reformat key-value pairs onto separate lines
Search: (\w+)=(\w+)\s*
Replace: $1 = $2\n
Before:
After:
Convert inline HTML attributes to multi-line
Search: \s+([\w-]+)=
Replace: \n $1=
Before:
After:
Extract email addresses to separate lines
Search: ([\w.+-]+@[\w-]+\.[\w.]+)
Replace: $1\n
This puts each email-like match on its own line.
Replacing newlines across multiple files
- Press
Ctrl+Shift+H(Windows/Linux) orCmd+Shift+H(Mac) to open the global search and replace panel - Enable regex mode with the
.*button - Use
\nin the replace field the same way as in single-file mode - Use the files to include and files to exclude fields to scope your changes (for example,
*.csvorsrc/**/*.ts)
Be careful with Replace All across files. There is no single undo for multi-file replacements: each file keeps its own undo history, so you would have to undo in each modified file.
Keyboard shortcuts
| Action | Windows / Linux | macOS |
| Find | Ctrl+F | Cmd+F |
| Find and Replace | Ctrl+H | Cmd+Option+F |
| Find in Files | Ctrl+Shift+F | Cmd+Shift+F |
| Replace in Files | Ctrl+Shift+H | Cmd+Shift+H |
| Toggle Regex | Alt+R | Option+R |
| Replace Next | Ctrl+Shift+1 | Cmd+Shift+1 |
| Replace All | Ctrl+Alt+Enter | Cmd+Option+Enter |
| Next Match | Enter or F3 | Enter or Cmd+G |
| Previous Match | Shift+F3 | Shift+Cmd+G |
Why the replacement inserts a literal \n (and other mistakes)
- Regex mode is off. Without the
.*toggle,\nis literal text, so the replacement inserts a backslash followed by the letter n. - Using
\r\nunnecessarily. VS Code normalizes line endings internally. Use\nin the replace field regardless of the file's line ending setting; VS Code converts it to the right line ending for the file. - Unescaped special characters.
.,*,+,?,(,),[,],{,},^,$and|have special meaning in regex. Prefix them with a backslash to match them literally:\.,\*, etc. - Replace All without a preview. Use the single replace button a few times to verify the result before running Replace All, especially with complex patterns.
- Lost indentation. A line created by
\nstarts at column 1. If you need indentation, include it in the replace string:\n(newline followed by four spaces). \nin search vs. replace. With regex enabled,\nin the search field matches existing newlines; in the replace field it inserts one. Same syntax, different roles.
Related reading
- Find image format using Bitmap object in C
- Find the highest occuring words in a string C
- Find the next TCP port in .NET
- Find type of nullable properties via reflection
- Find Types in All Assemblies
- Finding all positions of substring in a larger string in C
- Finding an item in a List using C
- Finding property differences between two C objects

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.