[英]How to copy text from Emacs to another application on Linux
当我在Emacs 22.1.1中剪切(杀死)文本时(在X中,在KDE中,在Kubuntu上的自己的窗口中),我无法在任何其他应用程序中粘贴(猛拉)它。
Let's be careful with our definitions here 我们在这里要小心我们的定义
kill-ring-save
(usually bound to Mw ). kill-ring-save
(通常绑定到Mw )。 yank
(usually bound to Cy ). yank
(通常绑定到Cy )。 In my case (on GNOME): 就我而言(在GNOME上):
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
. 要使系统复制与Emacs粘贴和Emacs复制一起使用系统粘贴,您需要将
(setq x-select-enable-clipboard t)
到.emacs
。 Or try 或者试试
META-X set-variable RET x-select-enable-clipboard RET t
I think this is pretty standard modern Unix behavior. 我认为这是非常标准的现代Unix行为。
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. 同样重要的是要注意(虽然你说你在一个单独的窗口中使用Emacs),当Emacs在控制台中运行时,它完全脱离系统和X剪贴板:在这种情况下剪切和粘贴由终端调解。 For example, "Edit->Paste" in your terminal window should act exactly as if you typed the text from the clipboard into the Emacs buffer.
例如,终端窗口中的“编辑 - >粘贴”应该与将剪贴板中的文本键入Emacs缓冲区的方式完全相同。
将以下内容插入.emacs
文件:
(setq x-select-enable-clipboard t)
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. 在Emacs中复制和粘贴的难点在于你希望它独立于内部kill / yank工作,并且你希望它在终端和gui中都能工作。 There are existing robust solutions for either terminal or gui, but not both.
现有终端或gui的强大解决方案,但不是两者兼而有之。 After installing xsel (eg
sudo apt-get install xsel
), here is what I do for copy and paste to combine them: 安装xsel之后(例如
sudo apt-get install xsel
),这里是我复制和粘贴它们的组合:
(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)
I stick this in my .emacs: 我把它粘在我的.emacs中:
(setq x-select-enable-clipboard t)
(setq interprogram-paste-function 'x-cut-buffer-or-selection-value)
I subsequently have basically no problems cutting and pasting back and forth from anything in Emacs to any other X11 or Gnome application. 我随后在从Emacs中的任何东西到任何其他X11或Gnome应用程序中来回切换和粘贴时基本没有问题。
Bonus: to get these things to happen in Emacs without having to reload your whole .emacs, do Cx Ce with the cursor just after the close paren of each of those expressions in the .emacs buffer. 额外:要让Emacs中的这些事情发生而不必重新加载整个.emacs,请在.emacs缓冲区中每个表达式的紧密关注之后用光标执行Cx Ce。
Good luck! 祝好运!
I assume by emacs you are meaning Emacs under X (ie not inside a terminal window). 我假设你通过emacs表示X下的Emacs(即不在终端窗口内)。
There are two ways: 有两种方法:
Clipboard operations available: 可用的剪贴板操作:
有一篇EmacsWiki文章解释了在X下复制和粘贴的一些问题以及如何配置它来工作。
This works with Mw
on Mac OSX. 这适用于Mac OSX上的
Mw
。 Just add to your .emacs file. 只需添加到.emacs文件即可。
(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 来源https://gist.github.com/the-kenny/267162
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 Cy, windows Cx). 下面的代码受@ RussellStewart上面的回答启发,增加了对x-PRIMARY和x-SECONDARY的支持,用
use-region-p
替换region-active-p
use-region-p
来覆盖空区域的情况,如果xsel有,则不会静默返回尚未安装(返回错误消息),并包含“剪切”功能(emacs Cy,windows Cx)。
(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 Cx Cw
and Cx Cy
be copy and paste on both Mac and Linux (if someone knows the version for Windows feel free to add it). 我根据这里的其他答案使用以下内容,使
Cx Cw
和Cx Cy
在Mac和Linux上都可以复制和粘贴(如果有人知道Windows的版本可以随意添加它)。 Note that on Linux you will have to install xsel and xclip with your package manager. 请注意,在Linux上,您必须使用软件包管理器安装xsel和xclip。
;; 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)))
Hmm, what platform and what version of emacs are you using? 嗯,你使用什么平台和什么版本的emacs? With GNU Emacs 22.1.1 on Windows Vista, it works fine for me.
使用Windows Vista上的GNU Emacs 22.1.1,它对我来说很好。
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..... 如果你有任何机会通过RealVNC查看器从windows到linux这样做,请确保你首先在linux机器上运行“vncconfig -iconic”.....
I always use quick paste -- drag selection in emacs, hit the middle mouse button in target window. 我总是在emacs中使用快速粘贴 - 拖动选择,在目标窗口中点击鼠标中键。
(From the reference to kate, I take it you're on linux or similar and probably using emacs in X one way or another.) (从参考kate,我认为你是在Linux或类似的,并可能在X中以某种方式使用emacs。)
You might want to specify what platform you are using. 您可能想要指定您正在使用的平台。 Is it on linux, unix, macosx, windows, ms-dos?
它是在linux,unix,macosx,windows,ms-dos上吗?
I believe that for windows it should work. 我相信对于Windows它应该工作。 For MacOSX it will get added to the x-windows clipboard, which isn't the same thing as the macosx clipboard.
对于MacOSX,它将被添加到x-windows剪贴板,这与macosx剪贴板不同。 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.
对于Linux,它取决于你的窗口管理器的风格,但我相信x-windows在大多数情况下以一种很好的方式处理它。
So, please specify. 所以,请说明。
What I do is to use a good terminal tool (PuTTY on Windows, Konsole or Terminal on Linux) that has copy facilities built-in. 我所做的是使用内置复制功能的好终端工具(Windows上的PuTTY,Linux上的Konsole或终端)。
In PuTTY, you highlight the text you want with the mouse and then paste it elsewhere. 在PuTTY中,您可以使用鼠标突出显示所需的文本,然后将其粘贴到其他位置。 Right-clicking in a PuTTY window pastes the contents of the Windows copy/paste buffer.
在PuTTY窗口中单击鼠标右键可粘贴Windows复制/粘贴缓冲区的内容。
In Konsole or Terminal on Linux, you highlight what you want then press Shift+Ctrl+C for copy and Shift+Ctrl+V for paste. 在Linux上的Konsole或终端中,突出显示所需内容,然后按Shift + Ctrl + C进行复制,按Shift + Ctrl + V进行粘贴。
In the win32 compile of emacs, yanking text does put it on the copy/paste buffer .. most of the time. 在emacs的win32编译中,yanking文本确实将它放在复制/粘贴缓冲区中..大部分时间。
On Mac OS X, the Apple-key chortcuts work fine, because Terminal traps them. 在Mac OS X上,Apple-key chortcuts工作正常,因为终端会捕获它们。
There is no direct way of doing it on the commandline because the shell does not maintain a copy/paste buffer for each application. 在命令行上没有直接的方法,因为shell不为每个应用程序维护一个复制/粘贴缓冲区。 bash does maintain a copy/paste buffer for itself, and, by default, emacs ^k/^y shortcuts work.
bash 会为自己维护一个复制/粘贴缓冲区,默认情况下,emacs ^ k / ^ y快捷方式可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.