How to Remove Empty Lines in VS Code Using Multiple Methods

Learn how to remove empty lines in VS Code using regular expressions, the Find and Replace feature, macros, and extensions.

Visual Studio Code (VS Code) is a powerful and flexible code editor, but when working with large files, unnecessary empty lines can clutter your workspace. Fortunately, VS Code provides multiple ways to remove empty lines efficiently, whether you prefer using regular expressions, built-in commands, or extensions. In this article, we’ll explore several methods to remove empty lines in VS Code, including using regex (^\s*$\n), the “Find and Replace” feature, macros, and extensions.

1. Removing Empty Lines Using Regular Expressions

One of the most efficient ways to remove empty lines in VS Code is by using regular expressions (regex). The regex pattern ^\s*$\n matches empty lines, including those with only whitespace characters.

Steps

  1. Open the Find and Replace Panel

    • Press Ctrl + H (Windows/Linux) or Cmd + Opt + F (Mac) to open the Find and Replace panel.
  2. Enable Regular Expression Mode

    • Click on the .* (regex) button in the Find field or press Opt+Cmd+R to enable regular expressions.
  3. Enter the Regex Pattern

    • In the Find field, enter:

      ^\s*$\n
      
    • This pattern finds lines that are empty or contain only whitespace.

  4. Leave the Replace Field Empty

    • This ensures that empty lines are deleted rather than replaced with any other text.
  5. Replace All

    • Click on Replace All to remove all empty lines in the document.

This method is particularly useful for quickly cleaning up files with unnecessary empty spaces.


2. Using VS Code’s Built-in “Trim Trailing Whitespace” Feature

VS Code has a built-in feature to remove trailing whitespace and empty lines automatically.

Steps

  1. Open Settings (Ctrl + , on Windows/Linux or Cmd + , on Mac).
  2. Search for “Trim Trailing Whitespace.”
  3. Enable the setting: “Files: Trim Trailing Whitespace”.
  4. Save the file (Ctrl + S / Cmd + S), and VS Code will automatically trim unnecessary whitespace and remove empty lines.

This method is useful when you want to maintain clean and formatted files as you work.


3. Using a VS Code Extension

If you frequently need to remove empty lines, installing an extension like “Trim Whitespace” or “Code Formatter” can automate the process.

Steps

  1. Open Extensions (Ctrl + Shift + X on Windows/Linux or Cmd + Shift + X on Mac).
  2. Search for “Trim Whitespace” or “Code Formatter.”
  3. Install the extension.
  4. Configure the extension settings if needed.
  5. Use the command “Trim Whitespace” from the Command Palette (Ctrl + Shift + P / Cmd + Shift + P).

Extensions can be a great solution if you want an automated approach without manually running regex searches every time.


4. Removing Empty Lines Using a VS Code Macro

Macros can be useful if you frequently need to remove empty lines in multiple files. With the “Macro” extension, you can automate the process.

Steps

  1. Install the “Macros” extension from the VS Code Marketplace.

  2. Open Settings and define a new macro:

    {
      "removeEmptyLines": [
        { "command": "editor.action.replaceAll", "args": { "find": "^\\s*$\\n", "replace": "" } }
      ]
    }
    
  3. Bind the macro to a shortcut (e.g., Ctrl + Alt + R).

  4. Run the macro whenever you need to clean up your files.

This method is ideal for automating repetitive cleanup tasks.


5. Using a Custom Keyboard Shortcut

If you often need to remove empty lines, binding the regex replacement to a shortcut can save time.

Steps

  1. Open Keyboard Shortcuts (Ctrl + K Ctrl + S).

  2. Click the gear icon and select “Open Keyboard Shortcuts (JSON)”.

  3. Add the following entry:

    {
      "key": "ctrl+alt+l",
      "command": "editor.action.replaceAll",
      "args": {
        "find": "^\\s*$\\n",
        "replace": ""
      }
    }
    
  4. Save the file and use Ctrl + Alt + L to remove empty lines instantly.

This method is excellent for those who prefer quick keyboard shortcuts.


6. Using Terminal Commands

For advanced users, using the terminal inside VS Code to clean up files can be an effective method.

Steps

  1. Open the Terminal (Ctrl + ~).

  2. Run the following command:

    sed -i '/^\s*$/d' filename.txt
    
    • This command removes all empty lines from filename.txt.
  3. Alternatively, use PowerShell on Windows:

    (Get-Content filename.txt) | Where-Object {$_ -match '\S'} | Set-Content filename.txt
    

These methods are useful for scripting and batch processing multiple files.


Conclusion

Removing empty lines in VS Code is easy and can be done using various methods depending on your preference. Whether you use regex (^\s*$\n), built-in commands, extensions, macros, keyboard shortcuts, or terminal commands, there’s a solution that fits your workflow. By automating this task, you can maintain clean, well-structured code and improve productivity.

Do you have a favorite method? Let us know in the comments!


Last modified 08.03.2025: new content (76eea7a)