Visual Studio Code
Text Editing
Programming
Coding Tips
Software Development

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

  1. Press Ctrl+H (Windows/Linux) or Cmd+Option+F (Mac) to open Find and Replace
  2. Click the .* button on the right side of the search field to enable regex mode (this is required for \n to work)
  3. In the Search field, type the text you want to find
  4. In the Replace field, type \n where you want a newline
  5. 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:

 
apple, banana, cherry, date

After:

 
1apple
2banana
3cherry
4date

Convert semicolons to newlines (SQL, CSS, etc.)

Search: ;\s* Replace: ;\n

Before:

css
margin: 0; padding: 0; border: none; display: flex;

After:

css
1margin: 0;
2padding: 0;
3border: none;
4display: flex;

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:

javascript
function hello() { console.log("hi"); return true; }

After:

javascript
function hello() {
 console.log("hi"); return true; }

Split a JSON array into separate lines

Search: ,\s*" Replace: ,\n"

Before:

json
["one", "two", "three", "four"]

After:

json
1["one",
2"two",
3"three",
4"four"]

Regex patterns for newlines

PatternMeaningExample Use
\nNewline character (line feed)Insert or match newlines
\r\nCarriage return + line feed (Windows line endings)Match Windows-style line breaks
\rCarriage return onlyMatch old Mac-style line breaks
\n+One or more newlinesCollapse multiple blank lines
\n\s*\nBlank 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:

 
name=John age=30 city=NYC

After:

 
name = John
age = 30
city = NYC

Convert inline HTML attributes to multi-line

Search: \s+([\w-]+)= Replace: \n $1=

Before:

html
<div class="container" id="main" data-value="42" style="color:red">

After:

html
1<div
2  class="container"
3  id="main"
4  data-value="42"
5  style="color:red">

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

  1. Press Ctrl+Shift+H (Windows/Linux) or Cmd+Shift+H (Mac) to open the global search and replace panel
  2. Enable regex mode with the .* button
  3. Use \n in the replace field the same way as in single-file mode
  4. Use the files to include and files to exclude fields to scope your changes (for example, *.csv or src/**/*.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

ActionWindows / LinuxmacOS
FindCtrl+FCmd+F
Find and ReplaceCtrl+HCmd+Option+F
Find in FilesCtrl+Shift+FCmd+Shift+F
Replace in FilesCtrl+Shift+HCmd+Shift+H
Toggle RegexAlt+ROption+R
Replace NextCtrl+Shift+1Cmd+Shift+1
Replace AllCtrl+Alt+EnterCmd+Option+Enter
Next MatchEnter or F3Enter or Cmd+G
Previous MatchShift+F3Shift+Cmd+G

Why the replacement inserts a literal \n (and other mistakes)

  • Regex mode is off. Without the .* toggle, \n is literal text, so the replacement inserts a backslash followed by the letter n.
  • Using \r\n unnecessarily. VS Code normalizes line endings internally. Use \n in 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 \n starts at column 1. If you need indentation, include it in the replace string: \n (newline followed by four spaces).
  • \n in search vs. replace. With regex enabled, \n in the search field matches existing newlines; in the replace field it inserts one. Same syntax, different roles.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data 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.

Practice algorithms