1

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 ?
Benibur
  • 816
  • 10
  • 9
  • 1
    It's because you're using `echo`, that adds a trailing newline. To fix this, you may use `echo -n` or, much better, use `printf`: `printf '%s' "$PWD" | xclip -selection c`. – gniourf_gniourf Jan 03 '15 at 11:04
  • Great ! so I am now looking for an alias such as alias xclip='xclip -selection c' but which would work with pwd | xclip and xclip myfile.txt – Benibur Jan 03 '15 at 15:15
  • I tried different solutions without success because the difference between printf '%s' `pwd` and pwd | printf '%s' ?? – Benibur Jan 03 '15 at 15:25
  • 3
    If you want such an alias, `alias myxclip='printf %s "$(< /dev/stdin)" | xclip -selection c'`. – gniourf_gniourf Jan 03 '15 at 20:35
  • excellent, it works, it will be long before I understand your syntax, many thanks ! – Benibur Jan 15 '15 at 11:03
  • Seriously helpful – Liso Jan 18 '22 at 13:20

2 Answers2

3

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.

Alex D.
  • 41
  • 5
0

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
...
Benibur
  • 816
  • 10
  • 9