我如何在没有鼠标的情况下从我的xterm中复制文本?

时间:2022-09-03 00:18:43

I want to use part of the output of a command run from the command line in another xterm, or as part of a different command. For instance:

我希望在另一个xterm中使用命令行运行的部分输出,或者作为另一个命令的一部分。例如:

> grep error error.log
error: can't find file ~/<some very long path>/thisfile

and I want to do this:

我想这样做:

>ls ~/<some very long path>/

I know two ways to do this:
1. copy ~/<some very long path>/ with the mouse.
2. use some combination of head/tail/awk/sed/perl/cut/etc... to extract only what I need from the output and then use that inside backticks.

我知道有两种方法:1。复制~/ <一些非常长的路径> /与鼠标。2。使用一些头/尾/awk/sed/perl/cut/等组合…为了只从输出中提取我需要的东西,然后使用这个内勾。

Is there any way to copy text without using the mouse? The example that comes to mind is visual mode inside VIM, but I don't know how to do that inside the xterm.

有没有不用鼠标复制文本的方法?我想到的例子是VIM中的视觉模式,但是我不知道如何在xterm中实现。

5 个解决方案

#1


38  

You can use GNU screen's copy and paste commands.

您可以使用GNU screen的复制和粘贴命令。

Quick tutorial:

快速教程:

  1. Open screen: screen (or screen myprog my args here)
  2. 打开屏幕:屏幕(或屏幕myprog my args here)
  3. Run your program, producing output you want copied
  4. 运行您的程序,生成您想要复制的输出
  5. Enter copy mode: ^A [
  6. 进入副本模式:^(
  7. Move your cursor to the start point
  8. 将光标移动到起点。
  9. Hit enter
  10. 回车
  11. Move your cursor to the end point
  12. 将光标移到终点
  13. Hit enter
  14. 回车
  15. Paste: ^A ]
  16. 粘贴:^)

