how to swap clipboard content with selection upon keyboard shortcut

后端 未结 3 1852
野趣味
野趣味 2021-01-16 13:28

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

3条回答
  •  心在旅途
    2021-01-16 14:19

    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"
    

提交回复
热议问题