1

Is it possible to bind the compile and run commands for a c++ code to a single key (for example F5). What i do at the moment is to run M-x compile and then as compile command g++ project.cpp and then i run ./a.out in a shell. Does anyone has a solution for doing these with less typing for c++?

4

You need to do two things: 1. Bind compile to a key (like f5) 2. Change the command that compile runs.

To do the first add (define-key c++-mode-map [f5] #'compile) to your .emacs. This sets the key only in c++-mode; you can make it a global binding if you want by using global-map instead.

For the second, the compile function just runs whatever is stored in the variable compile-command. Changing this globally would be a pain as you probably want different commands for different projects. File local variables to the rescue. Open the file and run M-x add-file-local-variable. Chose compile-command as the variable and "g++ project.cpp && ./a.out" as the value (don't forget the quotes). This will add a block

// Local Variables:
// compile-command: "g++ test.cpp && ./a.out"
// End:

to the bottom of your file. You can easily add this block yourself if you like.

If your project has multiple files, use add-dir-local-variable instead. Directory local variables are used by all files in that directory.

  • After adding the line to my init.el and reopening emacs i get this error: Symbol's value as variable is void: c++-mode-map. – amirt Jul 24 '16 at 19:06
  • 1
    That just means c++-mode isn't loaded yet. Try wrapping it with with-eval-after-load: (with-eval-after-load "cc-mode" (define-key c++-mode-map [f5] #'compile)). That way it won't run until after cc-mode loads and c++-mode-map exists. – erikstokes Jul 24 '16 at 19:45
  • Trying to do this and I cannot add a file local variable. Following your instructions, I get the error message: read-file-local-variable-value: Trailing garbage following expression. I am definitely using add-file-local-variable(not read). Even just pasting in the compile-command at the end of the file doesn't work. I still get prompted to enter a compile-command every time (with the usual make -k as default). – n1k31t4 Jun 18 '18 at 0:30
  • @n1k31t4 I don't recognize that error, but it still works on my machine. Try asking adding more details and asking as a new question. – erikstokes Jun 18 '18 at 23:24
  • @erikstokes - it was my mistake - using the function interactively, I did not place it within quotes. – n1k31t4 Jun 18 '18 at 23:43
-2

I wonder why you use Emacs, if you don't write Elisp at all. For example, http://www.nathan1hughes.co.uk/programming/why-i-persisted-to-learn-emacs/ suits your question well, on the bottom of the blog:

 ;; Run C programs directly from within emacs
(defun execute-c-program ()
  (interactive)
  (defvar foo)
  (setq foo (concat "gcc " (buffer-name) " && ./a.out" ))
  (shell-command foo))
  • 4
    Not sure what the patronizing is for. – wasamasa Jul 23 '16 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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