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

hek*_*ran 10 unix keyboard-shortcuts terminal.app selection macos

我喜欢从终端复制东西,但我不喜欢移动到鼠标。如何使用键盘在 Terminal.app 中选择文本?

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

jti*_*man 12

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

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

netstat -an | pbcopy
Run Code Online (Sandbox Code Playgroud)

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

netstat -an | grep LISTEN | pbcopy
Run Code Online (Sandbox Code Playgroud)

Terminal.app 本身没有办法使用键盘来选择文本,但是使用终端多路复用器(如 screen 或 tmux),您可以在“复制模式”下进行选择。OS X 上默认安装了 Screen,tmux 可以通过 Homebrew 或 Macports 等第三方工具安装。这两个程序的手册页描述了复制模式以及如何选择要复制到剪贴板的文本。在屏幕手册页中,可以在 下的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.
Run Code Online (Sandbox Code Playgroud)

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

(Ca [ 是“按住控制按 A 然后点击 [”,同样适用于 ]。请参阅屏幕手册页。)

  1. 启动屏幕并在那里工作。
  2. 进入复制模式:Ca [
  3. 使用 vi 键绑定导航(或根据手册页设置为使用 emacs 绑定)。
  4. 用空格键开始文本选择。
  5. 按 Enter 键将选择复制到粘贴缓冲区。这将退出复制模式。
  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.
Run Code Online (Sandbox Code Playgroud)

来自执行上述操作的屏幕手册页:)。

现在说了这么多,你确实提到了 Terminal.app,iTerm2 版本 0.20.20101102 的一项新功能正在 alpha 测试中显示:

  • 无鼠标选择:您是否经常在同一窗口中复制粘贴文本?现在您无需使用鼠标即可完成此操作。使用 cmd-f 打开查找栏并搜索要复制的文本。当部分文本匹配时,使用 tab 键将所选内容向右扩展一个完整的单词,然后使用 shift-tab 将其向左扩展到上一个单词边界。它会自动复制到剪贴板,或者您可以使用 opt-Enter 立即粘贴选择。

虽然它是 alpha 版本,但我已经将它用于我所有的终端工作大约一个半月,并且非常稳定。

iTerm2 主页