Insert a TAB without losing previous CLIPBOARD

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
samzz
Posts: 35
Joined: 31 Jan 2014, 17:47

Insert a TAB without losing previous CLIPBOARD

Post by samzz » 19 Jan 2022, 06:23

Hello,
I often use EditPlus 5.5 to code and I turned on the indent option: Insert SPACES instead of TAB.

But sometimes I need to insert a TAB character, so I wrote this little AHK script to insert a TAB using the shortcut: WIN+TAB

It works fine, but I lose the previous string stored in the CLIPBOARD.
1. How to fix this script to insert a TAB character without losing the previous CLIPBOARD?
2. Do you know a way to insert a TAB character without using the CLIPBOARD?
3. Do you know a way to get/store the selected text without using the CLIPBOARD?

Code: Select all

$#tab::
 ;cb1:=clipboard
 clipboard:=" "
 clipwait,1
 send,^v
 ;clipboard:=cb1
return
PS:
There is a TAB character between the 2 single quotes.
Uncommenting the 2 commented lines does not solve my problem.

Thank you and regards
Attachments
20220119b.PNG
20220119b.PNG (7.47 KiB) Viewed 373 times
20220119a.PNG
20220119a.PNG (9.78 KiB) Viewed 373 times

Rohwedder
Posts: 7630
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Insert a TAB without losing previous CLIPBOARD

Post by Rohwedder » 19 Jan 2022, 06:31

Hallo,
try:

Code: Select all

$#tab::
 cb1:=clipboardAll
 clipboard:=" "
 clipwait,1
 send,^v
clipboard:=cb1
return
With your TAB character.
or perhaps:

Code: Select all

#tab::Send,% Chr(9)

samzz
Posts: 35
Joined: 31 Jan 2014, 17:47

Re: Insert a TAB without losing previous CLIPBOARD

Post by samzz » 19 Jan 2022, 12:23

The 1st solution does not work for me, using EditPlus 5.5. It inserts the last CLIPBOARD content.

The 2nd solution neither, it inserts always a space.

Thank you anyway and regards.

User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Insert a TAB without losing previous CLIPBOARD

Post by boiler » 19 Jan 2022, 12:39

samzz wrote: The 1st solution does not work for me, using EditPlus 5.5. It inserts the last CLIPBOARD content.
That is because the old clipboard contents are restored to the clipboard before Windows has had time to complete the paste process, so it pastes the old clipboard contents that just got re-assigned to it. This is a known issue. Try it like this:

Code: Select all

$#tab::
 cb1:=clipboardAll
 clipboard:="" ; need to clear the contents first or else ClipWait will move on no matter which contents are in it
 clipboard:="`t" ; can use `t as the tab character or an actual tab in between quotes like you did before
 clipwait,1
 send,^v
 Sleep, 500 ; allow the paste process to complete before restoring the clipboard contents
 clipboard:=cb1
return
I like using `t to represent the tab character because you can see it and it won't be mistaken for a space or spaces when you revisit your script. Either should work.

You shouldn't need to use ClipWait after assigning new contents to the clipboard, but if you do want to keep that to make sure, there's no reason to do so unless you clear the clipboard contents after saving the previous contents to cb1. If you don't clear the contents, ClipWait will not wait for the new contents because the old contents already satisfy its need for the clipboard to contain text if the previous contents were also text.

samzz
Posts: 35
Joined: 31 Jan 2014, 17:47

Re: Insert a TAB without losing previous CLIPBOARD

Post by samzz » 19 Jan 2022, 13:20

Problem solved. Thank you so much and happy coding :)

Post Reply

Return to “Ask for Help (v1)”