Skip to content

Instantly share code, notes, and snippets.

@osirisgothra
Created December 26, 2022 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osirisgothra/aaa5fb773a4a3bc8aee5a199babff136 to your computer and use it in GitHub Desktop.
Save osirisgothra/aaa5fb773a4a3bc8aee5a199babff136 to your computer and use it in GitHub Desktop.
set clipboard data with an xclip style script
#!/bin/bash
# an Examples of wclip use
# you probably dont want to run this
# copy a list of all files in /etc with size, ownership, date, and permissions included
ls /etc -l | wclip
# copy a list of every positive integer from zero to 100
for ((x=0;x<100;x++)); do echo "$x"; done | wclip
# copy a list of every location of 'somefile' from the current directory and all
# subdirectories included (may take time depending on where you do this)
find -iname 'somefile' | wclip
# copy memory statistics, in kilobytes (1024/unit), to clipboard
free -k | wclip
# copy 'hello wclip' to clipboard :)
echo "hello wclip" | wclip
#!/bin/bash
#
# wclip (recommended name, original is wclip.sh)
# written by Gabriel T. Sharp, Dec 26 2022
# this is a public domain gist
#
# Summary
#
# drop-in replacement for SIMPLE xclip operation of sending text to clipboard
# arguments are ignored, and mimic that of 'xclip -sel -pri' the one exception being the argument '-tst' (see below).
#
# Requirements
#
# 1) Either you must have C:\ mounted on /mnt/c -OR- your subsystem must list the drive letter in the mount table so the regular expression can
# pick it up. (ie; J:\ on /mnt/MyWindowsPartitionJ would be matched), if your mount is in /mount, then you must make a symlink to /mnt
# This process is usually automatic and no intervention is needed. However if your use is exotic, it might not work for you.
# If the drive is not found, the script fails back to /mnt/c and C:\, attempts to check if /mnt/c is writable, and then will go from there.
# You can use the argument -tst to see how and if your drives are detected properly. This has so far been verified to work on cygwin and the
# ubuntu/linux subsystem for Windows 10 and Windows 11.
# 2) You will need windows powershell installed. And it must be executable via 'powershell.exe'. The cmdlets set-clipboard and get-content must be available!
# It is up to you to make sure you have these. Since this script gist is for programmers, its likely you know what you are doing.
# 3) You need a working egrep, mount, mktemp, cat and bash binaries.
# 4) You need either write access to C:\temp, or the privs to make c:\temp, and c:\temp\tmpfile must be available, you can of course change this if you want
# The tmpfile is removed after use.
#
# Notes
#
# The script caller's /dev/stderr (&2) stream is not read. The script caller's /dev/stdout stream is read as input to redirect to the Windows clipboard.
# Errors generated by this script or it's binaries may be output on /dev/stderr or /dev/stdout, be warned.
#
# There are other better scripts for handling xclip more precisely and adding output (getting the clipboard content) instead, but
# they already exist. I wrote this script because the existing scripts I found online did not work seamlessly across versions of linux
# subsystem for windows, cygwin, and other shells (and required the installation of tools or running of the scripts from some weird
# location that didn't sit well with me).
#
# Again please note that there are no xclip arguments, other than the '-tst' argument that tests that drive mappings are compatible.
# This is public domain, so I do not openly support it. Though feel free to drop me a line if you have questions (it might be some
# time before you hear back, depending on where you send your message, my current best contact is through github or 21shariria@gmail.com).
# make sure to subject any questions with "QUESTION:" so I know its not spam. Have a nice day!
#
tmpfile=$(mktemp)
mount | egrep '[A-Z]:\\ on /mnt/[A-Za-z]' -o > $tmpfile
detdrvs=( $(<$tmpfile) )
if [[ -r ${detdrvs[2]} ]] && [[ "${detdrvs[1]}" == "on" ]]; then
declare -g mntloc="${detdrvs[2]}"
declare -g sroot="${detdrvs[0]}"
if [[ "$1" == "-tst" ]]; then
echo "drives were found (test): (mntloc=$mntloc sroot=$sroot)"
declare -p detdrvs
exit 1
fi
else
declare -g mntloc="/mnt/c"
declare -g sroot="c:\\"
if [[ "$1" == "-tst" ]]; then
echo "drives were not found (test): failed back to mntloc=$mntloc and sroot=$sroot"
exit 2
fi
fi
if [[ -w $mntloc ]]; then
[[ -d "$mntloc/temp" ]] || mkdir -f "$mntloc/temp" || echo "warning, error creating $mntloc/temp"
cat > $mntloc/temp/tmpfile
powershell.exe get-content c:\\temp\\tmpfile \| set-clipboard
rm "$mntloc/temp/tmpfile"
else
echo "fatal: this works on subsystems that have main drive mounted in /mnt/c"
echo " please install or link your root drive at this point!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment