I like to use the xclip utility to capture the output of commands on the clipboard. However, when I try to use it with shell-command it seems to hang.

In a "normal" shell I can run a command such as echo "hello world" | xclip -selection clipboard and it will return almost immediately and copy hello world to the clipboard. When I try to run the same command from shell-command the command seems to "hang": the focus stays in the minibuffer and I can't do anything until I cancel the command with C-g. After cancelling the command the text has been copied to the clipboard though.

I'm running emacs 24.4.1 on Debian 8.6

Any idea why the xclip command hangs?

  • use xsel instead – chen bin Feb 23 '18 at 5:32

xclip has to "hang" around to own the clipboard, as X uses asynchronous clipboards which belong to a process. You should be able to get away with just using async-shell-command, which is bound to M-& by default. Another solution would be to use some clipboard manager instead.

  • 2
    Doing some research on xsel led me to some other information that helped clarify things for me. The way xclip is implemented is that it forks a child to manage the copied data and stdout is kept open. It seems that stdout being kept open is the immediate cause for shell-command not returning. Redirecting stdout to /dev/null (e.g. hello world" | xclip -selection clipboard &> /dev/null) causes shell-command to return immediately as expected. – shay Feb 23 '18 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.