使用 OS X 中的键盘从 Terminal.app 选择文本

使用 OS X 中的键盘从 Terminal.app 选择文本

我喜欢从终端复制内容,但不喜欢用鼠标操作。如何使用键盘在 Terminal.app 中选择文本?

例如,在终端中,人们可能希望能够移动到一行上的某个点,然后使用左右箭头键突出显示部分文本,然后可以剪切或复制。这可能吗?

答案1

如果要复制命令的输出,可以使用pbcopy。要获取特定输出,请使用grep

例如,复制整个输出netstat -an

netstat -an | pbcopy

仅复制开放端口(监听)的输出:

netstat -an | grep LISTEN | pbcopy

Terminal.app 本身没有办法使用键盘选择文本,但使用终端多路复用器(如 screen 或 tmux),您可以在“复制模式”下执行此操作。Screen 默认安装在 OS X 上,而 tmux 可以通过第三方工具(如 Homebrew 或 Macports)安装。这两个程序的手册页都描述了复制模式以及如何选择要复制到剪贴板的文本。在 screen 手册页中,可以在CUSTOMIZATION下的部分中找到有关导航复制模式的更多信息copy。以下是摘录:

   copy

   Enter  copy/scrollback mode. This allows you to copy text from the cur-
   rent window and its history into the paste buffer. In this mode  a  vi-
   like `full screen editor' is active:
   Movement keys:
     h, j, k, l move the cursor line by line or column by column.
     0,  ^  and  $  move to the leftmost column, to the first or last non-
       whitespace character on the line.
     H, M and L move the cursor to the leftmost column of the top,  center
       or bottom line of the window.
     + and - positions one line up and down.
     G moves to the specified absolute line (default: end of buffer).
     | moves to the specified absolute column.
     w, b, e move the cursor word by word.
     B, E move the cursor WORD by WORD (as in vi).
     C-u  and  C-d  scroll  the display up/down by the specified amount of
       lines while preserving the cursor position. (Default: half  screen-
       full).
     C-b and C-f scroll the display up/down a full screen.
     g moves to the beginning of the buffer.
     % jumps to the specified percentage of the buffer.

这仍然无法与系统的剪贴板交互,因此您必须以某种方式复制输出,也许通过将粘贴缓冲区内容回显到 pbcopy。示例工作流程:

(Ca [ 表示“按住 Control 键并按 A,然后按 [”,与 ] 相同,请参阅 screen 手册页。)

  1. 启动屏幕并在那里进行工作。
  2. 进入复印模式:Ca[
  3. 使用 vi 键绑定进行导航(或根据手册页设置为使用 emacs 绑定)。
  4. 使用空格键开始选择文本。
  5. 按回车键将选择内容复制到粘贴缓冲区。这将退出复制模式。
  6. 用 Ca 粘贴缓冲液]

    回声'Ca]'|复制

(不要逐字输入 Ca ],使用上面描述的键绑定,必要时使用引号)

您选择的文本应该在系统剪贴板中,您可以将其粘贴到另一个窗口中。例如以下内容:

   paste [registers [dest_reg]]

   Write  the  (concatenated)  contents  of the specified registers to the
   stdin queue of the current window. The register . is treated  as  the
   paste  buffer. If no parameter is given the user is prompted for a sin-
   gle register to paste.  The paste buffer can be filled with  the  copy,
   history  and  readbuf commands.  Other registers can be filled with the
   register, readreg and paste commands.  If paste is called with a second
   argument,  the  contents  of the specified registers is pasted into the
   named destination register rather than the window. If .  is  used  as
   the  second  argument,  the  displays  paste buffer is the destination.
   Note, that "paste" uses a wide variety of resources: Whenever a  second
   argument  is  specified  no  current  window is needed. When the source
   specification only contains registers (not the paste buffer) then there
   need not be a current display (terminal attached), as the registers are
   a global resource. The paste buffer exists once for every user.

是从屏幕手册页中执行上述操作的:)。

现在说了这么多,你确实特别提到了 Terminal.app,处于 alpha 测试阶段的 iTerm2 版本 0.20.20101102 的一个新功能显示:

  • 无鼠标选择:您经常在同一窗口内复制粘贴文本吗?现在您可以不用鼠标来做这件事了。使用 cmd-f 打开查找栏并搜索要复制的文本。当部分文本匹配时,使用 tab 键将选择范围向右扩展一个完整单词,使用 shift-tab 将其向左扩展至上一个单词边界。它会自动复制到剪贴板,或者您可以使用 opt-Enter 立即粘贴选择内容。

虽然它还处于 alpha 阶段,但我已经在我的所有终端工作中使用它大约一个半月了,它非常稳定。

iTerm2 主页

答案2

如果您想要复制的是您在 Bash 提示符下输入的命令,那么您可以使用 Bash 的readline行编辑功能:

以下是 (默认) emacs 模式下的一个示例:

  • Ctrl-r并输入你想从历史记录中调用的部分命令
  • Ctrl-u将该行剪切(删除)到删除缓冲区
  • Ctrl-y将 kill buffer 粘贴(拉出)到命令行(您可以多次按此键以获得重复的副本)

可能性有很多,包括影响行的部分内容或特定的单词等等。

man bash在“Readline”部分和man readline了解更多信息。

相关内容