Trinix Quick Tip!
Using Multi-Selection Regular Expression to Remove Empty Lines in Visual Studio Code VS Code

Replacing Blank Lines Using RegEx in Visual Studio Code (VS Code)

This tip covers how I went about cleaning up a JSON file that was left with empty lines that all had various levels of indentation (tabs & spaces). Basically I have a bunch of blank lines that I need to remove. and was having a hard time using the multi-select to do what I wanted it to. Need to get rid of blank lines that have different text above and below, making multiple-selection difficult.

The Conundrum:

The multi-select in VS Code is a very useful tool & I’ve grown quite fond of it and there’s not much that I can’t refactor in a “structured” text file. The key is “structured”. As long as you can spot a pattern, you’ll be able to clean up a textual based file with little effort.

I usually look for a common string of characters that are consistent through all the items I want to multi-select. In this case (and in most key:value data sets I can think of), I can select a key to begin my multi-select refactor.

I then, since I’m currently using the Linux version of Visual Studio Code, press the following keys: ctrl+shift+L to select all the instances of that match my current selection.

I usually hit home or end to position my cursor consistently given that the lines are usually different lengths.

From here it’s usually just a matter arrow key usage and shift + home or end to select the text I want.

Some tips:

Whats really neat is the ability to cut/copy all the lines that have been selected using the multi-cursor, then move the arrow key to a new line to paste those lines within a different section of your file. This is sort of like having a separate clipboard for each item in your multi-selection.

The Solution:

The solution to this particular conundrum came from, surprise surprise: StackOverflow and came in the form of our beloved/hated friend Mr. RegEx (or Regular Expression for long). This regular expression isn’t too hairy and a kind user posted a helpful explanation of the expression. (references at the bottom of the page).

Basically what the following steps do is, Open The Find & Replace Dialog, use alt + r to toggle the regular expression option. Next we enter our regular expression. Now this will vary depending on what you need todo.

For instance, if you just need to remove blank lines that have no white space, you would put \n\n in the find input box then put \n in the replace input box. This will essentially remove instances of 2 consecutive blank lines and replace them with a single blank line.

If however, your situation is like mine was & you have some whitespace within those blank lines, you’ll need to use the following RegEx: ^\s*$\n in the find input box and leave the replace input box empty or put a \n to convert all the instances to a single empty line.

The regular expression starts with the ^ character which indicates the beginning string anchor, followed by: \s, which looks for any white-space character with any number of repetitions (*). The string anchor is completed with the $ end of string anchor. Then we finish the RegEx pattern by looking for a new line (\n) after the white space match.

I’ve included a snippet below from the user comment that broke the expression down into a small table. These regular expressions can be a little tricky to grasp at first and I’m offering multiple styles of describing essentially the same thing as a way to anyone that may just need a different perspective to make everything click.

The following steps are what I followed to remove multiple empty lines within the file.


  1. Open Find & Replace Dialog with key shortcut: ctrl + h
  2. Toggle Regular Expression option key shortcut: alt + r
  3. Enter the following RegEx: ^\s*$\n
  4. Click the ‘Replace’ text box to bring focus to it.
  5. Either leave the Replace field empty, put a single newline \n or put whatever your heart desires 🙂
  6. Press enter (with focus still in replace input box) to replace the first match
  7. Click the Replace Icon (ctrl + shift + 1) to replace matches 1 by 1 or the Replace All Icon (ctrl + alt + Enter) to replace all empty lines.

Ur Done, Have a Nice Day 🙂


I found this tip at StackOverflow of course and can be found here: Visual Studio Code – delete all blank lines – regex. I used the 2nd post as I thought it was more adaptable (IMO).

What also works is this regex pattern:


Then CTRL+Enter to replace all lines.

Explanation of the above pattern:

|  ^ | beginning of string anchor             |
| \s | any whitespace character               |
| '*'| zero or more repetitions               |
|  $ | end of string anchor                   |
| \n | new line                               |

Thanks for checking out our site. If you have any comments, questions, recommendations or critiques, let us know in the comments. Thanks, and have a great & productive day. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *