I know that we can start compile interactively using C-u M-x compile. Is there a way to start this whenever I run M-x compile? If that cannot be done, how do I create a shortcut to C-u M-x compile?

My current shortcut for M-x compile is:

(global-set-key (kbd "<f5>") 'compile)

This would execute normal compile (not interactive). Ideally, I would like to create a separate shortcut for interactive compile (for program that requires a lot of user inputs).

Please let me know and thanks for your help

Edit: I forgot the mention that my compile command is also customized based on mode hook. For example, below is how I customize it for C++ mode hook and Fortran mode hook:

(add-hook 'c++-mode-hook
        (lambda ()
        (set (make-local-variable 'compile-command)
            (concat "g++ -g -Wall -lm " buffer-file-name " && ./a.out"  ) )))

(add-hook 'fortran-mode-hook
        (lambda ()
        (set (make-local-variable 'compile-command)
            (concat "gfortran " buffer-file-name " && ./a.out"  ) )))
  • @lawlist: I tried your answer, and it only shows a compilation window with the output from ls -la. This compilation window is not in comint mode, because if I do C-u M-x compile, then the compilation window would have something like "Comint mode started at....". I also customize my compile command base on mode-hook, will update that in the question asap.
    – mle0312
    Commented May 20, 2020 at 21:48
  • Sorry, I deleted the original comment shortly before you responded. How about? (global-set-key [f5] (lambda () (interactive) (let ((current-prefix-arg '(4))) (call-interactively 'compile)))) I see that the function call-interactively has optional arguments, one of them being KEYS -- it may be possible to use that argument as an alternative to setting the current-prefix-arg as I have done in the example of this comment.
    – lawlist
    Commented May 20, 2020 at 21:50
  • @lawlist: that works, thank you so much! And it also respects my customized mode hook
    – mle0312
    Commented May 20, 2020 at 21:53

1 Answer 1

(global-set-key [f5] (lambda ()
                       (let ((current-prefix-arg '(4)))
                         (call-interactively 'compile))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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