Using Emacs as an IDE
I had to learn Emacs on Unix many years ago as it was at that time the only development environment for Smallworld Magik. Having learnt it, I have stuck with it as it provides a consistent platform for editing source code and building applications in as many computer languages and platforms as I have used and many more I haven’t. It is the only source code editor I have used that always gets the indentation and syntax highlighting right.
There is an initial steep learning curve to Emacs, it can be a bit like Unix ed, once you get stuck, you can’t escape, especially on the command line. The thing to remember is Control-G – Quit. That will get you out of almost anything. Useful keyboard shortcuts to get started in no particular order:
Control-g
– QuitControl-x->Control-c
– Quit Emacs – it will prompt if you have unsaved changes.Control-s
– Incremental search – type in what you’re looking for and keep pressing Control-S until you find the instance you want.Control-r
– The same thing backwards – you can switch between the two.Alt-x
– Type in a command that isn’t on the menu – Control-G will get you out.Control-x->Control-f
– Open a new or existing file – Emacs calls it visiting a file for some reason.Control-x->Control-s
– Save the current buffer – the one you are editing.Control-x->Control-w
– Save the current buffer with a new name.Shift-Alt-%
– Query replace – Prompts for two strings and does a search and replace forward from the current point, prompting for each replacement. Control-G is your friend again.Shift-Control-Alt-%
– Query replace regexp – the same thing with a regular expression instead.Alt-x->delete-rectangle
– Delete a rectangular area of text – useful for editing log files or lists.
Some of these things are on the menu, but the keyboard shortcuts are easier to use. You need to customise Emacs to do some of the things I use it for.
There are several ways of customising Emacs, but it all ends up in your ~/.emacs config file. First off, you need to install the Emacs packages you need, like groovy-mode for editing Android Gradle build files, JS3-mode for Javascript files, Markdown-mode for Github README files, yaml-mode for Travis Cl build files, etc. This is on the Options->Manage Emacs Packages menu. You can change some settings on the Options menu, like Options->Use CUA keys (Cut/Paste with C-x/C-c/C-v), but remember to Options->Save Options before you quit. Some of them are buried deeply in Options->Customise Emacs and can be a pain to find. It’s easier to just put them in your ~/.emacs file. Examples are:
'(c-basic-offset 4)
'(c-offsets-alist (quote ((substatement-open . 0))))
That gives you an indent of four spaces for all code editing, and makes it put the opening bracket of if or for statements in the right place.
(add-to-list 'auto-mode-alist '("\\.c\\'" . c++-mode))
(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))
(add-hook 'nxml-mode-hook
(lambda ()
(set (make-local-variable 'compile-command)
"ant debug")))
(require 'package)
(add-to-list 'package-archives
'("melpa" . "http://melpa.milkbox.net/packages/") t)
(package-initialize)
(add-to-list 'auto-mode-alist '("\\.gradle\\'" . groovy-mode))
(add-hook 'groovy-mode-hook
(lambda ()
(set (make-local-variable 'compile-command)
"gradle build")))
The above Lisp code makes Emacs use c++mode for .c and .h source files, change the compile command to ‘ant debug’ for Android xml files, use groovy-mode for .gradle files and change the compile command to ‘gradle build’ for Android gradle files.
To use some of the options on the Tools menu, like diff, compile, egrep, etc., require the tools to be in your path. You can achieve this on windows by installing MSYS2 and making sure your path picks it up. Linux and OSX will have these tools installed if you are using it for development.
Build an Android app using Gradle
Build an Android app using Apache Ant
It will also edit HTML and just about anything else.
Java and HTML files
Find errors
If you click on compiler error messages it will take you to the point in the source file. This works well with C and C++, but not consistently on Android Java source files due to a missing blank in the error message.
See Also
- Create a Word Grid
- Android Kotlin
- Android Word Count
- Using Emacs in Android Development
- Fast Fourier Transform