16

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.
enter image description here

0

7 Answers 7

15

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`"
0
5

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
1
  • And how could I use it on GUI level when I copy some command from browser (like Stack answer) to my shell... In fact I ruined my OS yesterday because of such copy-paste :) Commented Feb 12, 2018 at 14:26
3

Since version 0.13 (released 2016-09), xclip seems to have an undocumented option -r, which does exactly what is needed: removes the very last newline character from the text. See https://github.com/astrand/xclip/issues/7. At least it works for me on Ubuntu 22.04.

2

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.

2

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]
1

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:

[root@rpm-server feature]# args=$(tr '\n' ' ' << EOI
        ../../root/bin/gitLatestTag.py
        ../../root/bin/sri-build-rpm
        ../../root/bin/sriBuild.py_orig
        ../../root/bin/sriBuild2.py
EOI
)
[root@rpm-server feature]# echo $args
../../root/bin/gitLatestTag.py ../../root/bin/sri-build-rpm ../../root/bin/sriBuild.py_orig ../../root/bin/sriBuild2.py
[root@rpm-server feature]#
0

I personally use

alias copy='xargs echo -n | xsel -ib'
alias paste='xsel -ob | xargs echo -n'

in my bashrc you can pipe to and from these aliases

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.