Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Sublime Text 3: is there a shortcut to remove blank lines in a selected text? (same for blanks in a line)

Tags:

sublimetext3

My question is very simple:

Is there a shortcut in Sublime Text 3 which allows to remove blank lines in a selected text? (same for blanks in a line)

For instance, how to make this text:

a

b

c

To become:

a
b
c

And this line:

I need to remove these blanks.

to become this line:

Ineedtoremovetheseblanks.

like image 638
JohnnyH Avatar asked Jan 05 '17 16:01

JohnnyH


People also ask

How remove extra lines sublime?

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.

How do you remove blank lines in a text?

Open TextPad and the file you want to edit. Click Search and then Replace. In the Replace window, in the Find what section, type ^\n (caret, backslash 'n') and leave the Replace with section blank, unless you want to replace a blank line with other text. Check the Regular Expression box.

How do you go to a specific line in Sublime Text?

In Sublime Text, you can quickly jump to any line in the code. Hit Ctrl–G (Mac and Windows). Type in a line number and hit Return/Enter to go to that line.


3 Answers

You don't need a plugin to do this, a simple regex search and replace will do. First, select the text where you'd like to delete the blank lines. Then, select Find → Replace… (or, hit CtrlH on Windows/Linux, F on OS X). Make sure the "Regular Expression" and "In selection" buttons are selected:

find/replace dialog

In Find What:, enter ^\n, and make sure the Replace With: field is empty. Then, simply hit "Replace All" and this:

before replace

becomes this:

after replace

As a bit of explanation, the regular expression ^\n searches for the beginning of a line (^) immediately followed by a newline character (\n). If you suspect that some of your "blank" lines contain whitespace, like space or tab characters, you can use ^\s*\n instead - \s* matches 0 or more whitespace characters, including newline characters.

For your second example, use the same Find/Replace settings as above, except your regular expression should simply be \s*.

like image 91
MattDMo Avatar answered Oct 14 '22 05:10

MattDMo


There is a package called Trimmer. You can install it via Package control.

After you got the package you can use its functionalities. Just highlight the text you want to change (or don't select anything if you want to change the entire file) and then choose:

1) Edit > Line > Delete Empty Lines

2) Edit > Line > Remove Blank Spaces

Alternatively, as Chris's answer already pointed out you can use the classic search & replace functionality that is already present in the editor.

like image 27
Simone Avatar answered Oct 14 '22 04:10

Simone


AS my original answer was incorrect, I have found this package which will be suitable for your needs.

The package is called DeleteBlankLines, for Sublime Text 3, has the ability to delete blank lines for the entire document and within the selected text only, all from a key stroke just like you were after.

The package can be found here:

https://packagecontrol.io/packages/DeleteBlankLines

like image 20
Chris Avatar answered Oct 14 '22 06:10

Chris