AHK Script for making Control+v work in non-CMD terminals (Bash, Putty, WSL...)

146 Views Asked by At

I want an AHK Script that remaps Control+v to send the Clipboard, as it is intended, in non-CMD terminals, like Bash and Putty.

Each terminal has a different command to paste the clipboard and it's always annoying to remmber which I should use in that moment. And the usual Control+C , Control+V causes some weird characters to appear (^[[200~...~), which then I need to delete manually.


I created the following script:


#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.

; #Warn  ; Enable warnings to assist with detecting common errors.

SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.

SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#SingleInstance, Force  

SetKeyDelay -1

^v::

sendRaw,% clipboard

return

Which works 'fine', but have some small cavieats:

When I copy something that expect another char to come right after, such as ` ´ ^ ~, or chars that are expected in pairs, such as " ( [ {, it has a strange behaviour, instead of just pasting the unique char I copied. On the first case, I have to paste twice in oder to anything to appear and then remove one of the duplicated chars. On the second, when the apps expect closures, like VScode, Intellij, instead of printing just the char I copied, it prints the opening and the closing characters, which I then have to delete the later.

2

There are 2 best solutions below

1
Vinícius Matos On

As per Relax comment, text mode solved almost all use cases...

In the end I had to use #IfWinActive, because print screens wouldn't work with that implementation...

That's the final solution:

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance, Force  
SetKeyDelay -1

#IfWinActive, ahk_exe mintty.exe 
^v::
#IfWinActive, ahk_exe cmd.exe
^v::
#IfWinActive, ahk_exe putty.exe
^v::
  send {Text}%clipboard% 
return

7
Kaz On

In PuTTY, the insert hot key sequence is Shift+Insert.

I think if it's possible to use AutoHotKey to intercept Ctrl+V, and have it send Shift+Insert to the PuTTY window, that should make PuTTY do a paste from the clipboard.

This AHK Version 2 script works for me:

#HotIfWinActive("ahk_exe putty.exe")
^v::Send "+{Ins}"
return

When this is active, Ctrl+V in Putty performs a paste.

Those weird characters ^[[200~...~ are the Xterm mouse protocol. You get the same if you paste with the mouse button. It's a long escape sequence that contains the paste data. It requires the program on the other end to understand the protocol. Vim has support for it, for instance.

PuTTY is not always in this Xterm mouse mode. I find that it gets into that mode "by accident" somehow, and won't come out of it even when the correct escape sequence is issued for exiting the mode. It can be a nuisance.

I suspect that if Vim is in that Xterm mouse mode, you will get those funny characters no matter how you paste (such as with the above AHK script). That is to say, PuTTY's paste feature is doing that, and there is probably no way around it other than to turn off the Xterm mouse mode.