AutoHotkey: Add specific character at the beginning of each line

Luis Diaz
Luis Diaz used Ask the Experts™
on
Hello experts,

The following script allows me to add a specific character at the beginning and end of a selected text:

;====================================
;EE Code
;====================================

!^c:: ;code
Send, ^c
Sleep, 50
ClipWait,1
If (ErrorLevel=1)
{
  MsgBox,4144,Error, No text appearing after one second
  Return
}
ClipboardVar:=Clipboard
Clipboard:="[code(string))]" . ClipboardVar . "[/code(string)]"
Sleep, 50
MsgBox,4160,Success,Your clipboard has been properly copied., 2
Return

Open in new window


I would like to take it as a reference to add specific character (-) at the beginning of the various lines of a selected text.

Example of a selected text:
Line 1
Line 2
Line 3
Line 4

Result:
-Line 1
-Line 2
-Line 3
-Line 4

If you have questions, please contact me.

Thank you for your help.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Developer
Fellow 2017
Most Valuable Expert 2018
Commented:
Hi Luis,
Your posted code displays wrong because the EE comment box interpreted the [/code] in your AHK code as a BBCode for the comment, so it closed out your code block.

Anyway, this modification of the AutoHotkey code that you posted does what you want:

!^c:: ;code
Send, ^c
Sleep, 50
ClipWait,1
If (ErrorLevel=1)
{
  MsgBox,4144,Error, No text appearing after one second
  Return
}
ClipboardVar:=Clipboard
UpdatedLines:=""
Loop,Parse,ClipboardVar,`n,`r
  UpdatedLines:=UpdatedLines . "-" . A_LoopField . "`n"
Clipboard:=UpdatedLines
Sleep, 50
MsgBox,4160,Success,Your clipboard has been properly copied., 2
Return

Open in new window

That leaves a newline (`n) after the last line...remove it if you don't want it. Regards, Joe
Luis DiazIT consultant

Author

Commented:
Thank you very much Joe,
I modify the question to have a proper code comment.
I will test your proposal and keep you informed.
Luis DiazIT consultant

Author

Commented:
Joe,

I tested and it works!
Thank you very much for your help!
Joe WinogradDeveloper
Fellow 2017
Most Valuable Expert 2018

Commented:
You're welcome, Luis, and thanks to you for letting me know that it works. Thanks, too, for writing the very nice Testimonial...I really appreciate it! Regards, Joe

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial