Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
870 views
in Technique[技术] by (71.8m points)

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

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.

question from:https://stackoverflow.com/questions/41489818/in-sublime-text-3-is-there-a-shortcut-to-remove-blank-lines-in-a-selected-text

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 ^ , 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 ^ searches for the beginning of a line (^) immediately followed by a newline character ( ). If you suspect that some of your "blank" lines contain whitespace, like space or tab characters, you can use ^s* 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*.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...