• テキストを選択
  • ヒットキーコマンド
  • Bashスクリプトがテキストを処理する
  • BashスクリプトはテキストをGUIエディターに貼り付け、選択を置き換えます(<-これは私が理解できないことです)
  • フィン




  • Xubuntu 19.10を使用しています
  • 単純なテキストに置き換えたいだけです(たとえば、小文字を大文字に変更します)
  • これをシンプルに保ちましょう-(キーコマンドを使用して)bashスクリプトからテキストを取得して、現在カーソルがあるGUIテキストエディター/エリアに貼り付け、選択したテキストがある場合はそれを置き換えますか?

Since you are using Xorg and ctrl+v ist almost universally the shortcut for paste, the easiest solution is xdotool (available from the official repositories):

xdotool key --clearmodifiers "ctrl+v"

The --clearmodifiers flag attempts to clear all modifiers (ctrl,alt...) before issuing the key presses and restores them afterwards. This is useful when you invoke your script via a keyboard shortcut, because otherwise the application will also see the modifiers you pressed for your shortcut, and e.g. ctrl+alt+v may result in a different command.

Sometimes, this can result in a race condition when you release the modifiers before xdotool restores them, making them stuck. In that case, just press and release the stuck modifiers to unstick them.

xdotool can do a lot more. Read man xdotool to get an overview. It is also often used in combination with xbindkeys, which lets you configure keyboard shortcuts more versatilely than e.g. gnome and also enables you to bind actions to mouse buttons.


While bash and xdotool will definitely handle a simple case like this, this is exactly the sort of thing AutoKey was designed to do.

In AutoKey, you can define a simple script written in Python using the AutoKey API to do this. This can be assigned to a hotkey and will run whenever that hotkey is pressed. It would look something like this (untested.)

field = clipboard.get_selection()
field = field.lower()

which gets the selected text into a variable, converts it to lower case, puts it back in the clipboard, and then pastes it back into the current selection where it originally came from. (If your application window is a terminal, then you'd have to use Ctrl+Shift+v instead.)

Doing it this way has several advantages: It's arguably easier to do than modifying your keyboard. You can easily change the hotkey in the AutoKey GUI. You can define a window filter so the hotkey only works in the windows of your choice. You can turn this functionality on and off at will. And, since you have the entire power of Python at your disposal, you do almost anything else you can think of.

If you had just wanted to substitute one phrase for another, an AutoKey phrase would have done that without writing a single line of code, but since you needed to manipulate the text, a script was required.

Note: Debian and derivative distributions (Ubuntu ...) currently provide a very old version of AutoKey. This is fixed in Debian testing and will be fixed in Ubuntu 20.04. For now, you can easily install the package with these instructions.

For assistance, visit our support forum.


I agree with Joe that AutoKey would be perfect for what you want to do. I thought I'd include another example script. This one uses string formatting to also change the selected text to lower case (you could have it do whatever you like instead of changing case):

selection = clipboard.get_selection()
keyboard.send_keys("%s" % selection.lower())