Remove line breaks while copying (Convert multiple lines to one line)

Post your working scripts, libraries and tools for AHK v1.1 and older
HalBenHB
Posts: 1
Joined: 31 Jan 2023, 10:30

Remove line breaks while copying (Convert multiple lines to one line)

Post by HalBenHB » 31 Jan 2023, 11:18

I work with pdfs a lot. When copying text from pdf it also copies line breaks in pdf. I usually solve this by opening a new Chrome tab, pasting the text to address bar (it automatically converts it to one line text), then copying it again. However, I wanted to increase my productivity. So, the following code automatically removes line breaks of selected text and copy it as a one line text when you press Ctrl+C.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.


^c::
Suspend, On ; Suspend this scripts so the following ^c becomes a normal copy
Clipboard =
Send, ^c
Sleep, 50 ; Wait for clipboard
Clipboard := RegExReplace(Clipboard, "\r\n|\r|\n", "")
Suspend, Off
return


mattersen
Posts: 1
Joined: 19 Mar 2024, 11:36

Re: Remove line breaks while copying (Convert multiple lines to one line)

Post by mattersen » 19 Mar 2024, 11:49

I was using a similar script to remove line breaks when copy/pasting but your script is working much better mine does, so, thank you!

I did make a small tweak because I was getting some cases where there was a missing space between words but this can be solved by substituting the "" with A_Space, which automatically trims out extra spacing (if I'm reading the documentation correctly). https://www.autohotkey.com/docs/v1/lib/AutoTrim.htm

Code: Select all

#NoEnv 
; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.   

^c:: Suspend, On ; Suspend this scripts so the following ^c becomes a normal copy 
Clipboard = 
Send, ^c 
Sleep, 50 ; Wait for clipboard 
Clipboard := RegExReplace(Clipboard, "\r\n|\r|\n", A_Space) 
Suspend, Off 
return  
In any case, this saved me a lot of time and I really appreciate it! :)

Post Reply

Return to “Scripts and Functions (v1)”