programing tip

Emacs에서 Linux의 다른 응용 프로그램으로 텍스트를 복사하는 방법

itbloger 2020. 8. 9. 10:11
반응형

Emacs에서 Linux의 다른 응용 프로그램으로 텍스트를 복사하는 방법


Emacs 22.1.1 (X의 자체 창, KDE, Kubuntu의 경우)에서 텍스트를 잘라내 (죽일) 때 다른 응용 프로그램에 붙여 넣을 수 없습니다.


여기서 우리의 정의에주의합시다

  • 이맥스 복사 명령입니다 kill-ring-save(일반적으로 결합 M-w).
  • 시스템 사본은 당신이 일반적으로 눌러에서 무엇을 얻을 것입니다 C-c(또는 응용 프로그램 창에서 "편집 -> 복사"를 선택).
  • X 복사는 "물리적으로"마우스 커서 텍스트를 강조한다.
  • 이맥스 붙여 넣기 명령입니다 yank(일반적으로 결합 C-y).
  • 시스템 페이스트 는 일반적으로 눌러에서 무엇을 얻을 것입니다 C-v(또는 응용 프로그램 창에 "편집 - 붙여 넣기"를 선택).
  • X 페이스트 (함께 왼쪽 및 오른쪽 마우스 버튼을 눌러 시뮬레이션) "센터 마우스 버튼"을 누르면된다.

제 경우 (GNOME에서) :

  • Emacs와 시스템 복사는 일반적으로 X 붙여 넣기와 함께 작동합니다.
  • X 복사는 보통 Emacs 붙여 넣기와 함께 작동합니다.
  • Emacs 붙여 넣기를 사용하여 시스템 복사 작업을 수행하고 시스템 붙여 넣기를 사용하여 Emacs 복사 작업을 수행 (setq x-select-enable-clipboard t)하려면 .emacs. 또는 시도

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

나는 이것이 꽤 표준적인 현대 유닉스 동작이라고 생각합니다.

Emacs가 콘솔에서 실행 중일 때 시스템 및 X 클립 보드에서 완전히 분리된다는 점 (별도의 창에서 Emacs를 사용한다고하더라도)에 유의하는 것도 중요합니다.이 경우 잘라 내기 및 붙여 넣기는 터미널에 의해 조정됩니다. . 예를 들어, 터미널 창에서 "Edit-> Paste"는 클립 보드의 텍스트를 Emacs 버퍼에 입력 한 것과 똑같이 작동해야합니다.


.emacs파일에 다음을 삽입 하십시오.

(setq x-select-enable-clipboard t)

Emacs에서 복사 및 붙여 넣기의 어려움은 내부 kill / yank와 독립적으로 작동하고 터미널과 GUI 모두에서 작동하기를 원한다는 것입니다. 터미널 또는 GUI에 대한 기존의 강력한 솔루션이 있지만 둘다는 아닙니다. 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)

내 .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.

Bonus: to get these things to happen in Emacs without having to reload your whole .emacs, do C-x C-e with the cursor just after the close paren of each of those expressions in the .emacs buffer.

Good luck!


I assume by emacs you are meaning Emacs under X (ie not inside a terminal window).

There are two ways:

  1. (Applies to unix OS's only) Highlight the desired text with your mouse (this copies it to the X clipboard) and then middle click to paste.
  2. Highlight the desired text and then "M-x clipboard-kill-ring-save" (note you can bind this to an easier key). Then just "Edit->Paste" in your favorite app.

Clipboard operations available:

  • clipboard-kill-ring-save -- copy selection from Emacs to clipboard
  • clipboard-kill-region -- cut selection from Emacs to clipboard
  • clipboard-yank -- paste from clipboard to Emacs

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


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 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)

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.....


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.


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)))

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.

참고URL : https://stackoverflow.com/questions/64360/how-to-copy-text-from-emacs-to-another-application-on-linux

반응형