Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs shell command on buffer

Tags:

emacs

elisp

I'd like to set up a function that does the equivalent of marking the whole buffer, and running C-u M-| to prompt for a command, pipe the buffer to the command and replace the buffer with the output. Then maybe set it to shift-f5 or something.

I've only got as far as this:

(defun shell-command-on-buffer ()
  (interactive)
  (mark-whole-buffer))

How can I do the rest?

like image 238
teppic Avatar asked Apr 07 '13 23:04

teppic


People also ask

How do I run a shell command in Emacs?

You can execute an external shell command from within Emacs using ` M-! ' ( 'shell-command' ). The output from the shell command is displayed in the minibuffer or in a separate buffer, depending on the output size.

What is shell command in Emacs?

Emacs has commands for passing single command lines to shell subprocesses, and for running a shell interactively with input and output to an Emacs buffer, and for running a shell in a terminal emulator window. M-! cmd RET. Run the shell command cmd and display the output ( shell-command ).

How do I access Emacs shell?

To enter Emacs, type emacs at the shell prompt. When you want to leave Emacs for a short time, type a C-z and Emacs will be suspended. To get back into Emacs, type %emacs at the shell prompt.

How do I switch buffers in Emacs?

To move between the buffers, type C-x b. Emacs shows you a default buffer name. Press Enter if that's the buffer you want, or type the first few characters of the correct buffer name and press Tab. Emacs fills in the rest of the name.


2 Answers

This works for me:

(defun shell-command-on-buffer (command)
  (interactive "sShell command on buffer: ")
  (shell-command-on-region (point-min) (point-max) command t))
like image 102
brontitall Avatar answered Sep 29 '22 13:09

brontitall


This one has the advantage of using the "shell command" minibuffer history instead of the general minibuffer history.

(defun shell-command-on-buffer ()
  (interactive)
  (shell-command-on-region (point-min) (point-max) (read-shell-command "Shell command on buffer: ") t))
like image 34
killdash9 Avatar answered Sep 29 '22 11:09

killdash9