I'm having trouble working cross-platform with Windows 10 and Linux (Ubuntu), after SSHing into a remote Ubuntu terminal with the SSH client Putty.
What I'm basically trying to do:
In my personal GitHub account, I store and edit some code files (instead storing and editing them on my PC). Some of these files are Bourne shell files (.sh
files).
I desire to copy some Bash code from inside a file I have there in my personal GitHub account, then paste it into a Bash terminal, for direct execution:
Copy code from one of the files in GitHub (non-raw version).
Paste that code in terminal.
Execute that code by hitting Enter in terminal.
The relevant Bash code is the following code (originally taken from here) which contains a Bash script declaration, 2 variable exports, and a Bash function with a cat
here-document, indented with leading tab indentations, along with the function's call:
#!/bin/bash
export user="benqzq"
export repo="ulcwe"
preparations() {
cat <<-EOF >> "$HOME"/.bashrc
export s_a="/etc/nginx/sites-available"
export s_e="/etc/nginx/sites-enabled"
export drt="/var/www/html"
source "$HOME"/"$repo"/software_internal.sh
EOF
source "$HOME"/.bashrc 2>/dev/null
}
preparations
Current state and problem
The code is copied with Windows characters like the Carriage Return (CR
) line delimiter common in Windows, or leading whitespaces that might break execution (depends on the particular code I copy, and in the case of an here-document it sure is in the sense that leading tab indentations turn into a single dot or non-tab spaces, etc, and that's forbidden when working with here-documents).
Desired state
I desire that the code I copy from GitHub into Windows 10 clipboard will be copied without leading spaces and CRs.
Things I've tried:
I failed to find a postprocessing solution in the Linux side (which is basically piping all Bash code in utilities that will filter out Windows characters (like
dos2unix
orsed "s/^[ \t]//g"
).I did found a solution with the AHk TrimClipboard() function that trimmed undesired characters in between copying and sending content to Windows clipboard.
A possible JavaScript approach
Given I failed postprocessing the code from Linux (Bash) side, and given that I don't want to be dependent in AHk, I desire a pure JavaScript vanilla way to copy the GitHub content into Windows clipboard, when it is free of all leading whitespaces and Windows characters (CR in particular).
Is there a way to do such "filtered copying" in vanilla JavaScript? I thought of a Greasemoneky script that will contain a script to do so, aimed to work only inside github.com
.
Notes
- I don't use Git at all, only GitHub.
- I indent with tabs and not regular spaces because that's what needed for heredocuments, and it's also more comfortable for me.
- The end result in Windows clipboard should be the same code, but without any leading spaces / whitespaces / tabs and also without CRs. A raw, CR-less version of the code / pure code.
- The problem I encounter isn't unique to GitHub, it could effect other browser-programs, hence I seek a general and radical approach with JavaScript. Indeed, in this case I'll match only GitHub with Greasemonkey, but I could always match another website if I copy text from there as well, and encounter that problem.