NetBeans Quick Tip #6 - Abbreviations in Editor



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:

AbbreviationAction
trtry statement
elifelse if
forfor cycle
whwhile
swswitch
trctry catch
ededitor fold
rnreturn null
fyfinally
cacatch
elelse
foreforeach
tthis
nunull
Dodouble
forjfor cycle with j
totodo
ifif clause
foritfor with iterator
psvmmain method


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

would be nice if we could have a place where netbeans users could put those kind of files.

Posted by guest on červen 10, 2005 at 05:44 dop. CEST #

Agree, how about creating a collaboration page on java.net under NetBeans? It could be used by the community to share such files.

Posted by Roman Strobl on červen 10, 2005 at 05:53 dop. CEST #

Very good idea. A place to share nB tips 'n' tricks is just what the doc ordered. :-)

Posted by Bharath on červen 10, 2005 at 06:07 dop. CEST #

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.

Posted by vbrabant on červen 10, 2005 at 01:19 odp. CEST #

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.

Posted by Roman Strobl on červen 10, 2005 at 01:25 odp. CEST #

Ok. But will it be easy for user to add new surround with and new cycle with system similar to one I proposed here ?

Posted by guest on červen 10, 2005 at 01:55 odp. CEST #

It should be. Our usability team will keep an eye on it.

Posted by Roman Strobl on červen 10, 2005 at 02:05 odp. CEST #

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>

Posted by jake on červen 10, 2005 at 03:28 odp. CEST #

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?

Posted by Jake on červen 10, 2005 at 03:30 odp. CEST #

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".

Posted by Roman Strobl on červen 10, 2005 at 03:36 odp. CEST #

I would really like more complex abreviations 1) If I make an abreviation that goes like this "for { int $a = 0; $a < $b; $a++) { $cursor }" 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 {})

Posted by Henrik on červen 10, 2005 at 04:09 odp. CEST #

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?

Posted by Roman Strobl on červen 10, 2005 at 04:19 odp. CEST #

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: http://wiki.java.net/bin/view/Netbeans/TipsAndTricks

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.

Posted by Roman Strobl on červen 10, 2005 at 04:44 odp. CEST #

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)

Posted by Henrik on červen 10, 2005 at 05:20 odp. CEST #

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.

Posted by Roman Strobl on červen 11, 2005 at 03:07 dop. CEST #

Roumen, is there documentation anywhere for this feature in NetBeans? Is "sout" documented? (I honestly don't know..)

Posted by jake on červen 11, 2005 at 08:36 odp. CEST #

It's just one of the abbreviations. Just type in Java editor sout and press space. It will be expanded to System.out.println("|");

Posted by Roman Strobl on červen 12, 2005 at 12:57 dop. CEST #

what I meant to ask was is any of this (abbreviations, list of implemented appreviations) documented?

Posted by jake on červen 12, 2005 at 07:22 dop. CEST #

The list is in Help | Keyboard Shortcuts. More info also here (I hope it's up-to-date).

Posted by Roman Strobl on červen 12, 2005 at 08:29 dop. CEST #

[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 | , [ , ] , { , } ...

Posted by M'enfin! - David Delabassee's blog on červen 13, 2005 at 03:16 odp. CEST #

Post a Comment:
Comments are closed for this entry.
About

Roman Strobl

Search

Archives
« duben 2014
PoÚtStČtSoNe
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today