Is there some way to run whatever you copy to the ‘clipboard’ through some sort of filter? Ideally to strip out the trailing newline from something you copy, so that it doesn’t auto-run in the terminal?
This is what it looks like when I copy the test in question, and I sometimes forget this is a new line.

Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Good modern terminals support bracketed paste: when you use the terminal’s paste command, it sends special escape sequences around the clipboard content. If your shell supports bracketed paste, it’ll paste the clipboard content including any control characters as-is, and in particular a trailing newline will not trigger the execution of the command.
Zsh ≥5.1 supports bracketed paste and has it on by default. Older versions can be taught. Bash ≥4.4 supports bracketed paste if you add set enable-bracketed-paste on to ~/.inputrc.
If your terminal or shell doesn’t support bracketed paste, you could define a shell function that pastes without the trailing newline.
In zsh, the following command recalls the content of the clipboard, minus trailing newlines, and brings it up for editing (even if there are multiple lines):
print -z -- "`xsel -b`"
In bash, you can push the content of the clipboard minus trailing newlines to the history stack. After this, press Up to bring up the command for editing.
history -s -- "`xsel -b`"
Method 2
Yes, use cat to paste in a file, do whatever you like and then execute it.
For this case:
$ cat > tmp
[paste][Ctrl+D]
$ tr -d 'n' <tmp | xclip # this remove all "new line" characters
# and copy back to clipboard
Method 3
This makes ev insert the clipboard without newlines in bash 4.0 and later:
pasteline() {
local input=$(xsel -b)
input=${input//$'n'}
READLINE_LINE=${READLINE_LINE:0:$READLINE_POINT}$input${READLINE_LINE:$READLINE_POINT}
READLINE_POINT=$((READLINE_POINT+${#input}))
}
bind -x '"ev": pasteline'
Replace xsel -b with pbpaste and install bash 4 in OS X.
Method 4
I realized we can do it in one line, inspired from @RSFalcon7 answer
by using xsel, to copy to primary selection
cat | tr -d 'n' | xsel [paste], 2 times[Ctrl-D]
Method 5
You can use a here document to queue up your pastes and then process the input to remove the unwanted chars.
So you would do …
args=$(tr 'n' ' ' << EOI
[PASTE]
[PASTE]
[PASTE]
EOI
)
Your desired output is saved in $args.
An full example:
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="88fae7e7fcc8faf8e5a5fbedfafeedfa">[email protected]</a> feature]# args=$(tr 'n' ' ' << EOI
../../root/bin/gitLatestTag.py
../../root/bin/sri-build-rpm
../../root/bin/sriBuild.py_orig
../../root/bin/sriBuild2.py
EOI
)
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="70021f1f043002001d5d031502061502">[email protected]</a> feature]# echo $args
../../root/bin/gitLatestTag.py ../../root/bin/sri-build-rpm ../../root/bin/sriBuild.py_orig ../../root/bin/sriBuild2.py
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1a6875756e5a686a7737697f686c7f68">[email protected]</a> feature]#
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0