How to remove empty lines in Visual Studio Code

For some reason every now and then I get every other line empty when opening a file with code in Visual Studio Code. Annoying. It looks like this (made up example):

How to remove empty lines in Visual Studio Code

Here is how to get rid of these empty lines:

  1. From Edit Menu, select Replace or press command + Option + F on Mac or Ctrl + H on Windows
  2. In the find box type \n\n
  3. In the replace box type \n
  4. Select the ‘Use Regular Expression’ button (top red arrow)
  5. Click on the ‘Replace All’ button (bottom red arrow)

Here is a photo of the search/replace box:
search replace empty lines  code

If you are not sure then you can replace it one by one but that is not really necessary. If you see something is wrong then just press CTRL-Z on Windows and VS Code will revert back to code before replacement.

and this is how it should look like after you remove all empty lines:
vs code empty lines removed

Much nicer and as it should.