Quick Tip #7 - Macros in Editor

Guest Author

As promised, today's tip is about macros. What are they? These are actions which are recorded according to what you do in the editor. Depending how good you are, you can create even such action such as surround with try-catch, comment a piece of code, etc.

You can record the macros by pressing the red button in editor toolbar, perform the actions and then end recording by pressing the grey square next to the button. Then you name the macro and assign a shorcut to it. To execute the macro, you just need to press the shortcut you've assigned.

Michel Graciano from Brazil has prepared some great macros and you can download them here. Similarly to abbreviations the content of the zip should be placed into the directory ~/.netbeans/4.1/config/Editors/text/x-java. Make sure to backup all XML files this may replace. The abbreviations in abbreviations.xml are necessary to execute the macros (they contain few more than last time), the keybindings.xml file sets the shortcuts. Macros.xml file contains macros recorded by Michel.

Here are the available macros:

Ctrl-J ISurround with if()
Ctrl-J TSurround with try-catch
Ctrl-J LAdd debug code
Ctrl-N PCreate property
Ctrl-J {Add a block
Ctrl-J HComment code
Ctrl-N FDefine identifier by previous class
Ctrl-N SDelete two words
Ctrl-J CTrim
Alt-U QToggle case of identifier
Ctrl-J [Format code
Ctrl-J DDebug variable
Ctrl-J FAdd editor fold

Most of these macros require to have selected a piece of code in the clipboard. You may not use all of these, but they're a good source of inspiration for creating your own macros. Actually this way you can add simple features to your IDE. How cool is that? :-)

Join the discussion

Comments ( 6 )
  • Michel Graciano Wednesday, June 15, 2005
    Thank's a lot for your tip publication. There are somne problems in that macros, and the worse is the fact that your clipboard is lost qhen you use a macro. If somebody know how do that appropriately, please tell us.
  • Roman Strobl Wednesday, June 15, 2005
    I'll ask the editor developers if anything can be done about the clipboard. Michel, what are the other problems?
  • Shreedhar Sunday, June 19, 2005
    Very cool indeed.

    A good enhancement would be for the IDE to autodetect a situation in the editor that requires the macro to be offered to be run. For example, say, the code is at a point where a try-catch block or Exception declaration is needed, the IDE then offers a popup with choices of "Surrounding with a Try-Catch Block" or "Declare Exception in the method". When one of these is chosen by user, the appropriate macro is run. This would be very cool to have and more interactive.

    Perhaps even better would be a framework that allows for custom macros to be defined to be offered to be played at certain custom defined situations for users to create. Hope to see this sometime in the future.

  • Roman Strobl Sunday, June 19, 2005
    Well that would be very nice, but probably not easy to implement. The functionality you are writing about is available through editor hints (in alpha stage), I wrote about them earlier. Also the surround with try-catch action was added to editor context menu in recent daily builds (with a few bugs :-)
  • manoj Monday, October 24, 2005
    how do i get the class name in the following
    macro :
    copy-to-clipboard caret-end-line insert-break "if(DEBUG" caret-end-line "System.out.println(\\"" paste-from-clipboard ";"
    for which I select the method name
    before calling it.
    The purpose is to produce the following:
    if(DEBUG)System.out.println("Method Name");
    I want to add the class name as well ...
  • Roumen Wednesday, October 26, 2005
    I don't think that this is be possible but I may be wrong - I've just never seen such usage. Feel free to file an RFE for this.
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.