Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime Text 2: How to delete blank/empty lines

Tags:

sublimetext2

People also ask

How do I remove blank lines in Sublime text?

Press: Ctrl + H on PC, or. Command + Alt + F on Mac or.

How do I get rid of empty lines?

There is also a very handy keyboard shortcut to delete rows (columns or cells). Press Ctrl + – on the keyboard. That's it! Our blank rows are gone now.

How the blank lines are deleted from the file?

Delete blank lines using the grep command When used with the -v option, the grep command helps to remove blank lines. Below is a sample text file, sample. txt, with alternative non-empty and empty lines. To remove or delete all the empty lines in the sample text file, use the grep command as shown.


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


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


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

  • Delete Blank Lines (also available via Package Control)
  • Trailing Spaces (also available via Package Control)

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


A Find/Replace solution:

Regex Find:\s+

Replace with: //single space


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}


I had to use:

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

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


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