Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

emacs keybinding to compile C file

Tags:

c

emacs

Here's what I would like to do: When C-c C-l is pressed, a new terminal window is launched if there is no terminal window already, then, in that terminal, gcc is invoked with some flags and the current buffer's file. How would I do that?

like image 390
Igor Avatar asked Aug 28 '10 20:08

Igor


1 Answers

Try the built in:

M-x compile gcc ...

compile isn't bound to any keys by default, but you could do something like:

(add-hook 'c-mode-common-hook 
          (lambda () (define-key c-mode-base-map (kbd "C-c C-l") 'compile))))

There are a bunch of packages people have written around compiling on the Emacs Wiki. Check out SmartCompile, CompileCommand, and the category Programmer Utils.

The benefit of using M-x compile ... over just running it in a "terminal" is that you get C-x ` (aka next-error) which will jump you to the file and line that caused the error command. And there's the command M-x recompile which does what you'd expect. And, of course, like all Emacs commands, the compile command keeps a history of the compile calls and you can go through the history with M-p and M-n.

like image 123
Trey Jackson Avatar answered Sep 22 '22 13:09

Trey Jackson