Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

AutoHotkey: remove two or more extra blank lines which start with blank space

Hello experts,

The following procedure allows me to remove extra blank lines.

^!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,"\R\R\K\R+") ; replace two or more consecutive blank lines with a single blank line
  sleep 100
  ; Joe: see if last two chars are CR and/or LF
  LastChar:=SubStr(Clipboard,0,1)
  If ((LastChar="`r") or (LastChar="`n"))
    StringTrimRight,Clipboard,Clipboard,1
  If ((LastChar="`r") or (LastChar="`n"))
    StringTrimRight,Clipboard,Clipboard,1
  
  Send, ^v
  Return

Open in new window


I noticed that removal is not performed when blank lines starts with a space.

Example of block text:

text


 
text with extra blank lines above.

Could you please help me to adjust the AutoHotkey to be able to remove extra blank lines even if they start with a space?

Thank you for your help.
ASKER CERTIFIED SOLUTION
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Luis Diaz

ASKER

Thank you very much for this proposal Joe.
Unfortunately I tested here with Notepad++ in W10 and it doesn't work.
I made sure to test it in a single AutoHotkey file.
I send you attach the file used in my test.
remove-empty-lines.txt
Works perfectly here, but keep in mind that it retains one of the blank lines in a sequence of multiple blank lines...and the blank line that it retains may or may not be one with spaces and/or tabs. Anyway, attached is a short video showing the AutoHotkey script as posted above working fine on the text file that you posted...but, as noted above, the one blank line that it retains is one with a space at the beginning. Regards, Joe
luis-remove-extra-blank-lines.mp4
Joe,

It works indeed. I was confused as in the previous script it finish with a ctrl + v. Now I understand that it keep in the clipboard.
Sorry for that.

Thank you for your help.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much Joe.
As always, Luis, a pleasure to help you. Regards, Joe