Skip to main content
Fix a typo and downcase "Shell"
Source Link
xuchunyang
  • 14.9k
  • 1
  • 22
  • 43

Emacs Command

Instead of select-and-copy manually, you can also write a command and let it do the work for you:

;; Adapted from `comint-delete-output' (defun comint-copy-output () "Copy all output from interpreter since last input." (interactive) (let ((proc (get-buffer-process (current-buffer)))) (save-excursion (let ((pmark (progn (goto-char (process-mark proc)) (forward-line 0) (point-marker)))) (kill-new (buffer-substring comint-last-input-end pmark)))))) 

foo | bar

Your idea can also be achieved by a simple Shellshell script. It assumes Emacs server is running, if it is not, you can start it via the command server-start or run Emacs as a daemon.

#!/bin/bash cat /dev/stdin > /tmp/clip emacsclient --eval '(kill-new (my-file-contents "/tmp/clip"))' 

Becuase it's not easy to embedding Lisp code in Shellshell, my-file-contents is defined in Emacs.

(defun my-file-contents (file) (with-temp-buffer (insert-file-contents file) (buffer-string))) 

Now in shell, % foo | bar should do what you want (assumeingassuming bar is the Shellshell script and it is already in your PATH).

foo > /tmp/kill

In Eshell, you can save output to the kill ring by redirecting output to /dev/kill. You can do the similar in any shell:

  1. redirect output to /tmp/kill
  2. when the contents of /tmp/kill changes, make Emacs copies the new contents to the kill ring

(notes that your Emacs should be compiled with support of watching filesystem, I guess it means (require 'filenotify) should return non-nil)

(defun save-to-kill-ring-if-changes (event) ;; (message "Event %S" event) (when (eq (cadr event) 'changed) (kill-new (my-file-contents "/tmp/kill")))) ;; Create /tmp/kill firstly (write-region "" nil "/tmp/kill") (file-notify-add-watch "/tmp/kill" '(change) 'save-to-kill-ring-if-changes) 

then in any shell, % foo > /tmp/kill should copy the output of foo to the kill ring.

Emacs Command

Instead of select-and-copy manually, you can also write a command and let it do the work for you:

;; Adapted from `comint-delete-output' (defun comint-copy-output () "Copy all output from interpreter since last input." (interactive) (let ((proc (get-buffer-process (current-buffer)))) (save-excursion (let ((pmark (progn (goto-char (process-mark proc)) (forward-line 0) (point-marker)))) (kill-new (buffer-substring comint-last-input-end pmark)))))) 

foo | bar

Your idea can also be achieved by a simple Shell script. It assumes Emacs server is running, if it is not, you can start it via the command server-start or run Emacs as a daemon.

#!/bin/bash cat /dev/stdin > /tmp/clip emacsclient --eval '(kill-new (my-file-contents "/tmp/clip"))' 

Becuase it's not easy to embedding Lisp code in Shell, my-file-contents is defined in Emacs.

(defun my-file-contents (file) (with-temp-buffer (insert-file-contents file) (buffer-string))) 

Now in shell, % foo | bar should do what you want (assumeing bar is the Shell script and it is already in your PATH).

foo > /tmp/kill

In Eshell, you can save output to the kill ring by redirecting output to /dev/kill. You can do the similar in any shell:

  1. redirect output to /tmp/kill
  2. when the contents of /tmp/kill changes, make Emacs copies the new contents to the kill ring

