# 向X window程序发送字符串

在一些日常维护操作中，会经常反复输入相同的命令，让人很是厌烦。

如何能够偷懒，使用较少的击键（如桌面快捷键）就完成一长串的文字输入呢？

X window平台提供了一个[xdotool](http://www.semicomplete.com/projects/xdotool/xdotool.xhtml)工具，可以神奇地节约不少重复击键的时间。

以下脚本存储为 `~/bin/sendtext.sh`

```bash
#!/usr/bin/bash
text=$1
windowid=$(xdotool getwindowfocus)
sleep 0.1 && xdotool windowactivate --sync $windowid type $text
```

在LXQt和Xfce管理桌面快捷键中创建一个新的快捷键`Meta-t`，则在Windows或Mac系统按下`Window`键/`Command`键加上`t`键，就会向当前窗口发送`text`这个字符串。只需要修改一下脚本，替换成你希望输入的大段文本，就可以一键输入大量的文字，堪称节约生命的神器。

## 参考

* [Keyboard Shortcut To Send Text Strings To Program](https://unix.stackexchange.com/questions/36922/keyboard-shortcut-to-send-text-strings-to-program)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://huataihuang.gitbook.io/cloud-atlas-draft/os/linux/x/send_string_to_x.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
