How to Join two lines?

53 views
Skip to first unread message

Andrew Ward

unread,
Oct 30, 2024, 1:34:10 PM10/30/24
to BBEdit Talk
Any suggestions on how to join two lines, deleting all trailing and leading whitespace replacing with a single space?

I've tried various script suggestions by chatGPT, Gemini(useless) and Perplexity (Almost works except does not remove leading whitespace from the line to be concatenated).   Here is the almost working .scpt

tell application "BBEdit"
    tell front text window
        if (count of characters of selection) is 0 then
            select line (get startLine of selection)
        end if
        set oldText to contents of selection
        log "Old text: " & oldText -- Debug output
        set newText to my joinLines(oldText)
        log "New text: " & newText -- Debug output
        set contents of selection to newText
    end tell
end tell

on joinLines(theText)
    set AppleScript's text item delimiters to {return, linefeed, character id 8233, character id 8232}
    set theLines to text items of theText
    if (count of theLines) > 1 then
        -- Trim trailing whitespace and add a single space to the first line
        set firstLine to do shell script "echo " & quoted form of (item 1 of theLines) & " | sed 's/[[:space:]]*$/ /'"
       
        -- Remove all leading whitespace from the second line
        set secondLine to do shell script "echo " & quoted form of (item 2 of theLines) & " | sed 's/^[[:space:]]*//'"
       
        -- Combine the modified first and second lines
        set combinedLines to firstLine & secondLine
       
        -- Add any remaining lines unchanged
        if (count of theLines) > 2 then
            set AppleScript's text item delimiters to return
            set remainingLines to (items 3 thru -1 of theLines) as text
            set combinedLines to combinedLines & return & remainingLines
        end if
       
        return combinedLines
    else
        return theText
    end if
end joinLines


Kaveh Bazargan

unread,
Oct 30, 2024, 1:38:55 PM10/30/24
to bbe...@googlegroups.com
So you want to select two lines and carry out this action on the selection, or do you have a big document with lots of lines to clean up? Can you give an example, before and after?

--
This is the BBEdit Talk public discussion group. If you have a feature request or believe that the application isn't working correctly, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Mastodon: <https://mastodon.social/@bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/bbedit/2b22def4-0b67-42c0-a2de-a45080b3f5a3n%40googlegroups.com.


--
Kaveh Bazargan PhD
Director
Accelerating the Communication of Research
  https://rivervalley.io/gigabyte-wins-the-alpsp-scholarly-publishing-innovation-award-using-river-valleys-publishing-technology/

James Reynolds

unread,
Oct 30, 2024, 1:39:38 PM10/30/24
to bbe...@googlegroups.com
search: ^\s*(.*[^\s])\s*$
replace: \1

search: (.*)\r(.*)\n
replace: \1 \2\n

James Reynolds
https://magnusviri.com

Andrew Ward

unread,
Oct 30, 2024, 2:36:31 PM10/30/24
to BBEdit Talk
Sorry... My bad for not giving context... I very frequently need to do this.  I need to eventually get this as a keyboard command just like
in Jetbrains Clion or PyCharm where you do a ctrl + shift + J to join lines.  

I don't want a regex to memorize and enter. I do not want to highlight any lines...Just place the cursor anywhere on a line and run the script or other solution to perform all the steps between that line and the following line

Reply all
Reply to author
Forward
0 new messages