How to copy text from Emacs to another application on Linux

When I cut (kill) text in Emacs 22.1.1 (in its own window on X, in KDE, on Kubuntu), I can't paste (yank) it in any other application.

74263 次浏览

Hmm, what platform and what version of emacs are you using? With GNU Emacs 22.1.1 on Windows Vista, it works fine for me.

If, by any chance, you are doing this from windows to linux through a RealVNC viewer, make sure you are running "vncconfig -iconic" on the linux box first.....

What I do is to use a good terminal tool (PuTTY on Windows, Konsole or Terminal on Linux) that has copy facilities built-in.

In PuTTY, you highlight the text you want with the mouse and then paste it elsewhere. Right-clicking in a PuTTY window pastes the contents of the Windows copy/paste buffer.

In Konsole or Terminal on Linux, you highlight what you want then press Shift+Ctrl+C for copy and Shift+Ctrl+V for paste.

In the win32 compile of emacs, yanking text does put it on the copy/paste buffer .. most of the time.

On Mac OS X, the Apple-key chortcuts work fine, because Terminal traps them.

There is no direct way of doing it on the commandline because the shell does not maintain a copy/paste buffer for each application. bash does maintain a copy/paste buffer for itself, and, by default, emacs ^k/^y shortcuts work.

I always use quick paste -- drag selection in emacs, hit the middle mouse button in target window.

