My job involves a lot of Java coding, so I have a few customizations for cc-mode and java-mode in order to make them easier to work with. While I don’t have too many at this time, they make working in Java a little more pleasant.
(require 'cc-mode) (add-hook 'java-mode-hook 'c-subword-mode) (add-hook 'c-mode-common-hook '(lambda () (c-toggle-syntactic-indentation 1) (setq c-basic-offset 4) (c-toggle-hungry-state 1) (c-toggle-electric-state 1)))
cc-mode, for those of you who don’t know, is emac’s base mode for c, c++, java, and a few other languages. It has a lot of keybindings in this mode for various movement commands, so you may want to look over the functions it gives. The manual is exhaustive and tells all the possible options you can do to modify it, but these are the ones I like the most. Java-mode is just a derived mode of cc-mode, so all hooks in c-mode-common-hook will be run for a java buffer.
C-subword-mode is a mode that treats changes of case to be word boundaries. This is often what I want, since Java uses camelcase by convention. If I want to change getBufferedReader() to getReader(), all I would have to do is place the point under ‘B’ and M-d to kill-word. Since Reader starts a new word, it will not be deleted. This can make it a bit less efficient to move past variables defined in camelcase, but if you’re traveling a long way you can probably get closer by searching for whatever it is.
The other hooks are all common to all cc-derived modes, since they are general utility functions. c-toggle-syntactic-indentatation will turn on indentation based on context; otherwise, all new lines will be aligned with the previous line. This also reindents the line on pressing of an ‘electric’ key, such as ‘;’, ‘(’, and many others. Without this command, creating a new line after opening a method would leave the point right below the first character in the function definition, instead of indenting it. the variable c-basic-offset just determines how large indentations should be; I like 4.
c-toggle-hungry-state and c-toggle-electric state turn on hungry deletion and eletric-mode, respectively. Hungry deletion is when backspacing or killing a character when on whitespace will delete not only one character, but to the next non-whitespace character. This is just very handy why you need to format code by deleting whitespace. Electric-mode will just turn on indentation when ‘electric’ keys are pressed.
Some people may be wondering why I don’t have CEDET or JDE in here. The problem with those packages is that they are incredibly slow and lag my emacs to the point of being unusable on most of my codebases - which is really annoying, since it makes programming in Java much better when it allows me to type. It provides some nice refactoring tools, automatic package importation, providing completion for the variable at point, and all sorts of nice stuff. I believe CEDET is being put into Emacs 23.2, so maybe when that comes out I’ll try it again and see if it’s gotten better.
The last thing you should know about if you are coding in emacs is M-x compile and M-x recompile. These commands give you nice buffers of the compile output with errors in red, linked to the location in the file in which they occur. You can cycle through the errors with next-error and previous-error (C-x ` and M-g p) without going into that buffer, making fixing your compile issues much easier.