Skip to content

Instantly share code, notes, and snippets.

@goromlagche
Last active November 8, 2017 14:38
Show Gist options
  • Save goromlagche/062ab3f8abb32cd21ef7dc1f98d3a4ca to your computer and use it in GitHub Desktop.
Save goromlagche/062ab3f8abb32cd21ef7dc1f98d3a4ca to your computer and use it in GitHub Desktop.
a small function to compile cpp/java code on eshell
(defun compile-current-cpp-file ()
"Compile and run current file on eshell"
(interactive)
(let ((filename (buffer-file-name)))
(with-selected-window (get-buffer-window "*eshell*")
(recenter)
(insert "g++ -std=c++11 " filename " -o a.out && ./a.out")
(eshell-send-input)
(erase-buffer))
(message "File '%s' is compiled and ran" filename)))
(defun compile-current-java-file ()
"Compile and run current file on eshell"
(interactive)
(let ((filename (buffer-file-name))
(filename-wo-extension (file-name-nondirectory (file-name-sans-extension buffer-file-name))))
(with-selected-window (get-buffer-window "*eshell*")
(recenter)
(insert "javac " filename " && java " filename-wo-extension)
(eshell-send-input)
(erase-buffer))
(message "File '%s' is compiled and ran" filename)))
(add-hook 'c++-mode-hook
'(lambda ()
(local-set-key (kbd "C-c C-c") 'compile-current-cpp-file)))
(add-hook 'java-mode-hook
'(lambda ()
(local-set-key (kbd "C-c C-c") 'compile-current-java-file)))
@goromlagche
Copy link
Author

I personally use a 3 window setup.

+-------------------------+-----------------------------+
|                         |                             |
|                         |                             |
|                         |                             |
|                         |      input file             |
|                         |                             |
|                         |                             |
|                         |                             |
|                         |                             |
|                         |                             |
|       CODE              +-----------------------------+
|                         |                             |
| freopen("input file",   |                             |
|   "r", stdin);          |                             |
|                         |                             |
|                         |         eshel               |
|                         |                             |
|                         |                             |
|                         |                             |
|                         |                             |
|                         |                             |
+-------------------------+-----------------------------+

now as soon as I am done with coding, I can just press C-c C-c and I can view the result in the eshell prompt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment