xclip and trailing spaces

926 Views Asked by At

I use xclip to get my current path in my clipboard this way :

pwd | xclip -selection c

it almost works : if you paste (ctrl v) in an empty file, you will see that there is a trailing carriage return. It's very annoying since if you past in a term, then it immediately executes your expression, even if you did not finished to type.

The problem is the same with :

echo "titi" | xclip -selection c
  • is it due to xclip ?
  • Why xclip would add a trailing carriage return ?
  • Is there a carriage return at the end of every string in bash ?
2

There are 2 best solutions below

0
On

The problem comes from the commands that add a new line character (\n) at the end of their output, pwd is one of these, as is echo (without -n).

One solution is to strip the new line out of the text before sending it to xclip, for instance :

pwd | tr -d "\n" | xclip -selection c

Here we use the tr tool to remove the new line.

0
On

Gniourf_gniourf answer is perfect : in .bahs_aliases :

alias myxclip='printf %s "$(< /dev/stdin)" | xclip -selection c'

then you can redirect the standard output to your clipboard :

pwd | myxclip
ls -al | myxclip
...