Yank: Yank Terminal Output to Clipboard
github.com
github.com
~/.tmux.conf:
# export to clipboard.
bind-key > run-shell -b 'tmux save-buffer - | xclip'
# paste from clipboard.
bind-key < run-shell -b '( xclip -o | tmux load-buffer - ) && tmux paste-buffer'
(edit: my actual setup is slightly different. It interacts with a Windows clipboard via ssh where the terminal is running. cf. https://github.com/euske/pyrexecd/blob/master/tools/tmux.con... )There seems to be multiple clipboards and I have no idea when I am using which. I generally spam some combination of ctrl-v, shift-insert, rightclick context menu paste, middleclick, etc until I get what I want.
I've abandoned vim for a gui solely because I have no idea how to copy stuff to/from it.
Can someone finally explain it to me? Or tell me what to do to make it sane.
(it of course did occur to me to google the question over the past 10 years but every time I did, I didn't find a satisfactory answer for something that ought to be a basic computer literacy skill)
For terminals, ctrl-c/Ctrl-v have conflicting interpretations so are usually bound to ctrl-shift-c/ctrl-shift-v. Some (e.g. iterm) try to emulate copy on select on operating systems that don't support it by copying the selection to the single clipboard.
For vim, the selections are bound to buffers. * Is the primary/selection buffer, + is the clipboard. Use "+y/"+p for ctrl-c/ctrl-v equivalents, or "*y/"*p for copy-on-select/middle click equivalents. Note that some distros ship a default "vim-minimal"/"vim-tiny" package built without this support so they can use it as "vi" for POSIX compatibility without pulling in x11 libraries. In that case look for a "vim-x11", "vim-full" or "gvim" package. Or use neovim, I haven't seen packagers slice that up the same way. Depending on your terminal, you may be also able to use the GUI equivalents in insert mode anyway (don't try use your terminal paste in normal mode, it'll paste into the command buffer).
Shift-Insert is unfortunately application dependent. I think it _should_ be equivalent to middle click, but some apps treat it as ctrl-v.
Some clipboard managers or applications will overwrite the primary contents with the clipboard contents when you perform an explicit copy.
primary = highlight / middle click
vim has its own used by default when you dd, p, etc. You can copy to one of the previous things (assuming your build enabled it) with "+y and "*y key sequences. You can also paste the same way, but I usually just let the term emu handle that with ctrl-shift-v or similar.
shift-insert is kinda weird actually. AFAIK it should paste from selection, but it often surprises me, so I avoid it.
edit: s/selection/primary/
set clipboard=unnamedplus " Use system clipboard.
(I don't know if the exact name depends on OS but this works on Linux and IIRC macOS)A nice thing you can do in Vim though is to make the default clipboard the system clipboard like this in your .vimrc:
set clipbaord=unnamedplus # 1. check the bindings for your system
stty -a
# 2. put the stuff you want to unbind in your shell rc file
stty <command name> undef
And then rebinding yank/paste in your .vimrc to only use your system clipboard.This would, however, become painful as soon as you need to quickly use a machine remotely where all your local configs won't be present. It also generally raises the question why one is using Vim in the first place, if one is not planning to use its powerful features.
Rightclick paste always pastes it into a terminal.
Selecting in the terminal window and right click copying is the only way to reverse it which is pretty attrocious as I have to zoom way out and other shenanigans.
If any emacs-fu greybeards know of how to emacs Yank to the Gnome clipboard I will be eternally grateful.
caveat my terminal app is usually emacs.
The submission is not equivalent but clearly can help avoid having to move your mouse around. Still wish I didn't have to modify the original command as, very often, I realize that I want to copy the command's output after I execute it.
It is never too late to steal features that the Commodore 64 could do in the early 80s (!!!).
oh, Extraterm can help you reuse previous output after the fact and without having to be clairvoyant.
With Zsh, cheating a little, you can achieve it with:
_insert-last-command-output () {
LBUFFER+="$(LS_COLORS= TERM=vt220 eval $history[$((HISTCMD-1))])"
}
There's some junk used for my configuration, the relevant part is the "eval" of the last command on the history, it doesn't paste the output of the previous command but it's execute d again, this time with the output going straight in the edit buffer. I use it to manipulate the output of the previous command on the shell — it works better if the output is a single line — but I think it could be modified to put the output also on the clipboard... alias clip="xclip -selection clipboard"
`echo hello | clip` will copy 'hello' to the clipboard, and similarly `ls | clip` will copy a bunch of lines to the clipboard. You might need to install xclip through your package manger (the package is just named xclip on Arch). # Paste clipboard into command.
xsel --clipboard | grep foobar
# Copy into clipboard
grep foobar somefile.txt | xsel --clipboard
# Transform clipboard.
xsel --clipboard | tr A-Z a-z | xsel --clipboard
I find this so useful that I have it aliased to `c`. if [[ "$(uname)" == "Linux" ]]; then
if ! grep -q Microsoft /proc/version; then
alias pbcopy='xsel --clipboard --input'
alias pbpaste='xsel --clipboard --output'
else
alias pbcopy='clip.exe'
alias pbpaste='powershell.exe -command "Get-Clipboard"'
fi
fiThis tool looks great for more involved use cases though I think for most scenarios I’d use it for, using the mouse with iTerm2 with “copy on highlight” might be quicker - can be nice to avoid the mouse though!
But if you run VcXsrv you can use the native Linux clipboard and have it shared between WSL 2 and Windows. This is what I do because then tools like Vim, the pass utility and other command line tools can write to and read from the native Linux clipboard and everything will work out of the box.
If you wanted to reproduce pbcopy / pbpaste in native Linux or WSL 2 you can set up aliases for:
alias pbcopy="xclip -selection clipboard"
alias pbpaste="xclip -o"
And in case you don't already have xclip installed in WSL 2, you can apt-get install xclip or use whatever package manager you have. I can't remember if xclip is installed by default in WSL 2 distros.A video walk through of my current Windows / WSL 2 / etc. dev environment is here: https://nickjanetakis.com/blog/a-linux-dev-environment-on-wi...
Sometimes I need the URL itself, as opposed to actually opening it. I wrote elisp to modify the command that displays the URL the cursor is over within Emacs to also use the above method, but use `pbcopy` to pass the URL onto the Mac clipboard.
Two immediate thoughts: one, the naming had me confused for a while. The only place I've seen the word "yank" in computing is in Emacs context, where it means "paste", not "copy"...
Two, natural extension of this idea is "interactively select and transform data in the pipe". I think existing text editors would be natural fit for this, especially if configured with some conveniences (like field-based navigation and ability to delete everything except a particular column/line with one command). Then, you could drop such editor into a pipe using something like [0]. I'm gonna try this with terminal Emacs client at some point.
--
This is a confusing difference between Emacs and Vim. Emacs uses it for pasting, Vim uses it for copying.
(Legitimate question. I'm aware some UX faux-pas do happen, but I don't see how)
I'm wondering if there's a standard way to invoke vim and just get a selection. Might make a good analog for vim users who don't want to commit to using its terminal.
[0] https://sw.kovidgoyal.net/kitty/launch.html
[1]
#: You can pipe the contents of the current screen + history buffer as
#: STDIN to an arbitrary program using the ``pipe`` function. For
#: example, the following opens the scrollback buffer in less in an
#: overlay window::
# map f1 pipe @ansi overlay less +G -R
map f1 pipe @text tab /usr/local/bin/nvim '+ normal GA' -
map f2 pipe @text overlay /usr/local/bin/nvim '+ normal GA' - " bind leader-y and leader-p to clipboard copy/paste
noremap <Leader>y "+y
noremap <Leader>p "+p
(\y or ,y is easier to use than "+y which is the default binding)`ps ux | vim -`, go into visual mode and select the stuff you want, then ,y to copy it to the clipboard.
If you don't use a clipboard manager then of course it will be destroyed once you :q out of vim. You could probably add the :q! to the binding to make it automatically exit vim once you 'confirm' (copy) the selection.
You initialize the plugin and it shows you hints of what to copy, you press the wanted key and it copies the text next to the hint. Works with bunch of things (hashes, urls, numbers) and usually highlights what I want to copy, otherwise tmux with it's normal keyboard shortcuts work fine to manually select things.
$ git push
fatal: The current branch new has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin new
Is there any sane way to copy the perfectly formatted `git push -u` command shown above without resorting to the following eval, which doesn't alias nicely? $(git push 2>&1 | grep git) git push -u origin HEAD alias gpsu='git rev-parse --abbrev-ref HEAD | xargs git push --set-upstream origin'clip.exe does similar on Windows, although much simpler - e.g:
dir /b c:\windows\*.exe |clip
Been an integral part of my workflow for well over a decade!printf "\033]52;c;$(printf "%s" "blabla" | base64)\a"
I have also considered trawling through the various terminal emulator source repos in order to know which are actually implemented (since that can for sure be a different set from those which are specified)
Paradoxically, I find that the terminal is the place where I end up using the mouse the most.