Screen is much more powerful than that (I use it to tab several virtual terminals without the need for a special terminal emulator, and also so that I don't loose my sessions when X crashes or something). To get out of screen, simply end your shell session, or type ^A ^.

Screen的功能要强大得多(我使用它来标记多个虚拟终端,而不需要特殊的终端仿真器,而且当X崩溃或其他情况时,我也不会中断会话)。的屏幕,简单的shell会话结束,或者类型^ ^。

#2


11  

I find XSel is useful in similar situations. It's a tool that manipulates the X selection. For example, this will store the output of your command to the clipboard:

我发现XSel在类似的情况下很有用。它是一个操纵X选择的工具。例如,它将把命令的输出存储到剪贴板:

grep error error.log | xsel -bi

#3


5  

You could also use tmux which has similar capabilities to screen. Once you're in tmux and you have the output you want to copy, you can do the following to copy and then paste it:

您还可以使用tmux,它具有与screen类似的功能。一旦你在tmux,你有你想要拷贝的输出,你可以做以下的复制然后粘贴:

  1. Enter copy mode with Ctrl + B, [
  2. 使用Ctrl + B进入复制模式,[
  3. Move the cursor with the arrow keys and then start selection with Ctrl + Space
  4. 使用箭头键移动光标,然后使用Ctrl +空格开始选择
  5. Move the cursor to select the text/region to copy and then copy with Alt + W (this will immediately exit you from copy mode)
  6. 移动光标选择要复制的文本/区域,然后使用Alt + W进行复制(这将立即退出复制模式)
  7. You can now paste (within tmux only) using Ctrl + B, ]
  8. 您现在可以使用Ctrl + B粘贴(仅在tmux中),]

These instructiuons assume emacs key bindings are enabled. For the vi bindings see this comparison or just follow this step-by-step guide.

这些指令说明假定emacs键绑定已经启用。对于vi绑定,请参见这个比较,或者遵循这个步骤指南。

#4


2  

The OP said he didn't want to do this, but here is a unix utils way to do it for posterity. If you learn these little unix utilities they can be quite powerful.

OP说他不想这样做,但是这里有一个unix实用程序的方法来为子孙后代做这件事。如果您了解了这些小的unix实用程序,它们可能会非常强大。

ls $( dirname $( grep error error.log | head -1 | cut -d " " -f 5- ) )

ls $(dirname $) (grep错误。log | head -1 | cut -d " " - f5 - ")

note: syntax from memory

注意:从记忆语法

#5


1  

You may send output directly to the concerned xterm using xterm device no. Here is the command.

您可以使用xterm设备号直接将输出发送到相关的xterm。这是命令。

xyz$ls /home/ankit/documents/etc/x/y/z > /dev/pts/0 

(or watever is the device name, You can get it using command 'w' for opened xterm)

(或者watever是设备名,您可以使用命令“w”获取它,用于打开的xterm)

Try it & hope this may solve your problem.

试试看,希望这能解决你的问题。

#1


38  

You can use GNU screen's copy and paste commands.

您可以使用GNU screen的复制和粘贴命令。

Quick tutorial:

快速教程:

  1. Open screen: screen (or screen myprog my args here)
  2. 打开屏幕:屏幕(或屏幕myprog my args here)
  3. Run your program, producing output you want copied
  4. 运行您的程序,生成您想要复制的输出
  5. Enter copy mode: ^A [
  6. 进入副本模式:^(
  7. Move your cursor to the start point
  8. 将光标移动到起点。
  9. Hit enter
  10. 回车
  11. Move your cursor to the end point
  12. 将光标移到终点
  13. Hit enter
  14. 回车
  15. Paste: ^A ]
  16. 粘贴:^)

Screen is much more powerful than that (I use it to tab several virtual terminals without the need for a special terminal emulator, and also so that I don't loose my sessions when X crashes or something). To get out of screen, simply end your shell session, or type ^A ^.

Screen的功能要强大得多(我使用它来标记多个虚拟终端,而不需要特殊的终端仿真器,而且当X崩溃或其他情况时,我也不会中断会话)。的屏幕,简单的shell会话结束,或者类型^ ^。

#2


11  

I find XSel is useful in similar situations. It's a tool that manipulates the X selection. For example, this will store the output of your command to the clipboard:

我发现XSel在类似的情况下很有用。它是一个操纵X选择的工具。例如,它将把命令的输出存储到剪贴板:

grep error error.log | xsel -bi

#3


5  

You could also use tmux which has similar capabilities to screen. Once you're in tmux and you have the output you want to copy, you can do the following to copy and then paste it:

您还可以使用tmux,它具有与screen类似的功能。一旦你在tmux,你有你想要拷贝的输出,你可以做以下的复制然后粘贴:

  1. Enter copy mode with Ctrl + B, [
  2. 使用Ctrl + B进入复制模式,[
  3. Move the cursor with the arrow keys and then start selection with Ctrl + Space
  4. 使用箭头键移动光标,然后使用Ctrl +空格开始选择
  5. Move the cursor to select the text/region to copy and then copy with Alt + W (this will immediately exit you from copy mode)
  6. 移动光标选择要复制的文本/区域,然后使用Alt + W进行复制(这将立即退出复制模式)
  7. You can now paste (within tmux only) using Ctrl + B, ]
  8. 您现在可以使用Ctrl + B粘贴(仅在tmux中),]

These instructiuons assume emacs key bindings are enabled. For the vi bindings see this comparison or just follow this step-by-step guide.

这些指令说明假定emacs键绑定已经启用。对于vi绑定,请参见这个比较,或者遵循这个步骤指南。

#4


2  

The OP said he didn't want to do this, but here is a unix utils way to do it for posterity. If you learn these little unix utilities they can be quite powerful.

OP说他不想这样做,但是这里有一个unix实用程序的方法来为子孙后代做这件事。如果您了解了这些小的unix实用程序,它们可能会非常强大。

ls $( dirname $( grep error error.log | head -1 | cut -d " " -f 5- ) )

ls $(dirname $) (grep错误。log | head -1 | cut -d " " - f5 - ")

note: syntax from memory

注意:从记忆语法

#5


1  

You may send output directly to the concerned xterm using xterm device no. Here is the command.

您可以使用xterm设备号直接将输出发送到相关的xterm。这是命令。

xyz$ls /home/ankit/documents/etc/x/y/z > /dev/pts/0 

(or watever is the device name, You can get it using command 'w' for opened xterm)

(或者watever是设备名,您可以使用命令“w”获取它,用于打开的xterm)

Try it & hope this may solve your problem.

试试看,希望这能解决你的问题。