Sublime Text 2: How to delete blank/empty lines

Sublimetext2

Sublimetext2 Problem Overview


Let's say I had a text file with the following nine lines:

foo

bar

baz

qux

quux

How can I use Sublime Text 2 to remove all four of the blank/empty lines, leaving only five lines?

Sublimetext2 Solutions


Solution 1 - Sublimetext2

Select the text

Press:

  • Ctrl + H on PC, or
  • Command + Alt + F on Mac or
  • Click Find->Replace.

Make sure you have selected 'regular expression' by pressing:

  • Alt + R on PC or
  • Command + Alt + R on Mac or
  • Click .* in the Find box.

Find what: ^\n or ^(\r|\n\r?)

Replace With: (nothing, leave in blank).

Solution 2 - Sublimetext2

The regexp in Hugo's answer is correct when there is no spaces in the line. In case if there are space regexp can be ^\s+$

Solution 3 - Sublimetext2

There are also some ST2/ST3 Plugins for such tasks. I do like these two:

The first one has two methods for removing empty/unnecessary lines. One of them called Delete Surplus Blank Lines which is cool. It removes only those lines that are followed by another empty line

Solution 4 - Sublimetext2

A Find/Replace solution:

Regex Find:\s+

Replace with: //single space

Solution 5 - Sublimetext2

Don't even know how this whole thing works, but I tried ^\s*$ and didn't work (leaving still some empty lines).

This instead ^\s* works for me

{sublime text 3}

Solution 6 - Sublimetext2

I had to use:

replace \n^\s*\n with \n

The https://github.com/NicholasBuse/sublime_DeleteBlankLines plugin did nothing at all.

Solution 7 - Sublimetext2

Their is a more easily way to do that without regex. you have just to select the whole text. then go to: Edit--> Permute Lines --> Unique.

That's all. and all blank lines will be deleted.

Solution 8 - Sublimetext2

Simpler than I thought. Ctrl + A Followed by Ctrl + H Then Select Regular Expression .* . Replace \n\n with \n. Voila!

Solution 9 - Sublimetext2

Sublime Text 2 & 3

The Comments of @crates work for me,

Step 1: Simply press on ctrl+H

Step 2: press on RegEX key

Step 3: write this in the Find: ^[\s]*?[\n\r]+

Step 4: replace all

Solution 10 - Sublimetext2

Using multiple selections: select one pair of line breaks, then use Quick Find All (Alt+F3), or Quick Add Next (Ctrl+D) repeatedly, to select them all; then hit Enter to replace them with single line breaks.

Solution 11 - Sublimetext2

You are looking for this:

^\n|^\s+$

it will not delete the line, if there is content with white space or tabs in front>

e.g.:

these will not be deleted: ...space... abc

...tab... abc

this will:

...space... ...nothing else...

...tab... ...nothing else...

Solution 12 - Sublimetext2

Another way, you can use the command line cc.dbl of ConyEdit (a plugin) to delete blank lines or empty lines.

Solution 13 - Sublimetext2

There's also "Join lines". If on OSX, select all your text, and press CMD-J a few times, and it will collapse your selection by line, removing the line breaks.

Edit: This approach will leave you with everything on one line, which is not what you asked for.

Solution 14 - Sublimetext2

For those who are curious of sublime text editor, the unofficial-documentation may be interesting!

Solution 15 - Sublimetext2

There is a wonderful package (for Sublime 2 & 3) called 'Trimmer' which deletes empty lines. It also does many other useful things.

Refer this: https://packagecontrol.io/packages/Trimmer

Solution 16 - Sublimetext2

To find extra spaces and blank lines press Ctrl+Shift+F Select Regular Expressions Find

[\n\r]{2,}

and then replace with

\n

to remove all kind of spaces in sublime and dreamviewr

Solution 17 - Sublimetext2

Using find / replace, try pasting a selection starting at the end of the line above the blank line and ends at the beginning of the line after the blank. This works for a single blank line. You can repeat the process for multiple blank lines as well. CTRL-H, put your selection in the find box and put a single newline in the replace box via copy/paste or other method.

Solution 18 - Sublimetext2

In my case some empty lines contained the unicode character zero width space (U+200b). To rid empty lines, including the ones with this unicode character:

\s\x{200b}|^\s

Solution 19 - Sublimetext2

If ^\n does not work properly ===> try .*[^\w]\n

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNickView Question on Stackoverflow
Solution 1 - Sublimetext2Hugo CorráView Answer on Stackoverflow
Solution 2 - Sublimetext2signalpillarView Answer on Stackoverflow
Solution 3 - Sublimetext2V-LightView Answer on Stackoverflow
Solution 4 - Sublimetext2UserBSS1View Answer on Stackoverflow
Solution 5 - Sublimetext2absView Answer on Stackoverflow
Solution 6 - Sublimetext2Bruce EdgeView Answer on Stackoverflow
Solution 7 - Sublimetext2Dihia LANASRIView Answer on Stackoverflow
Solution 8 - Sublimetext2Samuel WahomeView Answer on Stackoverflow
Solution 9 - Sublimetext2AnonymousView Answer on Stackoverflow
Solution 10 - Sublimetext2deltabView Answer on Stackoverflow
Solution 11 - Sublimetext2Brian Joseph SpinosView Answer on Stackoverflow
Solution 12 - Sublimetext2DickView Answer on Stackoverflow
Solution 13 - Sublimetext2DonnchaView Answer on Stackoverflow
Solution 14 - Sublimetext2herveView Answer on Stackoverflow
Solution 15 - Sublimetext2Yogesh MistryView Answer on Stackoverflow
Solution 16 - Sublimetext2Naeem IjazView Answer on Stackoverflow
Solution 17 - Sublimetext2Clayton LoudonView Answer on Stackoverflow
Solution 18 - Sublimetext2MetalGodwinView Answer on Stackoverflow
Solution 19 - Sublimetext2Marshall ZView Answer on Stackoverflow