I used to use tmux with vim to write codes in C/C++. So I split the screen into two in tmux and then press F3 to "make" the file. And then I can press F4 to run the "main" file in the next subscreen.

Now I have switched to emacs and am wondering if there is anyway to do the same.


Here is a start. Add to your .emacs file:

(global-set-key [f4] 'compile)

You can also enter the "f4" and "compile" interactively. Starting out with

M-x global-set-key

Compile is a generic for running any command in a separate buffer. The default command runs "make", which may or not be what you want. You have an opportunity to change that and compile will remember what you last typed and keep a history of invocations you've given.

If what you always wanted to run "foo" with no questions asked, you could define a command and bind that instead of "compile" above to f4. The function definition would look like this:

(defun compile-run-remake ()
   "Run `remake -x` in a compilation buffer."
   (compile "remake -x"))

Going further you instead of hard-coding a command like "remake -x" you can pick that out from a constant or a customizable variable.


In your init file, add:

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

By default, when you press F3, it will execute make -k (or --keep-going - continue as much as possible after an error) in the directory where your file is.

If you want to allow the shortcut for a specific language (like C++), use:

(define-key c++-mode-map (kbd "<f3>") #'compile)

The new buffer opened is called the *compilation* buffer.

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.