Coder
Coder

Reputation: 1139

Visual Studio Code - Remove blank lines from code

I use Visual Studio Code as my preferred text editor and I have ran into a problem with regards to the formatting. I have copied code from one file and pasted into another, however when I have pasted the code VSC has placed a blank line between every line with code.

It is quite a big file so I am hoping there is a way to identify all the blank lines at once and remove them?

This is how I want the code to look:

<div>
  <h1></h1>
  <p></p>
</div>

however, on paste it now looks like this:

<div>

  <h1></h1>

  <p></p>

</div>

I have looked online but have been unable to find any direct answer to this :(

Upvotes: 81

Views: 135175

Answers (10)

Deepu Reghunath
Deepu Reghunath

Reputation: 9663

Follow the steps

  1. In windows, Press Ctrl+H (quick replace).
  2. Click "Use Regular Expressions".
  3. In Find specify ^$\n
  4. leave Replace as blank
  5. Click "Replace All" All Blank lines will be deleted.

enter image description here

NB: leave the replace box as blank

Upvotes: 252

Yohn
Yohn

Reputation: 2559

I got really tired of VSCode adding a new line after the opening and closing <head> and <body> tags, and finally found the setting to stop it.

{
  "html.format.contentUnformatted": "pre,code,textarea", // dont mess with the code in here
  "html.format.extraLiners": "", // having this as null put the unwanted space
  "html.format.maxPreserveNewLines": 0, // make 0 empty lines.
  "html.format.preserveNewLines": false, // make 0 empty lines.
  "html.format.indentInnerHtml": false, // I believe this made the body and head tag not indented
  "html.format.unformatted": null, // Does not format these tags - https://html.spec.whatwg.org/multipage/dom.html#phrasing-content
}

Upvotes: 0

Engr.Aftab Ufaq
Engr.Aftab Ufaq

Reputation: 6272

a very simple solution for this is on Windows press Crtl + F and then in the first box type \n\n and o replace box type \n and after this click on replace untill all the result become zero. dont forgot to select regular expression

check this image

Upvotes: 1

PaperinFlames
PaperinFlames

Reputation: 932

You can use regular expressions.

  1. Open your code in Visual Studio Code
  2. From Edit Menu, select Replace or use a short cut key (command + Option + F on Mac or Ctrl + H on Windows)
  3. In the find box type ^(\s)*$\n
  4. Leave the replace box empty
  5. Make sure the 'Use Regular Expression' is selected
  6. Select the 'Replace All' button

enter image description here

I referred to this link. It worked for me

Upvotes: 57

DragonFire
DragonFire

Reputation: 4082

Use the following plugin

Remove empty lines
v0.0.11
Alexander

Then in settings go and change

Remove-empty-lines: Allowed Number Of Empty Lines
Number of allowed consecutive empty lines.
Set to 1

On Mac

Command-Shift-P -> Remove empty lines : in Document

Upvotes: 19

Malik Zahid
Malik Zahid

Reputation: 745

Here are step-by-step instructions to remove those empty lines:

  1. Open your code in Visual Studio Code
  2. From Edit Menu, select Replace or use a short cut key (command + Option + F on Mac or Ctrl + H on Windows)
  3. In the find box type ^(\s)*$\n
  4. Leave the replace box empty
  5. Make sure the ‘Use Regular Expression’ is selected
  6. Select the ‘Replace All’ button

Upvotes: 3

Jimmix
Jimmix

Reputation: 6506

If you would like to remove lines that consist of only these characters:

  • A space character
  • A tab character
  • A carriage return character
  • A new line character
  • A vertical tab character
  • A form feed character

Then use as a regular expression:

^\s*$\n

in the find and replace box Ctrl+H to open it.

enter image description here

Turn on regular expression mode Alt+R enter image description here

Leave replace field empty: enter image description here

Replace all occurences with Ctrl+Alt+Enter or by clicking: enter image description here

The regex pattern ^\s*$\n does not use capture and group () because it is redundant.

  • ^ - matches the beginning of the line
  • \s - matches any white space character
  • * - quantity operator for \s = match zero or more
  • $ - matches end of the line
  • \n - matches new line character

Upvotes: 11

David Peel
David Peel

Reputation: 11

At the bottom right of the screen you should see LF or CRLF

If it is CRLF it will create the blank lines when you paste the code. Change it to LF and problem solved.

Upvotes: 1

talha
talha

Reputation: 53

With extession remove-empty-lines in vscode, you can process the entire document or only the part you choose.

https://marketplace.visualstudio.com/items?itemName=usernamehw.remove-empty-lines

Upvotes: 3

grmdgs
grmdgs

Reputation: 585

I found this question when searching for a solution. I did not want to use a regular expression and was able to delete empty lines in a file using a different method with multiple cursors.

  1. select the empty space resulting in a desired pattern. Example of selection

  2. Use the command "Select all Occurrences of Find Match:", cmd + shift + L

You will then have a cursor at each empty line and can delete them. Cursors on each empty line

Upvotes: 2

Related Questions