From 4d817a4e565aa913aa0ebbe8dc597e5521c9b246 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 19 May 2018 23:08:05 -0400 Subject: add some keybindings to let tmux interact with the system clipboard --- bin/tmux-clipboard | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 bin/tmux-clipboard (limited to 'bin/tmux-clipboard') diff --git a/bin/tmux-clipboard b/bin/tmux-clipboard new file mode 100755 index 0000000..455fa1a --- /dev/null +++ b/bin/tmux-clipboard @@ -0,0 +1,49 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use 5.014; + +use Config; +use Path::Class; +use POSIX qw(mkfifo); + +use constant { + FIFO_PATH => "$ENV{HOME}/.cache/tmux/clipboard/", + COPY_CMD => ($Config{osname} eq 'linux' ? 'xclip -i' : 'pbcopy'), + PASTE_CMD => ($Config{osname} eq 'linux' ? 'xclip -o' : 'pbpaste'), +}; + +my $fifo_file = file(FIFO_PATH, $$); +$fifo_file->dir->mkpath; +$fifo_file->remove; +END { $fifo_file->remove } +mkfifo("$fifo_file", 0700); + +if ($ARGV[0] eq 'copy') { + if (fork) { + open my $clipboard, '|-', COPY_CMD + or die "can't copy from clipboard using ${\COPY_CMD}: $!"; + $clipboard->print($fifo_file->slurp); + $clipboard->close; + } + else { + exec("tmux save-buffer -a $fifo_file"); + } +} +elsif ($ARGV[0] eq 'paste') { + if (fork) { + open my $clipboard, '-|', PASTE_CMD + or die "can't paste from clipboard using ${\PASTE_CMD}: $!"; + my $contents = do { local $/; <$clipboard> }; + $clipboard->close; + $fifo_file->spew(iomode => 'a', $contents); + } + else { + system("tmux load-buffer -b tmux-clipboard $fifo_file"); + exec("tmux paste-buffer -b tmux-clipboard -dp"); + } +} +else { + die "usage: $0 [copy|paste]"; +} + -- cgit v1.2.3-54-g00ecf