2

I have the following function in AutoHotkey that attaches the strings Left and Right as a prefix and suffix to a given text string that is pre-selected on a text file:

TagWrapper(Left, Right)
{
  ClipSaved := ClipboardAll
  Clipboard =
  send ^c
  Clipboard = %Left%%clipboard%%Right%
  send ^v
  Clipboard := ClipSaved
  ClipSaved = 
}

The function works by copying the text into the clipboard, and then adding the prefix Left and suffix Right to it.

Now, I would like to add line breaks between Left and clipboard and also between clipboard and Right so that the text is wrapped between two lines with contents in Left and Right. How can I do this?

I have tried adding

`n

or

`r

between the prefix and suffix and the clipboard, e.g. as follows :

TagWrapper(Left, Right)
{
  ClipSaved := ClipboardAll
  Clipboard =
  send ^c
  Clipboard = %Left%%`nclipboard`n%%Right%
  send ^v
  Clipboard := ClipSaved
  ClipSaved = 
}

but that didn't work (the script can't even run). Any thoughts?

1 Answer 1

3

You need to move the middle 2 % characters in the following, so that they are next to the clipboard. (You seem to have inserted the ```n`` in the middle of the %clipboard%)

  Clipboard = %Left%%`nclipboard`n%%Right%

i.e. this would be an improvement:

  Clipboard = %Left%`n%clipboard%`n%Right%

If this still doesn't work, then there is a specific example in the AutoHotKey Tutorial and Overview that talks about using ```r`n`` to add both carriage return and linefeed.

(Depending on which platform you're on, text files will have different line endings: see The Great Newline Schism)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .