Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm looking for a tool that allows me to easily re-wrap text, i.e. a tool that removes line breaks, but not empty lines, from a text selection or text field, and that works on Chrome and on Windows.

Bonus points for anything that works outside of the browser too, and that works in-place (i.e. that doesn't require copy-pasting the text through a separate window or using something like http://www.textfixer.com/tools/remove-line-breaks.php)

Browser extensions, GreaseMonkey scripts or applications that also work on Linux and/or Mac (or even better, that are multi-platform) are all welcomed.

Here is an example of how the tool should behave. If I have the following in a text field:

This is a test for SuperUser.com. This is 
a test for SuperUser.com. This is
a test for
SuperUser.com. This is a test 
for SuperUser.com 

This is a test for SuperUser.com. This is 
a test for SuperUser.com. This 
is
a 
test
for SuperUser.com. This is a test for SuperUser.com

then, using a (e.g. keyboard) shortcut, the tool should convert it to:

This is a test for SuperUser.com. This is a test for SuperUser.com. This a test for SuperUser.com. This is a test for SuperUser.com

This is a test for SuperUser.com. This is a test for SuperUser.com. This a test for SuperUser.com. This is a test for SuperUser.com

Thanks in advance!

share|improve this question
    
Just to be clear, you want that functionality within Google Chrome? (since you mention "applications") –  Daniel Beck Jan 11 '11 at 16:11
    
At the very least, anything that works on Chrome on Windows would work for me. But it would be great if we could also find something that works outside the browser and that is cross-platform. –  user815423426 Jan 11 '11 at 16:14
    
Well this may not meat your exact specifications so I'll post it as a comment. Anything that that has regular expressions would be able to do this with find replace like notepad++(notepad-plus-plus.org). Switch replace mode to regular expressions then replace /n with nothing. –  Jeff F. Jan 11 '11 at 16:14
    
What is the difference between a line break and a paragraph break? Is it just '<text>\n<text>' compared to '<text>\n\n<text>' ? –  Puddingfox Jan 11 '11 at 16:17
    
@Puddingfox, It probably depends of how the text is encoded and rendered (e.g. in HTML it might mean <p></p> or <br></br>), but ideally the tool should work regardless of how the line breaks or paragraphs are encoded (i.e. it should respect the underlying text markup language). After all, I am asking for a WYSIWYG (What You See Is What You Get) solution. –  user815423426 Jan 11 '11 at 16:25

3 Answers 3

Not browser, not cross-platform, but might be useful for someone anyway:

TextMate for Mac OS X has the command Edit » Unwrap Paragraph/Selection (Ctrl-Opt-Q) for exactly this purpose.


Emacs has M-q for this purpose, you might have to assign a greater width than default.


Use the following Python script (written on OS X, but otherwise cross-platform):

#!/usr/bin/env python
import sys

current = "\n"
for line in sys.stdin:
    if line != "\n":
        sys.stdout.write(current[:-1])
    else:
        print "\n"
    current = line
print current[:-1]

Invoke like this:

cat test.txt | ./rewrap.py

On Mac OS X, create a Service using Automator in any application that receives text as input and replaces selected text. Add a Run Shell Script action that passes input to stdin and call the Python script above. Assign a global keyboard shortcut for any application in System Preferences » Keyboard.

alt text

share|improve this answer
    
These solutions are great, Daniel, thank you!. However, I think I'm still looking for something else. The python idea is interesting, but I am not sure if or how I could use that as an in-place solution when editing a text field in my browser. Also, I use Emacs when coding, so thanks for the tip, but I need something that works on the browser. –  user815423426 Jan 11 '11 at 16:38
    
@AmV then see my most recent edit. At least on OS X, you get the Python script in any application, thanks to Services and Automator. –  Daniel Beck Jan 11 '11 at 16:39
    
Thanks @Daniel. They look like a great fit for OS X, and I would vote you up if I had enough reputation to do it (I just signed up). Unfortunately I need something that works under Windows as well. –  user815423426 Jan 11 '11 at 16:41
    
@AmV You should really include this requirement in your question. As it is, it didn't seem to matter on which system, as long as it's in all applications. Well, at least I got a global rewrap/unwrap command out of it ;) –  Daniel Beck Jan 11 '11 at 16:45
    
Good point. I added the "at least in my browser (Chrome) and on Windows" requirement at the beginning. –  user815423426 Jan 11 '11 at 16:51

(G)Vim can do this. E.g., put this in your .vimrc:

vnoremap <F5> :s/\s*\n\s*\n\s*/<<mybreak>>/g<CR>gvJgv:s/<<mybreak>>/\r\r/g<CR>

This assigns the key F5 (you can use whatever you want instead; with vim you can customize all the keymappings however you want) to do all the following: while in "visual mode" (i.e., when a block of text is selected), replace all linebreaks in the selected text followed by a line with nothing but whitespace followed by another linebreak with the placeholder <<mybreak>>, rehighlight the same portion and join all the lines, reducing all whitespace between lines to a single space,, rehighlight the same portion and replace the placeholder <<mybreak>> with two newlines to restore the paragraph breaks.

Of course, you don't need to remember anything but F5.

Actually, there's probably a simpler way to do this in vim. My vim skills are nothing compared to some; this is just how it occurred to me to do it.

Can you use it in a browser: perhaps not directly, but with Firefox plugins such as vimperator or pentadactyl allow you to edit all webfields in vim (or any other text editor of your chosing), so then you'd be able to use it there too.

share|improve this answer

Check out Clipboard Fusion by Binary Fortress Software. I have it set so when ever I copy text from certain applications (Adobe Reader), copied text is automatically stripped of line breaks. Very useful.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.