在 Web 浏览器中使用空格缩进文本块的最快方法是什么?

ænd*_*rük 5 text code-blocks

我偶尔需要缩进带有空格的文本块以在 Web 浏览器中使用,例如,在格式化此站点上的代码块或 Launchpad 上的帖子时。

到目前为止,我只是通过将四个空格复制到剪贴板然后快速混合键来手动完成它:

?, Home, Ctrl+ V(重复)

实现这一目标的最快方法是什么?

  • 复制并粘贴到另一个程序?(哪一个?)
  • Firefox 或 Chrome 浏览器扩展程序?
  • 直接修改剪贴板内容的命令?
  • 一个自动打字程序?

Fre*_*urk 5

您可以通过一个简短的 shell 脚本进行管道传输:

$ xsel -b | sed 's/^/    /' | xsel -b
Run Code Online (Sandbox Code Playgroud)

第一个 xsel -b 读取剪贴板,sed 添加四个空格(^ 匹配行首),然后第二个 xsel -b 将其放回剪贴板。删除 -b 以使用主要选择(中键单击粘贴缓冲区)。

例子:

# put two lines in the clipboard, "abc" and "123", for the example
# the \n is a newline, and echo adds another newline to the end
$ echo $'abc\n123' | xsel -b
Run Code Online (Sandbox Code Playgroud)

$ xsel -b | sed 's/^/    /'  # output written to the terminal
    abc
    123
$ xsel -b | sed 's/^/    /' | xsel -b  # again, to the clipboard
Run Code Online (Sandbox Code Playgroud)

您可以将其放入 shell 脚本中,例如名为“indent4”,其内容为:

#!/bin/bash
xsel -b | sed 's/^/    /' | xsel -b
Run Code Online (Sandbox Code Playgroud)

然后使其可执行。您还可以通过右键单击 Nautilus 中的文件、转到属性并更改权限来执行 chmod。

$ chmod +x indent4
Run Code Online (Sandbox Code Playgroud)

# test it:
$ echo $'abc\n123' | xsel -b  # load clipboard
$ ./indent4                   # assuming it's in the current directory
$ xsel -b                     # show clipboard
    abc
    123
Run Code Online (Sandbox Code Playgroud)

然后将文件放在桌面上,或将其存储在任何地方并为其制作启动器。现在您可以运行 indent4 (例如双击)并且剪贴板将被修改。

xsel 来自一个名为“xsel”的包,这并不奇怪。你可能需要安装它。有关详细信息,请参阅“man xsel”。示例中的 $'' 样式字符串是 bash 特定的。