Luis Diaz
asked on
AutoHotkey: remove blank lines
Hello experts,
The following AutoHotkey scripts allows me to remove extra lines and extra spaces:
I would like to take as a reference to have a third AutoHotkey script to remove all blank lines from a selection.
Example of text entry:
text
text
text
text
text
After I launch the script (#!Space AutoHotkey combination) I expect to have the following:
text
text
text
text
text
If you have questions, please contact me.
Thank you for your help.
The following AutoHotkey scripts allows me to remove extra lines and extra spaces:
;====================================
;Remove two consecutive extra lines
;====================================
^!Space::
Send ^c
ClipWait,1
If (ErrorLevel=1)
{
MsgBox,4144,Error,No text appeared on clipboard after initial Ctrl+c and waiting for one second
Return
}
PreviousLineBlank:=False
NewClipboard:=""
Loop,Parse,Clipboard,`n,`r
{
CurrentLine:=A_LoopField
CurrentLineWithoutBlanks:=StrReplace(CurrentLine,A_Space) ; remove all spaces
CurrentLineWithoutBlanks:=StrReplace(CurrentLineWithoutBlanks,A_Tab) ; remove all tabs
If (CurrentLineWithoutBlanks="") ; see if current line is blank
{
If (PreviousLineBlank) ; current line is blank - see if previous one is
Continue ; a consecutive blank line, so do not include it
Else
{
NewClipboard:=NewClipboard . CurrentLine . "`n" ; not a consecutive blank line, so include it
PreviousLineBlank:=True
}
}
Else
{
NewClipboard:=NewClipboard . CurrentLine . "`n" ; current line is not blank, so include it
PreviousLineBlank:=False
}
}
LastChar:=SubStr(NewClipboard,0,1) ; see if last char is CR or LF
If ((LastChar="`r") or (LastChar="`n"))
{
StringTrimRight,NewClipboard,NewClipboard,1 ; last char is CR or LF - remove it
LastChar:=SubStr(NewClipboard,0,1) ; see if new last char is CR or LF
If ((LastChar="`r") or (LastChar="`n")) ;
StringTrimRight,NewClipboard,NewClipboard,1 ; new last char is CR or LF - remove it
}
Clipboard:=NewClipboard
ClipWait,2
If (ErrorLevel=1)
MsgBox,4144,Error,No text appeared on clipboard with blank lines removed after waiting for two seconds`nThis should never happen
Else
Send ^v
MsgBox,4160,Success,Extra lines have been removed.,1
Return
;====================================
;Remove extra spaces
;====================================
+^Space::
; clipboard:="" ; Joe commented out - not needed because Ctrl+c replaces Clipboard
;Send, ^a ; Luis commented out because he prefers to select the block
Send, ^c
ClipWait,1 ; Joe added - good practice to do a ClipWait after Ctrl+c
If (ErrorLevel=1)
{
MsgBox,4144,Error,No text appeared on clipboard after waiting for one second
Return
}
Clipboard:=RegExReplace(Clipboard,"` +","` ") ; replace multiple spaces with one space
sleep 100
Send, ^v
MsgBox,4160,Success,Extra spaces have been removed.,1
Return
I would like to take as a reference to have a third AutoHotkey script to remove all blank lines from a selection.
Example of text entry:
text
text
text
text
text
After I launch the script (#!Space AutoHotkey combination) I expect to have the following:
text
text
text
text
text
If you have questions, please contact me.
Thank you for your help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> Tested in Word 365 and in EE!
Great news! Thanks for letting me know. Regards, Joe
Great news! Thanks for letting me know. Regards, Joe
ASKER
Regards,
Luis.