我如何才能管道输出的命令到我的剪贴板,并粘贴回来时,使用终端?例如:
cat file | clipboard
我如何才能管道输出的命令到我的剪贴板,并粘贴回来时,使用终端?例如:
cat file | clipboard
当前回答
有一个方法可以在不安装xclip/xsel的情况下工作,如果您没有sudo访问权限,这个方法非常有用。
你只需要安装vimx,这是VIM +剪贴板支持。你可以在大多数发行版中找到它。
将脚本保存为~/copy_to_clipboard.sh,
#!/bin/sh
if [ -z "$1" ]
then
str=" "
else
str=$1
fi
vimx -u NONE -U NONE -N -c "let @a=\".\"" -c "let @*= \"$str\" " -c " put a " -c "sleep 2" -c " q!"
赋予执行权限:chmod +x ~/copy_to_clipboard 并调用它:
~/copy_to_clipboard.sh STRING
出于某种原因,您需要设置一个虚拟的@a寄存器,并将其粘贴到临时文件上。否则剪贴板寄存器将无法正确设置。 最后丢弃临时Vim文件。
你的剪贴板将被设置,你可以用鼠标中点击粘贴它
其他回答
我制作了一个提供类似功能的小工具,但没有使用xclip或xsel。Stdout复制到剪贴板,并可以再次粘贴到终端中。看到的:
https://sourceforge.net/projects/commandlinecopypaste/
注意,这个工具不需要x会话。剪贴板可以在终端内使用,不需要通过Ctrl+V或鼠标中键单击粘贴到其他x窗口中。
把这个添加到~/.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
我在这里创建了一个工具
取决于xclip包
只是为了覆盖一个边缘情况:),因为问题标题问(至少现在)如何将命令的输出直接复制到剪贴板。
我经常发现,在命令已经执行并且我不想或不能再次执行之后,复制命令的输出是很有用的。
对于这个场景,我们可以使用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配置文件中
这只是给您一个概念性的答案,以覆盖这种不可能再次执行命令的边缘情况。如果您需要更具体的代码示例,请告诉我
干杯
基于之前的文章,我最终得到了以下轻量级的别名解决方案,可以添加到.bashrc:
if [ -n "$(type -P xclip)" ]
then
alias xclip='xclip -selection clipboard'
alias clipboard='if [ -p /dev/stdin ]; then xclip -in; fi; xclip -out'
fi
例子:
# Copy
$ date | clipboard
Sat Dec 29 14:12:57 PST 2018
# Paste
$ date
Sat Dec 29 14:12:57 PST 2018
# Chain
$ date | clipboard | wc
1 6 29