(From the reference to kate, I take it you're on linux or similar and probably using emacs in X one way or another.)

You might want to specify what platform you are using. Is it on linux, unix, macosx, windows, ms-dos?

I believe that for windows it should work. For MacOSX it will get added to the x-windows clipboard, which isn't the same thing as the macosx clipboard. For Linux, it depends on your flavour of window manager, but I believe that x-windows handles it in a nice way on most of them.

So, please specify.

There is an EmacsWiki article that explains some issues with copy & pasting under X and how to configure it to work.

selection from Emacs to clipboard

Another one using Microsoft Moles (Isolation framework for .NET).

MDateTime.NowGet = () => new DateTime(2000, 1, 1);

Moles allows to replace any .NET method with a delegate. Moles supports

  • clipboard-yank -- paste from clipboard to Emacs
  • public string WhatsTheTime() { return DateTime.Now.ToString();

    Simple answer: ditch System.DateTime :) Instead, use NodaTime and it's testing library: NodaTime.Testing.

    } }

    In Visual Studio 2012 you can add a Fakes assembly to your test project by right clicking on the assembly you want to create Fakes/Shims for and selecting "Add Fakes Assembly"

    Adding Fakes Assembly

    Finally, Here is what the test class would look like:

    using System;
    using ConsoleApplication11;
    using Microsoft.QualityTools.Testing.Fakes;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    
    
    namespace DateTimeTest
    {
    [TestClass]
    public class UnitTest1
    {
    [TestMethod]
    public void TestWhatsTheTime()
    {
    
    
    using(ShimsContext.Create()){
    
    
    //Arrange
    System.Fakes.ShimDateTime.NowGet =
    () =>
    { return new DateTime(2010, 1, 1); };
    
    
    var myClass = new MyClass();
    
    
    //Act
    var timeString = myClass.WhatsTheTime();
    
    
    //Assert
    Assert.AreEqual("1/1/2010 12:00:00 AM",timeString);
    
    
    }
    }
    }
    }
    
  • An X paste is pressing the "center mouse button" (simulated by pressing the left and right mouse buttons together).
  • I faced this situation so often, that I created simple nuget which exposes Now property through interface.

    public interface IDateTimeTools
    {
    DateTime Now { get; }
    }
    

    In my case (on GNOME):

      Implementation is of course very straightforward

      public class DateTimeTools : IDateTimeTools
      {
      public DateTime Now => DateTime.Now;
      }
      
    • Both Emacs and system copy usually work with X paste.
    • X copy usually works with Emacs paste.
    • So after adding nuget to my project I can use it in the unit tests

      enter image description here

    • To make system copy work with Emacs paste and Emacs copy work with system paste, you need to add (setq x-select-enable-clipboard t) to your .emacs. Or try

      META-X set-variable RET x-select-enable-clipboard RET t
      

    I think this is pretty standard modern Unix behavior.

    It's also important to note (though you say you're using Emacs in a separate window) that when Emacs is running in a console, it is completely divorced from the system and X clipboards: cut and paste in that case is mediated by the terminal. For example, "Edit->Paste" in your terminal window should act exactly as if you typed the text from the clipboard into the Emacs buffer.

    Good luck!

    How can I convert a uniform distribution (as most random number generators produce, e.g. between 0.0 and 1.0) into a normal distribution? What if I want a mean and standard deviation of my choosing?

    The difficulty with copy and paste in Emacs is that you want it to work independently from the internal kill/yank, and you want it to work both in terminal and the gui. There are existing robust solutions for either terminal or gui, but not both. After installing xsel (e.g. sudo apt-get install xsel), here is what I do for copy and paste to combine them:

    (defun copy-to-clipboard ()
    (interactive)
    (if (display-graphic-p)
    (progn
    (message "Yanked region to x-clipboard!")
    (call-interactively 'clipboard-kill-ring-save)
    )
    (if (region-active-p)
    (progn
    (shell-command-on-region (region-beginning) (region-end) "xsel -i -b")
    (message "Yanked region to clipboard!")
    (deactivate-mark))
    (message "No region active; can't yank to clipboard!")))
    )
    
    
    (defun paste-from-clipboard ()
    (interactive)
    (if (display-graphic-p)
    (progn
    (clipboard-yank)
    (message "graphics active")
    )
    (insert (shell-command-to-string "xsel -o -b"))
    )
    )
    
    
    (global-set-key [f8] 'copy-to-clipboard)
    (global-set-key [f9] 'paste-from-clipboard)
    

    This works with M-w on Mac OSX. Just add to your .emacs file.

    (defun copy-from-osx ()
    (shell-command-to-string "pbpaste"))
    (defun paste-to-osx (text &optional push)
    (let ((process-connection-type nil))
    (let ((proc (start-process "pbcopy" "*Messages*" "pbcopy")))
    (process-send-string proc text)
    (process-send-eof proc))))
    
    
    (setq interprogram-cut-function 'paste-to-osx)
    (setq interprogram-paste-function 'copy-from-osx)
    

    Source https://gist.github.com/the-kenny/267162

    The Ziggurat algorithm is pretty efficient for this, although the Box-Muller transform is easier to implement from scratch (and not crazy slow).

    The code below, inspired by @RussellStewart's answer above, adds support for x-PRIMARY and x-SECONDARY, replaces region-active-p with use-region-p to cover the case of an empty region, does not return silently if xsel has not been installed (returns an error message), and includes a "cut" function (emacs C-y, windows C-x).

    (defun my-copy-to-xclipboard(arg)
    (interactive "P")
    (cond
    ((not (use-region-p))
    (message "Nothing to yank to X-clipboard"))
    ((and (not (display-graphic-p))
    (/= 0 (shell-command-on-region
    (region-beginning) (region-end) "xsel -i -b")))
    (error "Is program `xsel' installed?"))
    (t
    (when (display-graphic-p)
    (call-interactively 'clipboard-kill-ring-save))
    (message "Yanked region to X-clipboard")
    (when arg
    (kill-region  (region-beginning) (region-end)))
    (deactivate-mark))))
    
    
    (defun my-cut-to-xclipboard()
    (interactive)
    (my-copy-to-xclipboard t))
    
    
    (defun my-paste-from-xclipboard()
    "Uses shell command `xsel -o' to paste from x-clipboard. With
    one prefix arg, pastes from X-PRIMARY, and with two prefix args,
    pastes from X-SECONDARY."
    (interactive)
    (if (display-graphic-p)
    (clipboard-yank)
    (let*
    ((opt (prefix-numeric-value current-prefix-arg))
    (opt (cond
    ((=  1 opt) "b")
    ((=  4 opt) "p")
    ((= 16 opt) "s"))))
    (insert (shell-command-to-string (concat "xsel -o -" opt))))))
    
    
    (global-set-key (kbd "C-c C-w") 'my-cut-to-xclipboard)
    (global-set-key (kbd "C-c M-w") 'my-copy-to-xclipboard)
    (global-set-key (kbd "C-c C-y") 'my-paste-from-xclipboard)
    

    I use the following, based on the other answers here, to make C-x C-w and C-x C-y be copy and paste on both Mac and Linux (if someone knows the version for Windows feel free to add it). Note that on Linux you will have to install xsel and xclip with your package manager.

    ;; Commands to interact with the clipboard
    
    
    (defun osx-copy (beg end)
    (interactive "r")
    (call-process-region beg end  "pbcopy"))
    
    
    (defun osx-paste ()
    (interactive)
    (if (region-active-p) (delete-region (region-beginning) (region-end)) nil)
    (call-process "pbpaste" nil t nil))
    
    
    (defun linux-copy (beg end)
    (interactive "r")
    (call-process-region beg end  "xclip" nil nil nil "-selection" "c"))
    
    
    (defun linux-paste ()
    (interactive)
    (if (region-active-p) (delete-region (region-beginning) (region-end)) nil)
    (call-process "xsel" nil t nil "-b"))
    
    
    (cond
    ((string-equal system-type "darwin") ; Mac OS X
    (define-key global-map (kbd "C-x C-w") 'osx-copy)
    (define-key global-map (kbd "C-x C-y") 'osx-paste))
    ((string-equal system-type "gnu/linux") ; linux
    (define-key global-map (kbd "C-x C-w") 'linux-copy)
    (define-key global-map (kbd "C-x C-y") 'linux-paste)))