Saturday, September 26, 2009

My First Hack into Netbeans

This post describes how to develop a simple macro for Netbeans to comment and uncomment the selected text. By default the purpose is done by a inbuilt template in Netbeans. To comment the selected text using this template follow :
  • Select the text to be commented.
  • Press Alt+Enter. A drop-down menu will come. Select Surround with /*$selection*/
This method becomes cumbersome when you keep adding your own Templates.(How to develop your own Template in another post :P ) NetBeans allows you to develop your own macros to perform such tasks.
  • Go to Tools -> Options.
  • In Options select the Editor -> Macros tab.
  • Now Click New. A pop-up will come. Enter the macro name Comment .
  • A new entry in the macro table is entered. Select the entry and paste the following code in the Macro Code window.
select-identifier cut-to-clipboard caret-begin-line insert-break "/*" insert-break delete-previous delete-previous paste-from-clipboard insert-break remove-line "*/" insert-break
  • Set Shortcut Alt+Q (or anything you like)
Comment macro is created. Similarly create uncomment macro. Paste following code in the Macro Code window:
select-identifier last-non-white remove-line cut-to-clipboard caret-begin-line paste-from-clipboard first-non-white remove-line remove-line
  • Set shortcut Shift+Alt+Q (or anything you like)
  • Click Ok.
Now both the macros are created. To use them Select the text from the editor and press Alt+Q to comment and press Shift+Alt+Q to uncomment the commented code. Here is the list of elementary macro functions. You can develop your own using these link.

Enjoy NetBeans :)


g2 said...

an interesting evolution for your blog...

from excavating your life, you are back to doing what you are best at.. excavating comp science stuff :p

scadza said...

g2 ... more appropriate would be that "Netbeans and stuffs like it" form an important part of his life and hence excavating such stuffs is what he is doin