(notes that your Emacs should be compiled with support of watching filesystem, I guess it means (require 'filenotify) should return non-nil)

(defun save-to-kill-ring-if-changes (event) ;; (message "Event %S" event) (when (eq (cadr event) 'changed) (kill-new (my-file-contents "/tmp/kill")))) ;; Create /tmp/kill firstly (write-region "" nil "/tmp/kill") (file-notify-add-watch "/tmp/kill" '(change) 'save-to-kill-ring-if-changes) 

then in any shell, % foo > /tmp/kill should copy the output of foo to the kill ring.

Emacs Command

Instead of select-and-copy manually, you can also write a command and let it do the work for you:

;; Adapted from `comint-delete-output' (defun comint-copy-output () "Copy all output from interpreter since last input." (interactive) (let ((proc (get-buffer-process (current-buffer)))) (save-excursion (let ((pmark (progn (goto-char (process-mark proc)) (forward-line 0) (point-marker)))) (kill-new (buffer-substring comint-last-input-end pmark)))))) 

foo | bar

Your idea can also be achieved by a simple shell script. It assumes Emacs server is running, if it is not, you can start it via the command server-start or run Emacs as a daemon.

#!/bin/bash cat /dev/stdin > /tmp/clip emacsclient --eval '(kill-new (my-file-contents "/tmp/clip"))' 

Becuase it's not easy to embedding Lisp code in shell, my-file-contents is defined in Emacs.

(defun my-file-contents (file) (with-temp-buffer (insert-file-contents file) (buffer-string))) 

Now in shell, % foo | bar should do what you want (assuming bar is the shell script and it is in your PATH).

foo > /tmp/kill

In Eshell, you can save output to the kill ring by redirecting output to /dev/kill. You can do the similar in any shell:

  1. redirect output to /tmp/kill
  2. when the contents of /tmp/kill changes, make Emacs copies the new contents to the kill ring

(notes that your Emacs should be compiled with support of watching filesystem, I guess it means (require 'filenotify) should return non-nil)

(defun save-to-kill-ring-if-changes (event) ;; (message "Event %S" event) (when (eq (cadr event) 'changed) (kill-new (my-file-contents "/tmp/kill")))) ;; Create /tmp/kill firstly (write-region "" nil "/tmp/kill") (file-notify-add-watch "/tmp/kill" '(change) 'save-to-kill-ring-if-changes) 

then in any shell, % foo > /tmp/kill should copy the output of foo to the kill ring.

Source Link
xuchunyang
  • 14.9k
  • 1
  • 22
  • 43

Emacs Command

Instead of select-and-copy manually, you can also write a command and let it do the work for you:

;; Adapted from `comint-delete-output' (defun comint-copy-output () "Copy all output from interpreter since last input." (interactive) (let ((proc (get-buffer-process (current-buffer)))) (save-excursion (let ((pmark (progn (goto-char (process-mark proc)) (forward-line 0) (point-marker)))) (kill-new (buffer-substring comint-last-input-end pmark)))))) 

foo | bar

Your idea can also be achieved by a simple Shell script. It assumes Emacs server is running, if it is not, you can start it via the command server-start or run Emacs as a daemon.

#!/bin/bash cat /dev/stdin > /tmp/clip emacsclient --eval '(kill-new (my-file-contents "/tmp/clip"))' 

Becuase it's not easy to embedding Lisp code in Shell, my-file-contents is defined in Emacs.

(defun my-file-contents (file) (with-temp-buffer (insert-file-contents file) (buffer-string))) 

Now in shell, % foo | bar should do what you want (assumeing bar is the Shell script and it is already in your PATH).

foo > /tmp/kill

In Eshell, you can save output to the kill ring by redirecting output to /dev/kill. You can do the similar in any shell:

  1. redirect output to /tmp/kill
  2. when the contents of /tmp/kill changes, make Emacs copies the new contents to the kill ring

(notes that your Emacs should be compiled with support of watching filesystem, I guess it means (require 'filenotify) should return non-nil)

(defun save-to-kill-ring-if-changes (event) ;; (message "Event %S" event) (when (eq (cadr event) 'changed) (kill-new (my-file-contents "/tmp/kill")))) ;; Create /tmp/kill firstly (write-region "" nil "/tmp/kill") (file-notify-add-watch "/tmp/kill" '(change) 'save-to-kill-ring-if-changes) 

then in any shell, % foo > /tmp/kill should copy the output of foo to the kill ring.