2

I use Emacs under Ubuntu.

I would like to bind the make command to a shortcut, so that makefile could be launched. If the compilation fails, I would the error message to be shown in a sub-window or the message bar; otherwise, I would all the sub-windows to be closed except the one containing the main text.

Does anyone know how to realize that? Also which shortcut should be chosen?

SoftTimur
  • 121
  • 2
  • Why not just bind `compile` to whatever key you'd like? `compile-command` is 'make -k' by default. – Kyle Meyer Apr 27 '15 at 02:51
  • 1
    Or, even better, use `recompile` instead of `compile`. That way you skip the confirmation step. – deprecated Apr 27 '15 at 03:35
  • Like deprecated said, just bind the recompile command, and set compile-command somewhere in your init file. F5 or F7 are pretty common shortcuts for most IDEs. – user2699 Apr 27 '15 at 03:36
  • 1
    I know this is an old thread, but, when I run `M-x compile` it uses my dir vars, and run make accordingly. If I use `(global-set-key (kbd "C-c C-c") #'compile)`, the compile command run is not the same (asks complete different questions, probably related to the major mode). How to specify the exact 'built-in' compile method? – Alberto Mar 28 '19 at 10:09

2 Answers2

5

One possibility (based on an original suggestion by Kyle Meyer) is

(global-set-key (kbd "C-c m") 'recompile)

Then you can type C-c m, which will first offer to save the current buffer if necessary, and then will run the command make -k in the directory containing the associated file. The -k switch causes make to try to keep going even if some commands fail. In case you want to modify this, you can type C-u C-c m and you will get the chance to edit the compilation command in the minibuffer first. The edited command will then be remembered on a per-buffer basis.

Any compilation errors will be shown in a dedicated buffer (called *compilation*) and you can either click on these with the mouse (to jump to the relevant point in the source file) or use C-x backtick to cycle through them (sorry, I can't work out how to type a literal backtick inside backticks!). The compilation buffer is not shown if no errors occur but you get a notification in the echo area when compilation is complete.

Of course, you can use any other key of your choice instead of C-c m - I chose that for the mnemonic m = "make" and because the C-c letter keys are reserved for the end-user, so there is little risk of collision.

deprecated
  • 2,775
  • 14
  • 15
1

So the global set key to recompile is the first way to go. Now we can try another option, more interactive and visual, which is helm-make by abo-abo, a prolific emacs lisp developer: https://github.com/abo-abo/helm-make

With the command helm-make-projectile you will have a nice helm buffer which suggests the targets of the makefile, that it will find at your project root directory (handy). Choose one and it calls compile.

Install it with

package-install RET helm-make RET

(you need MELPA)

Note: and this package is similar, but it's mine, it displays a menu with the Makefile targets, and it isn't in melpa. To try it copy it in a buffer and call M-x eval-buffer.

Ehvince
  • 1,091
  • 10
  • 14