Regular Expression for Empty Lines in Visual Studio Code (VS Code)

In VS Code, you can use the following regular expressions (regex) to find and manipulate empty lines in your code.


✅ 1. Find Completely Empty Lines

^\s*$

🔹 This matches entirely empty lines or lines containing only spaces or tabs.


✅ 2. Find Lines That Are Truly Empty (No Spaces)

^$

🔹 This matches only completely blank lines, excluding lines with spaces or tabs.


✅ 3. Remove All Empty Lines

If you want to remove all empty lines, use:

  1. Open Find & Replace (Ctrl + H or Cmd + H on Mac).
  2. Enter this regex in the Find field:
       ^\s*\n
  3. Leave the Replace With field empty.
  4. Enable Regex mode (.* icon in the search bar).
  5. Click Replace All.

🔹 This removes all blank lines, including lines with spaces or tabs.


✅ 4. Remove Consecutive Empty Lines (Keep Single Empty Line)

If you want to replace multiple empty lines with a single empty line, use:

(\n\s*){2,}

Replace it with:

\n

🔹 This reduces multiple empty lines to just one.


✅ 5. Remove Blank Lines at the End of a File

(\n\s*)+$

🔹 This removes trailing blank lines at the end of a file.


Need More Regex Help in VS Code?

Let me know if you need custom regex patterns for your specific use case! 🚀