i wanted to bind a key to a compile and run command, then i found this post, Compile and run single file c++ programs in Emacs[SOLVED] ( last answer)

It works perfectly fine and as intended.

The only problem i am having is: is it possible to run the "compile" process in the eshell or in the shell instead of the "compile" shell? The problem with the "compile" shell is that i am unable to write anything after the program exited.

For those who dont want to click on the link,here is the elisp function, even tho i dont think it has much to do with the question:

(defun compileandrun()
  (compile (concat "g++ "  (file-name-nondirectory (buffer-file-name)) " -o " (file-name-sans-extension   (file-name-nondirectory (buffer-file-name))) " && ./" (file-name-sans-extension  (file-name-nondirectory (buffer-file-name)))) t )
(other-window 1)
(add-hook 'c++-mode-hook
          (lambda () (local-set-key (kbd "<f8>") #'compileandrun)))
  • 1
    You can compile a program in eshell, but you cannot use compilation-mode in an eshell buffer. If you want the goodies of compilation-mode, then you'll need to stick with using the compile.el library. You can continue to execute new commands with compile provided that you have the correct working directory, or you let-bind the default-directory for the duration of the compile command. E.g, (defun make () (interactive) (compile "make")); or, M-x eval-expression RET (compile "make install") RET. FYI: eshell is able to process Lisp expressions at the command prompt. – lawlist Jul 24 '20 at 19:43
  • 1
    @lawlist There is compilation-minor-mode which you can temporarily activate to have the goodies of compilation-mode. I just wrote a little C++ test program and put intentionally an error in there. I run g++ test.cc -o test.exe in an Eshell and switched on M-x compilation-minor.mode afterwards. The errors are highlighted like in compilation-mode and clicking on the error lines in the Eshell buffer sends me to the occurrence of the error in the C++ file. When I am done with debugging I can switch compilation-minor-mode off and use the Eshell buffer as usual. – Tobias Jul 25 '20 at 13:01

Your Answer

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

Browse other questions tagged or ask your own question.