2010-02-01 63 views
20

我想複製一個字符串到剪貼板(不是任何特定的緩衝區,只是一個普通的字符串)。如果它也被添加到殺死環中將會很好。這裏有一個例子:如何複製到剪貼板在Emacs Lisp

(copy-to-clipboard "Hello World")

,這種功能存在嗎?如果是這樣,那叫什麼,你是怎麼找到它的?是否還有paste-from-clipboard函數?

我似乎無法在Lisp參考手冊中找到這個東西,所以請告訴我你是如何找到它的。

+1

你可能會發現這個問題有幫助:http://stackoverflow.com/questions/64360 – 2010-02-01 19:38:29

回答

36

您正在查找kill-new

kill-new is a compiled Lisp function in `simple.el'. 

(kill-new string &optional replace yank-handler) 

Make string the latest kill in the kill ring. 
Set `kill-ring-yank-pointer' to point to it. 
If `interprogram-cut-function' is non-nil, apply it to string. 
Optional second argument replace non-nil means that string will replace 
the front of the kill ring, rather than being added to the list. 

Optional third arguments yank-handler controls how the string is later 
inserted into a buffer; see `insert-for-yank' for details. 
When a yank handler is specified, string must be non-empty (the yank 
handler, if non-nil, is stored as a `yank-handler' text property on string). 

When the yank handler has a non-nil PARAM element, the original string 
argument is not used by `insert-for-yank'. However, since Lisp code 
may access and use elements from the kill ring directly, the string 
argument should still be a "useful" string for such uses. 
+1

看起來很有前途。你是怎麼找到這個的? – User1 2010-02-01 18:23:45

+0

很多年前我都需要它。不記得我是如何找到它的。 – 2010-02-01 18:24:31

+0

那麼,(殺死新的「Hello World」)效果很好。謝謝! – User1 2010-02-01 20:07:45

3

我這樣做:

(with-temp-buffer 
    (insert "Hello World") 
    (clipboard-kill-region (point-min) (point-max))) 

這得到它在剪貼板上。如果你想在殺死環上添加一個kill-region表單。

+0

copy-region-as-kill'可以一次複製到剪貼板和kill-ring: 'copy-region-as-kill是一個交互式編譯的Lisp函數,在 'simple.el'中。 (copy-region-as-kill beg end) 保存區域就好像被殺死一樣,但不要殺死它。 在瞬態標記模式下,取消標記。 如果'interprogram-cut-function'非零,那麼還要保存系統剪切和粘貼窗口的文本。 – 2010-02-01 18:26:50

0

將您的選擇放在窗口系統剪貼板上的命令是x-select-text。你可以給它一段文字來記住。所以(buffer-substring (point) (mark))什麼都應該給你你需要傳遞給它的東西。在Joe的回答中,您可以看到跨程序切換功能。看看如何找到這個。

0

在我.emacs文件,我用這個

(global-set-key "\C-V" 'yank) 
(global-set-key "\C-cc" 'kill-ring-save) 

我不能使用Ctrl-C(或系統複製),但這種可能的情況下,舊習慣踢是不夠的。