1

I frequently have to copy file paths from Windows 10 programs or dialogue boxes into my R code. Since R escapes backslashes, I always have to change the backslashes to forward slashes or double backslashes, so I am trying to program this problem away so that anytime I copy text and then use some special combination of shortcut keys to paste, it replaces my backslashes with double backslashes. So for example, if I copied "C:\windows\system32\drivers\etc" and then pressed some combination of keys like Ctrl+Alt+P (or even using the right click context menu) I'd like to change the pasted output to "C:\\windows\\system32\\drivers\\etc" instead of what was copied. I thought I might be able to do this in PowerShell with something like this:

$copiedtext = Get-Clipboard;
$copiedtext = -join(-join('"', $copiedtext -replace "\\", '\\'), '"');
Set-Clipboard $copiedtext;
Write-Output $copiedtext | clip

But that only outputs my clipboard to the console (and doesn't work if I assigne a shortcut to my PowerShell program and assign shotcut keys to it). Can anyone recommend how to modify this to achieve what I want or recommend some other language that would allow Windows to always recognize some shortcut key combination of characters as my special paste operation? I'd like to do this without having to start up the program when I want to use it too, if possible.

StatsStudent
  • 1,384
  • 2
  • 10
  • 28
  • 1
    [Several of these answers](https://stackoverflow.com/q/17605563/903061) are applicable, taking an R approach rather than a Windows approach. You can use RStudio snippets, or there's also [this old answer](https://stackoverflow.com/a/12703931/903061). But all of these require at least an additional keystroke. – Gregor Thomas Oct 24 '19 at 01:13
  • I'm trying to avoid an R approach. I think they are rather clumsy. I'd really like to intervene somehow after the copy and before the paste in Windows. – StatsStudent Oct 24 '19 at 04:30
  • If your problem isn't solved yet, please provide feedback. – mklement0 Oct 25 '19 at 01:59

1 Answers1

1

Note: While the following approach works, it is:

  • slow
  • not fully reliable

Ultimately, you're better off looking for a solution based on a specialized tool such as AutHotkey.


  • Create a script, say $HOME\pasteEscaped.ps1, with the following content:
# Get text from the clipboard and escape '\' chars. as '\\'
$escapedText = (Get-Clipboard -Format Text) -replace '\\', '\\' 

# Helper COM object for sending keystrokes.
$sh = New-Object -ComObject WScript.Shell

# Switch back to the previously active application...
$sh.SendKeys("%{tab}")
Start-Sleep -Milliseconds 100
# ... and send the escaped text. additionally escaped for .SendKeys()
$sh.SendKeys(($escapedText -replace '[{}()+^%]', '{$&}'))
  • Create a shortcut file, e.g. on your desktop, with the following properties:

    • Target:

      powershell.exe -executionpolicy bypass -noprofile -file %USERPROFILE%\pasteEscaped.ps1
      
    • Run: Minimized

    • Shortcut key: (a shortcut key of your choice)


Thereafter, when you press the shortcut key, the text is retrieved from the clipboard, the \ instances in it are doubled, and the resulting text is pasted into the foreground window, using simulated keyboard input.

mklement0
  • 382,024
  • 64
  • 607
  • 775