Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

In a text document, how do I join several lines into a single line? I want each paragraph to be on a single line, but keep the lines between the paragraphs.

Here is an example. I want to convert this text

Lorem ipsum dolor sit amet, consectetuer
adipiscing elit. Ut purus elit, vestibulum ut, placerat ac,
adipiscing vitae, felis.

Curabitur dictum gravida mauris. Nam arcu
libero, nonummy eget, consectetuer id, vulputate a, magna. Donec
vehicula augue eu neque.

to this text

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis.

Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula augue eu neque.

I know how to do it in vim or fmt, but can I do it in TextEdit and in the builtin text boxes? I don't want to manually have to do ^D<space> at the end of each line.

share|improve this question
    
I have to ask: why don't you just make such editing changes in vim and then do the rest of the editing (e.g., formatting) in Text Edit? –  soxman Oct 13 '12 at 15:20
    
I will simply go with a C++ application fast. LOL –  Shane Hsu Mar 12 '13 at 8:02
add comment

4 Answers

If your paragraphs are separated by a blank line, you can do this with a two-part Find and Replace in TextEdit.

  1. Open the Find and Replace dialog (⌥⌘F).
  2. In the Find field, insert the newline character by typing option-return.
  3. In the Replace field, insert a space, then click All. This replaces all newlines with a space, so you'll have one long line of all your text.
  4. Change the Find field to two spaces.
  5. In the Replace field, enter two newline characters (option-return twice).
  6. Click All to move paragraphs to a new line.
share|improve this answer
    
Thanks, good to know. It almost does what I want, which is to turn paragraphs separated by empty lines into single lines. –  lhf Oct 13 '12 at 18:24
    
Maybe you could clarify this in your question a bit, perhaps post a sample and the result you expect. You want each paragraph to be on a single line, but keep the lines in between the paragraphs? –  robmathers Oct 13 '12 at 18:30
    
Exactly. I've updated my question as you suggest. –  lhf Oct 13 '12 at 19:33
    
Updated it with the process to work the way you're looking for. –  robmathers Oct 13 '12 at 19:43
add comment

I find this works best in three steps, particularly if there's some character or combination of characters not used in your document. Let's assume for a moment that •%• doesn't happen to come up in your document. If it does, pick a different arbitrary short string.

  • Use the Find and Replace dialog to replace all double newlines (return return) with your arbitrary text string; replace all.

  • Find and Replace all newlines (return) with a space (space).

  • Find and Replace all instances of your arbitrary text string with a double newline.

share|improve this answer
add comment

You could create a service like this:

With AppleScript you'd have to use the clipboard:

try
    set old to the clipboard as record
end try
try
    tell application "System Events" to keystroke "c" using command down
    delay 0.1
    do shell script "export LC_CTYPE=UTF-8; pbpaste | fmt -w 99999 | pbcopy"
    tell application "System Events" to keystroke "v" using command down
    delay 0.1
end try
try
    set the clipboard to old
end try

I have also written a script like TextMate's filter through command (run shell script.scpt at http://lri.me/applescript.html).

share|improve this answer
add comment

Just run this workflow. This worked for me:

Ask for Finder items

Open Finder Items (using TextEdit)

Wait for Finder Items

Get Contents of Text Edit Document

Remove Empty Paragraphs

Get Paragraphs of Text

Combine Text String (Select Insert Delimiter Between Combined strings, then put one space in that field)

New Text File

That should work. Let me know if it does.

share|improve this answer
add comment

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.