310

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?

Nick
  • 8,049
  • 18
  • 63
  • 107

19 Answers19

738

Select the text

Press:

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

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

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

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

Replace With: (nothing, leave in blank).

Community
  • 1
  • 1
Hugo Corrá
  • 14,546
  • 3
  • 27
  • 39
  • 3
    That works, thank you! Is there a way to bind that find/replace to a keyboard shortcut or somehow save it for quick access? I googled it and couldn't find anything – Nick Aug 17 '12 at 16:43
  • 49
    Small suggestion use `^(\r|\n\r?)` to cover all possible line breaks. – Haris Krajina Nov 07 '12 at 09:22
  • 5
    For me (Sublime Text 2) shortcut is (Alt + Cmd + F) then as describe by Hugp Corra – Maxence Jul 03 '13 at 14:41
  • On OS X, regex is enabled with Command-Alt-R (while the Find window is open) – Jedidja Feb 03 '14 at 15:50
  • 28
    A better way to find all empty lines (including ones with whitespace) would be: `^[\s]*?[\n\r]+` – Crates Aug 29 '14 at 16:47
  • 5
    @Crates you don't need the `?`, as a `*` also matches zero occurences and `\s*` will match the extra '\r' when for example editing windows text in a linux environment, so `^\s*$` does the trick. – drevicko Nov 01 '14 at 09:41
  • And small last step: press Ctrl + Alt + Enter to replace them all – avj Aug 26 '15 at 10:53
  • Also you can do it on multiple files and folder also. visit this link http://docs.sublimetext.info/en/latest/search_and_replace/search_and_replace_files.html – cijagani Dec 27 '15 at 07:33
  • This continues to work properly in Sublime Text 3. :-) – Corey S. Jun 13 '18 at 13:52
72

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+$

signalpillar
  • 916
  • 7
  • 3
  • 38
    That assumes that there IS whitespace. Also, it won't work for actually substituting the lines with nothing. Try `^[\s]*?[\n\r]+` instead. I've tested that, and it works. – Crates Aug 29 '14 at 16:49
  • Crates's solution should be the best answer. However, `^[\s]*[\n\r]+` should be enough. – emeraldhieu Mar 06 '23 at 05:08
25

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

V-Light
  • 3,065
  • 3
  • 25
  • 31
12

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}

abs
  • 129
  • 1
  • 2
10

A Find/Replace solution:

Regex Find:\s+

Replace with: //single space

UserBSS1
  • 2,091
  • 1
  • 28
  • 31
8

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.

Dihia LANASRI
  • 133
  • 2
  • 6
  • 2
    This will delete ALL non-unique lines. Not just blanks. So, say you have HTML with a lot of lines being a single closing , it will delete them all except the first. Or in code, it will delete all your closing }. This is not reliable to delete just blank lines, understand what it does. – Ivan McA Jan 08 '20 at 12:25
  • This may not exact match the origin question, but this is the only native and easiest solution when a) You're sure there are no other duplicated lines. b) You're not fan of regexp. – Dai Mar 09 '22 at 01:39
  • Also this leaves one empty line always, as it is always unique within dataset – Suncatcher Jul 12 '22 at 12:34
7

I had to use:

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

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

Bruce Edge
  • 1,975
  • 1
  • 23
  • 31
  • 2
    Make sure you select the area you want it to effect and then use the respective shortcut keys **Windows**: _Ctrl+Alt+Backspace_ (Delete Blank Lines) _Ctrl+Alt+Shift+Backspace_ (Delete Surplus Blank Lines) **OSX**: _Ctrl+Alt+Delete_ (Delete Blank Lines) _Ctrl+Alt+Shift+Delete_ (Delete Blank Lines) **Linux**: _Ctrl+Alt+Backspace_ (Delete Blank Lines) _Ctrl+Alt+Shift+Backspace_ (Delete Surplus Blank Lines) – Xtremefaith Oct 23 '14 at 19:13
6

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

Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
Samuel Wahome
  • 61
  • 1
  • 1
6

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

Community
  • 1
  • 1
rsc05
  • 3,626
  • 2
  • 36
  • 57
4

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...

Brian Joseph Spinos
  • 2,144
  • 2
  • 18
  • 18
3

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.

Volker E.
  • 5,911
  • 11
  • 47
  • 64
deltab
  • 2,498
  • 23
  • 28
2

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

Yogesh Mistry
  • 2,082
  • 15
  • 19
2

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

Dick
  • 49
  • 3
1

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.

Donncha
  • 64
  • 1
  • 7
  • 2
    i see where you're going with this though.. this suggestion results in dragging all content into one line, but a macro would make this work: (OSX keys) `ctrl-q` (record macro), `cmd-j` (join lines), `down arrow`, `ctrl-q` (end macro). Then `shift-ctrl-q` to repeat as often as required... – ptim Feb 13 '15 at 10:00
1

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

herve
  • 3,825
  • 2
  • 18
  • 27
1

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

Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
Naeem Ijaz
  • 805
  • 9
  • 17
0

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.

Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
0

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
MetalGodwin
  • 3,784
  • 2
  • 17
  • 14
-1

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

McCygnus
  • 4,187
  • 6
  • 34
  • 30
Marshall Z
  • 21
  • 3