We help IT Professionals succeed at work.

Autohotkey: duplicate word

High Priority
155 Views
Last Modified: 2020-02-20
Hello experts ,
I am looking for a procedure to duplicate a word from left to the right or right to the left.
Here is my attempt:
#w::
SendInput ^{Left}+^{Right}^c
ClipWait, 1
Send, {Right}+{Space}^v
Return

Open in new window

The problem with it is that it creates an extra space between word and duplicated ex : word    word    
Do you know if there is a better way to duplicate word from left to the right or right to the left without creating space?
Thank you for your help.
Comment
Watch Question

Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Commented:
Hi Luis,
This depends on what product you are in. Notepad? Notepad++? Word? WordPad? KEDIT? They may, or may not, handle the same keystrokes in the same way. For example, consider this sentence:

Hi Luis, I hope you are well today.

When you do a Ctrl+Left followed by a Shift+Ctrl+Right when inside the word Luis in that sentence, Notepad selects both the comma and the space after Luis, but Notepad++ selects only Luis...neither the comma nor the space. In any case, let me know what product(s) you want this for. Regards, Joe
Luis DiazIT consultant

Author

Commented:
Hi Joe,
The aim is to use it at word, office, notepad++ and chorme.
Can we have multiple conditions (#If WinActive) and adapt to each of the app?
Thank you for your help.
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Commented:
> word, office, notepad++ and chorme

Word, Notepad++, and Chrome are clear, but what do you mean by Office?

> Can we ... adapt to each of the app?

Yes.
Luis DiazIT consultant

Author

Commented:
Sorry, by office I meant Outlook which should be in line with Word.
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Commented:
Do you want a space between the original word and its duplicate? For example, do you want this...

word word

...or this:

wordword
Luis DiazIT consultant

Author

Commented:
An space :
word word
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Commented:
OK, will have a solution for you soon.
Luis DiazIT consultant

Author

Commented:
Thank you Joe.
Developer
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018
Commented:
Hi Luis,

This AutoHotkey script does what you want in Chrome, Notepad++, Outlook, and Word:

SendMode Input ; faster and more reliable
!^w::
Send ^{Left} ; go to beginning of word
Sleep 50 ; give it time to get there
Send +^{Right} ; select word
Sleep 50 ; give it time to select
Send ^c ; copy to clipboard
ClipWait,1 ; give it one second to reach clipboard
If (ErrorLevel=1)
{
  MsgBox,4144,Error,Text did not appear in clipboard after one second
  Return
}
Send {Right} ; remove selection
Sleep 50 ; give it time to remove
Word:=Clipboard
LastChar:=SubStr(Word,0,1)
If (LastChar!=A_Space) ; if last character is already a space, don't need to add one
  Word:=A_Space . Word
Send %Word% ; duplicate the word
Return

Open in new window

Btw, I made the hotkey Alt+Ctrl+w — make it whatever you want, but the reason I changed it is that Win+w minimized all windows here...it overrode the AHK definition. Regards, Joe

Edit: See comment below on what Win+w actually did.
Luis DiazIT consultant

Author

Commented:
Thank you Joe.
Unable to test it right know.
I will test it soon and keep you informed.
Just for curiosity, which ahk allows you to minimize all windows and what is the difference between win+d which brings you to desktop and minimize all windows?
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Commented:
My previous comment was not completely accurate...yes, it minimized all windows, but what it really did was take me to the Switch User screen...shook me up as I was expecting it to duplicate the word. :) When I went back to the same user from the Switch User screen, all windows were minimized.

Regarding the other Win shortcut keys, Win+m attempts to minimize all windows, but not all apps honor the request. Win+d displays the desktop...resistance is futile. :)
Luis DiazIT consultant

Author

Commented:
Joe,
I tested proposal and it works!
Thank you for your help.
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Commented:
Hi Luis,
Glad to hear it! You're very welcome. Regards, Joe

Explore More ContentExplore courses, solutions, and other research materials related to this topic.