2

I'd like to exchange the currently selected text in eclipse (or even any program) on linux with the content of the clipboard when pressing a shortcut like Ctrl-B. Any ideas?

A similar question has been posted here regarding Visual Studio but unfortunately the only useful answer pointed to AutoHotkey which is only available for Windows or is there a linux equivalent?

Community
  • 1
  • 1
user829755
  • 1,489
  • 13
  • 27

3 Answers3

2

There is a project called IronAHK which aims to make AutoHotkey cross-platform. It looks to have an extremely long development cycle, so I am not certain it supports everything that the latest AutoHotkey does. It is definitely worth a look!

Below is the code that would work in Windows:

^b::
    Old_Clip := clipboard
    Send ^x
    Send % Old_Clip 
Return
Elliot DeNolf
  • 2,920
  • 1
  • 15
  • 12
  • +1 for that link. I managed to install IronAHK and to run the Hello World example program, but now I'm stuck and the ironahk docu is quite sparse and many things yield "The requested feature is not implemented.". What I don't understand at all is how ahk could interact with a different program like eclipse. would I have to launch the program from ahk or can ahk capture the keyboard for the whole system? – user829755 Jun 25 '13 at 22:05
  • I looked at IronAHK once more and it seems not yet mature enough. On http://www.ironahk.net/docs/developing/status/ it says that most methods aren't implemented yet, including the Send method which is probably essential for my purpose. – user829755 Aug 09 '13 at 16:43
1

found following solution using xvkbd:

  • make sure following packages are installed: xsel xvkbd
  • save the script below and bind it to a global keyboard shortcut like ^B. (in KDE I had to add it to the KDE-Menu first)

this is not very robust, e.g. when I tested it without the sleep command, the ctrl modifier key seemed to be stuck, i.e. the A key was interpreted as ctrl-A and I found no way to reset it. Otherwise this does what I want.

#!/bin/sh
# swap currently selected text with content of system clipboard, i.e.
# 1. save current clipboard content in oldClip
# 2. copy current selection into clipboard
# 3. print oldClip so that it overwrites previous selection
# this is supposed to work in eclipse but could work in other applications, too.
# usage: invoke this script via a global keyboard shortcut

# give user time to release keys, otherwise ctrl modifier might get stuck
sleep 0.5

# when run via shortcut stdin+stdout are redirected => xsel behaves differently.
# therefore always specify the mode explicitly.
oldClip=`xsel --clipboard --output`
xsel --primary --output | xsel --clipboard --input

xvkbd -text "$oldClip"
user829755
  • 1,489
  • 13
  • 27
0

With xclip, these three steps can be performed (read > as "replaces"):

  • Primary > Secondary
  • Cliboard > Primary
  • Secondary > Clipboard

Since no program ever uses it, we use Secondary as a temporary slot to swap Clipboard with Primary.

xclip -o -sel p|xclip -i -sel s
xclip -o -sel c|xclip -i -sel p
xclip -o -sel s|xclip -i -sel c

Make the script executable with chmod +x script and now you can bind a shortcut to it.

Quasímodo
  • 3,812
  • 14
  • 25