Remove Empty Lines & Extra Spaces from Document Easily | Simple Trick

preview_player
Показать описание
You write clean code, but your buddies and colleagues may not. I also often have to work with code that is not formatted properly. We have a CSS file. Code is all over the place. Lots of spaces, empty lines, messy code. How can we format this code? How can we remove all empty extra lines from the code?

First of all use Visual Studio Code. Install an extension: Prettier and enable it. Make it default code formatter if it asks. Or see its settings from settings and then prettier options. Select all code from document and use option 'Format Document'. This should format the code. But empty lines may not be removed yet. You can also go to user settings for visual studio code and add a single line there that auto format code on document save. It will auto format document when you save it.

Now save document and code will be much better but there are still empty lines. To fix it, use regular expression search and replace. Use CTRL + F to bring up option to search and replace from visual studio code editor.
Use:

^\s*$\n

^ Caret matches the beginning of the line
\s This matches any white space character
* This is quantity operator for \s it match zero or more
$ Dollar sign matches end of the line
\n and this one matches new line character

In short Remove lines that consist of
- Space character
- Tab character
- Carriage return character
- New line character
- Vertical tab character

To only remove blank lines use:

^$\n

Now the document is 100% formatted and clean. And you can do it not just with CSS, but with other code as well, if you need to. So this is how you can clean CSS code, format CSS code and remove empty lines from code.

Using this technique you can do the following:
- Remove blank lines from code
- Find and replace with a newline in Visual Studio Code
- Delete all blank lines using regex in Visual Studio Code
- Remove empty lines in text using Visual Studio Code
- Search and Replace all in Code Editor

You can also use Visual Studio Code Editor extension named as 'Remove empty lines' to remove empty lines from code.

Thank You!
👍 LIKE VIDEO
👊 SUBSCRIBE
🔔 PRESS BELL ICON
✍️ COMMENT

#WebStylePress #VSCode #CleanCode #WebDevelopment #trick #tips #format
Рекомендации по теме
Комментарии
Автор

thank you for info - works great ( my prettier settings are now at 100% )

gabriel.d
Автор

you can use extention to remove empty lines

CoderDmitri
Автор

Thanks man you're a live saver!!
I was asking ChatGPT for that and it kept telling me to enter ^\s*$ instead of ^(\s)*$\n 😅

WAVY.Y