Search code examples
shellemacsxclip

Emacs shell-command with xclip hangs when copying to clipboard


I'm encountering an issue where running xclip through shell-command in Emacs to copy text to the clipboard hangs. Here's the minimal code snippet that reproduces the problem:

(shell-command "echo foobar | xclip -selection clipboard")

When I execute this, Emacs freezes. Running the same xclip command directly in a terminal works without any issues.

I've tried various workarounds, such as using the -i option with xclip, redirecting stderr to /dev/null and executing xclip in the background with nohup and &, but none have resolved the issue.

I'm running Emacs on Manjaro with Emacs 29.1. Any insights or solutions to prevent Emacs from hanging when using xclip in this way would be greatly appreciated.


Solution

  • I was curious and played a bit with this problem. Using nohup in conjunction with shell-command seems to do what you want:

    (shell-command 
      "echo nowitworks | nohup xclip -selection clipboard > /dev/null 2>&1")
    

    nohup would normaly create a file nohup.out, the redirections are there to prevent that.