Reputation: 1139
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
Reputation: 9663
Follow the steps
^$\n
NB: leave the replace box as blank
Upvotes: 252
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
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
Upvotes: 1
Reputation: 932
You can use regular expressions.
^(\s)*$\n
I referred to this link. It worked for me
Upvotes: 57
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
Reputation: 745
Here are step-by-step instructions to remove those empty lines:
^(\s)*$\n
Upvotes: 3
Reputation: 6506
If you would like to remove lines that consist of only these characters:
Then use as a regular expression:
^\s*$\n
in the find and replace box Ctrl+H to open it.
Turn on regular expression mode Alt+R
Replace all occurences with Ctrl+Alt+Enter or by clicking:
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 characterUpvotes: 11
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
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
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.
select the empty space resulting in a desired pattern. Example of selection
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.
Upvotes: 2