我如何才能管道输出的命令到我的剪贴板,并粘贴回来时,使用终端?例如:
cat file | clipboard
我如何才能管道输出的命令到我的剪贴板,并粘贴回来时,使用终端?例如:
cat file | clipboard
当前回答
使用.bashrc脚本的本机解决方案
Linux, macOS, Windows (WSL/CYGWIN)
这些系统都使用自己的工具将剪贴板功能合并到命令行界面(CLI)中。这意味着,当使用例如在Windows子系统for Linux (WSL)上的Ubuntu命令行时,通常的xclip解决方案将不起作用。这同样适用于macOS。
下表概述了在不同系统上需要的复制/粘贴工具:
OS | Copy | Paste |
---|---|---|
WSL | clip.exe |
powershell.exe Get-Clipboard |
CYGWIN | > /dev/clipboard |
cat /dev/clipboard |
macOS | pbcopy |
pbpaste |
Linux | xclip -sel clip |
xclip -sel clip -o |
统一的。bashrc解决方案
只需将以下代码放入~/。允许在所有系统上使用复制和粘贴。该解决方案适用于“正常”Linux系统(即Ubuntu, Debian)以及WSL和macOS:
if grep -q -i microsoft /proc/version; then
# on WSL: version contains the string "microsoft"
alias copy="clip.exe"
alias paste="powershell.exe Get-Clipboard"
elif grep -q -i cygwin $(uname -a); then
# on CYGWIN: uname contains the string "cygwin"
alias copy="/dev/clipboard"
alias paste="cat /dev/clipboard"
elif [[ ! -r /proc/version ]]; then
# on MAC: version is not readable at all
alias copy="pbcopy"
alias paste="pbpaste"
else
# on "normal" linux
alias copy="xclip -sel clip"
alias paste="xclip -sel clip -o"
fi
在所有系统上使用
复制:
# pipe
echo "hello world" | copy
# or for direct file input
copy < file
粘贴:
paste > file
其他回答
把这个添加到~/.bashrc:
# Now `cclip' copies and `clipp' pastes'
alias cclip='xclip -selection clipboard'
alias clipp='xclip -selection clipboard -o'
现在剪贴粘贴和剪贴复制-但你也可以做更花哨的东西: 剪切| sed 's/^/ /' |剪辑 ↑缩进您的剪贴板;适用于没有堆栈溢出{}按钮的站点
您可以通过运行以下命令添加它:
printf "\nalias clipp=\'xclip -selection c -o\'\n" >> ~/.bashrc
printf "\nalias cclip=\'xclip -selection c -i\'\n" >> ~/.bashrc
只是为了覆盖一个边缘情况:),因为问题标题问(至少现在)如何将命令的输出直接复制到剪贴板。
我经常发现,在命令已经执行并且我不想或不能再次执行之后,复制命令的输出是很有用的。
对于这个场景,我们可以使用gdm或类似的鼠标实用程序并使用鼠标进行选择。apt-get安装gdm,然后右键单击或Cntrl+Shift+c和Cntrl+Shift+v组合在终端进行复制和粘贴
Or, which is the preferred method for me (as the mouse cannot select properly inside one pane when you have multiple panes side by side and you need to select more than one line), using tmux we can copy into the tmux buffer using the standard [ , space , move to select , enter or you can select a block of code. Also this is particularly useful when you are inside one of the lanes of the cli multiplexer like tmux AND you need to select a bunch of text, but not the line numbers (my vim setup renders line numbers)
在此之后,您可以使用命令:
tmux save-buffer - | xclip -i
当然,您可以将其命名为您喜欢的名称,或者直接绑定到tmux配置文件中
这只是给您一个概念性的答案,以覆盖这种不可能再次执行命令的边缘情况。如果您需要更具体的代码示例,请告诉我
干杯
这是Arch Linux用户的一个很好的解决方案。用pacman安装xsel,如下所示:
sudo pacman -S xsel
在~/中创建别名。Bashrc文件,比如:
alias pbcopy='xsel --clipboard --input'
alias pbpaste='xsel --clipboard --output'
用源代码重新加载终端:
source ~/.bashrc
像上面提到的那样使用它:
cat your_file.txt | pbcopy
供参考,在~/中填充所有别名是很好的做法。别名并在。bashrc文件中调用它
在不使用外部工具的情况下,如果你连接到服务器视图SSH,这是一个相对简单的命令:
在Windows 7+命令提示符中:
ssh user@server cat /etc/passwd | clip
这将把远程文件的内容放到本地剪贴板中。
(该命令需要运行Pageant以获取密钥,否则它将要求您输入密码。)
一种方法是:
安装xclip,例如: Sudo apt-get安装xclip 管道输出到xclip复制到剪贴板: Cat文件| xclip 将刚才复制的文本粘贴到X应用程序中: xclip - o
要粘贴除X应用程序之外的其他地方,例如浏览器窗口中网页的文本区域,请使用:
cat file | xclip -selection clipboard
考虑创建一个别名:
alias "c=xclip"
alias "v=xclip -o"
为了了解这有多有用,假设我想在一个新的终端窗口中打开我的当前路径(在某些系统中可能有其他的方法,如Ctrl+T,但这只是为了说明目的):
Terminal 1:
pwd | c
Terminal 2:
cd `v`
注意v周围的' ',这首先将v作为命令执行,然后将其替换为cd使用。
只复制内容到X剪贴板
cat file | xclip