How to Remove Empty Lines in VS Code – Blank lines cluttering your code or text file?
VS Code gives you several fast ways to remove empty lines, no plugins required in most cases.
This guide covers the quickest methods, from regex Find & Replace to dedicated extensions.
What Counts as an “Empty Line” in VS Code?
An empty line in VS Code is any line that contains:
- No characters at all, or
- Only whitespace characters (spaces, tabs)
Both types can be targeted and removed using the methods below.
Method 1: Find & Replace with Regex (Fastest, No Extension Needed)
This is the most reliable and universally applicable method.
Steps:
- Open Find & Replace:
Ctrl+H(Windows/Linux) orCmd+H(Mac) - Click the
.*icon to enable Regex mode - In the Find field, enter:
^\s*\n - Leave the Replace field completely empty
- Click Replace All
What this regex does:
^— matches the start of a line\s*— matches any whitespace (spaces, tabs) on that line\n— matches the newline character at the end
Tip: To only remove empty lines within a selected block of text, highlight the text first, then enable “Find in Selection” (the selection icon in the Find bar).
Alternatively, you can also use the following regex:
^\s*$\nMethod 2: Collapse Multiple Empty Lines Into One
If you want to reduce multiple consecutive blank lines to a single one (instead of removing all blanks):
- Find:
(\n\s*){2,} - Replace:
\n\n
This is useful for cleaning up code that requires some whitespace for readability.
- How to Connect Claude Code to Discord Using Claude Code Channels
- EmDash by Cloudflare: The Open-Source CMS That Wants to Replace WordPress
- How to Install and Set Up Claude Code on macOS, Linux, and Windows (2026)
- Nano Banana 2 Python Tutorial: Image Generation, Subject Consistency, and Search Grounding
- Python Exercises for Beginners With 20+ Real Projects
Method 3: Use an Extension
For a GUI-friendly approach, install the Remove Empty Lines extension from the VS Code Marketplace.
Once installed:
- Select the text you want to clean (or select nothing to apply to the whole file)
- Open the Command Palette:
Ctrl+Shift+P/Cmd+Shift+P - Search for: “Remove Empty Lines”
- Choose “in Document” or “in Selection”
Method 4: Sort Lines (Indirect Method)
VS Code has a built-in “Sort Lines Ascending” command that pushes empty lines to the top or bottom, making them easy to delete manually.
This is less precise but works as a quick workaround.
Quick Reference Table
| Method | Requires Extension | Works on Selection | Removes Whitespace-Only Lines |
|---|---|---|---|
| Regex Find & Replace | No | Yes | Yes |
| Collapse multiple blanks | No | Yes | Yes |
| Remove Empty Lines ext. | Yes | Yes | Yes |
| Sort Lines | No | No | Moves, doesn’t remove |
Summary
To remove empty lines in VS Code:
- Fastest native method: Find & Replace with regex
^\s*\n→ empty replace field → Replace All - For multiple blank lines: Use
(\n\s*){2,}→ replace with\n\n - GUI preference: Install the Remove Empty Lines extension
The regex method works in any file type, HTML, CSS, JavaScript, Python, Markdown, or plain text, with no additional setup.
Frequently Asked Questions (FAQs)
Does VS Code have a built-in “Remove Empty Lines” command?
No. VS Code does not include a dedicated built-in command for this.
The fastest native method is the regex Find & Replace approach using ^\s*\n.
How do I remove empty lines from only part of a file?
Select the text first, then open Find & Replace and enable “Find in Selection” before running the regex replacement.
Will this remove lines with only spaces or tabs?
Yes. The regex ^\s*\n matches lines with any amount of whitespace (including none), so both truly empty lines and lines with only spaces/tabs are removed.
What’s the best VS Code extension for removing empty lines?
Remove Empty Lines by usernamehw is a lightweight, well-maintained extension available on the VS Code Marketplace.
It adds a simple command palette entry with no extra configuration needed.