Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

It's common for me to run a throwaway piece of code I'm not particularly interested in saving on my disk, so I'm often working inside emacs in a buffer not attached to any file in my system.

When working with interpreted (I'm a little reluctant to use this term) languages like Python, Scheme or Lua, each respective major mode (the common ones) offer ways of running the REPL/interpreter inside the editor and executing the content of a buffer directly. (Generally with a function called send-buffer or something like that).

However when working with C, I cannot find a similar functionality. I find myself forced to save the file to disk, in order to give gcc a filename as argument.

Is there a way to send the content of an emacs buffer to gcc without writing the file to disk?

share|improve this question
I'm not that familiar with emacs, but gcc can be instructed to compile from stdin using "gcc -x c -o tmp_prog -" – Kotte Feb 7 '13 at 15:43
up vote 3 down vote accepted

Use C-x h (mark-whole-buffer), then'M-| (shell-command-on-region), then use @Kotte's suggestion above to run gcc -x c -o tmp_prog - && tmp_prog

Or here's an elisp function that does it:

(defun compile-and-run-buffer()
  "Compiles the code in the buffer using gcc, run the exe and remove it."
   "gcc -x c -o emacs_tmp - && ./emacs_tmp && rm emacs_tmp"))
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.