by rjeli · 11y ago · 0 comments
function clip { [ -t 0 ] && pbpaste || pbcopy }
This is easier to type and allows for the following: % echo hello | clip
% clip
helloThat's great! Would you please explain why that works? Are there any cases you know of where that doesn't work as expected?
Edit: btw you need a semicolon after pbcopy to make that function valid on one line.
`[` is a Bash built-in command also known as `test`. The `-t` parameter tells it to test if a file descriptor is open for `0`, which is also known as `STDIN`.
So if you're piping something into `STDIN` it will do `pbcopy`. If not -- it will do `pbpaste`.
-t FD file descriptor FD is opened on a terminal
FD 0 is stdin I'd guess. Pretty clever! function clip
if not tty >/dev/null
pbcopy
else
pbpaste
end
endfunction clip() { [ -t 0 ] && pbpaste || pbcopy }
clip() { [ -t 0 ] && pbpaste || pbcopy;}
function clip { [ -t 0 ] && pbpaste || pbcopy;}