NetBeans Quick Tip #6 - Abbreviations in Editor

Guest Author

NetBeans editor supports so called abbreviations. They can make you really productive, it's enough to type in psfs, press space and the IDE generates for you "public static final String". The full abbreviation list is in Help | Keyboard Shortcuts.

The nice thing is that you can add your own abbreviations. So whenever you find out you're writing something often, you can create an abbreviation and use it instead of typing (all programmers are lazy, aren't they?). An even nicer thing is that there are people willing to share their abbreviations - Michel Graciano from Brazil has sent me a list of his abbreviations. They're more advanced than the IDE offers by default, for instance, you can type in trc and the IDE generates:

try {
} catch (Exception e) {

The | mark denotes placement of cursor. I have added into the list one of my popular abbreviations - psvm, it generates:

public static void main(String[] args) {

You can download the XML file here. Just copy it to your userdir in ~/.netbeans/4.1/config/Editors/text/x-java/abbreviations.xml. In case this file is already there it means you have already created some abbreviations and then you can just paste the tags from the xml file to add new abbreviations.

These are the contents of the abbreviations XML file:

trtry statement
elifelse if
forfor cycle
trctry catch
ededitor fold
rnreturn null
forjfor cycle with j
ifif clause
foritfor with iterator
psvmmain method

Next time I will write about macros (Michel has also a great list of macros).

Join the discussion

Comments ( 20 )
  • guest Friday, June 10, 2005
    would be nice if we could have a place where netbeans users could put those kind of files.
  • Roman Strobl Friday, June 10, 2005
    Agree, how about creating a collaboration page on java.net under NetBeans? It could be used by the community to share such files.
  • Bharath Friday, June 10, 2005
    Very good idea. A place to share nB tips 'n' tricks is just what the doc ordered. :-)
  • vbrabant Friday, June 10, 2005
    What would be nice is the following:
    I do a selection in the editor, right click, select option surround with and NetBeans propose all abbreviation where a | appears.
    It means that I can
    easily surround code with try {
    } catch (Exception ex) {}
    easily surround code with /\*\*
    or with necessary information to create a code fold, ...
    Also, what would be nice is if netbeans could support abbreviation like that :
    for (|;|;|) {
    And when I type fr + space, I type int i=0 TAB
    i<j TAB i++ TAB and then body of the for loop.
    I can press Escape at any moment to espace that mode.
  • Roman Strobl Friday, June 10, 2005
    Vincent, actually you can do this through macros. I'll blog about it soon. Surround with try-catch functionality is being prepared (both as a hint and as an action). A for cycle which let's you do what you've described is being prepared for next release.
  • guest Friday, June 10, 2005
    Ok. But will it be easy for user to add new surround with and new cycle with system similar to one I proposed here ?
  • Roman Strobl Friday, June 10, 2005
    It should be. Our usability team will keep an eye on it.
  • jake Friday, June 10, 2005
    Excellent. I've been lamenting not having the feature known in Eclipse as templates and this is exactly what I need. I've added an appreviation for System.out.println(""). Here:
    <abbrev key="sop" xml:space="preserve">System.out.println("|");</abbrev>
  • Jake Friday, June 10, 2005
    I should add that this kind of feature _should_ be a part of the IDE, (IMHO) as it is in Eclipse. DO I smell a plug-in, anyone?
  • Roman Strobl Friday, June 10, 2005
    Agree, it should be a part of the IDE. Maybe it will be in next release... ;-) Btw, System.out.println() abbrev exists in the IDE by default on "sout".
  • Henrik Friday, June 10, 2005
    I would really like more complex abreviations
    1) If I make an abreviation that goes like this
    "for { int $a = 0; $a < $b; $a++)
    I want the cursor to start at $a. There i can write a variable name, and press tab to goto next "stop place" ($b). When I press tab the other $as is filled with what I wrote.
    Then I can write somthing for $b and tab my way to $cursor
    2) abbreviations should appear in the code completion popup menu.
    On a side note, One idea for the error/hint stripe: have it indicate the scope for the inner most block (pair of {})
  • Roman Strobl Friday, June 10, 2005
    What you're suggesting with 1) is called live templates (from IDEA) and I know we want to support something similar. We've discussed 2) recently, my objection was that there could be too many options in code completion. But otherwise it's a good idea. Concerning error stripe, we should not put too many things into it for performance reasons, each such little feature takes some performance. When would you use the indication of the {} pair?
  • Roman Strobl Friday, June 10, 2005
    Answer to comments 1-3 - Wiki on java.net unfortunately doesn't support uploading of files. Anyway we could use it to keep an up-to-date list of NetBeans tips & tricks with links. NB users can share through it anything which helps other users - through the list you can let everybody know. We'll see if this initiative will work well ;-)

    The Wiki page is:


    It's linked from the main Wiki of NetBeans.
    I'll try to add into the table tricks & tips I'll find on the web and I encourage others to do the same.
  • Henrik Friday, June 10, 2005
    1)I actually got it from Eclipse, haven't used IDEA yet.
    2) my counter arguments: I (and I suppose others) tend to forget such handy features when they are hidden, a little exposure would not hurt. Secondly abbreviations are normally short and at times have names that is not similar to method names, so it would not be everytime that they would appear in the code completions
    They would e.g. never be there if the popup is activated after a "." (method start).
    3) I would use it to easily spot matching brackets and to spot variable scope (can't really remember the details of how it is presented in eclipse)
    Maybe it would be in idea to merge the stripe and the codefolding annnotations. So that there is not so many columns of stuff (1 for line numbers and annotation, 1 for codefolding and 1 for stripe)
  • Roman Strobl Saturday, June 11, 2005
    Ad 1) yes, Eclipse has it, too. Ad 2) I don't say I'm against I just said we should be carefull not to make a christmas tree from the IDE (meaning too many things offered everywhere). I've seen this request more times (to put abbrevs into code completion). Ad 3) I'd like to see usages of a field in error stripe when you are with cursor on that field. For sure there'll be discussions about error stripe when it gets to trunk, so we'll see what will be the results. I think merging codefolding and error stripe is a UI overkill. From usability point of view, people are used from other IDEs and already from NB to have it on the right side, so I would keep it as it is.
  • jake Saturday, June 11, 2005
    Roumen, is there documentation anywhere for this feature in NetBeans? Is "sout" documented? (I honestly don't know..)
  • Roman Strobl Saturday, June 11, 2005
    It's just one of the abbreviations. Just type in Java editor sout and press space. It will be expanded to System.out.println("|");
  • jake Sunday, June 12, 2005
    what I meant to ask was is any of this (abbreviations, list of implemented appreviations) documented?
  • Roman Strobl Sunday, June 12, 2005
    The list is in Help | Keyboard Shortcuts. More info also here (I hope it's up-to-date).
  • M'enfin! - David Delabassee's blog Monday, June 13, 2005
    [Trackback] There is something wrong for Java developers using French Apple hardware! French Apple keyboards are not developer friendly!
    If you look at any Apple French keyboard, you‘ll notice that they are missing some details keys like | , [ , ] , { , } ...
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.