One of the great features of the Netbeans IDE is the ability to create a macro to simplify repetitive and tedious tasks. In my case I had a long list of country names that needed to be placed in a Java property file. I.e. turning the following:
...
Armenia
Australia
Austria
Belgium
Bolivia
...
into the following key-value pair:
...
Armenia=Armenia
Australia=Australia
Austria=Austria
Belgium=Belgium
Bolivia=Bolivia
...
This of course is a real nuisance but with the help of a little macro the work can get done in no time.
My first attempt to create such a macro is in using the macro recorder. I placed the curser right after the word “Armenia” and pressed the round red “Start Macro Recording” button at the top of the editor.
I then moved the cursor holding the shift button (select) and using the arrow buttons to the beginning of the line. Then I pressed Ctrl-C (copy) and then moved the cursor via the mouse back to the end of the word “Armenia”. I entered a “=” symbol and pressed Ctrl-V. I then stopped recording the macro. Finally I assigned the previously unused keyboard shortcut CTRL-L to my new macro.
As could be expected the macro recorder has no notion of the beginning of the word or the beginning of the line. The recorder counted my backward selection steps and consequently the macro works only for countries with the same amount of letters as “Armenia” i.e. 7.
I opened the Macro Editor Tools -> Options -> Editor -> Macros and found the following code for my macro:
selection-backward
selection-backward
selection-backward
selection-backward
selection-backward
selection-backward
selection-backward
copy-to-clipboard
caret-forward
"="
paste-from-clipboard
What I need was a command that would take me to the beginning of the line. (The beginning of the word is not enough since some country names like for example “United States” consist of two words). In one of the previously defined macros I found the command “caret-begin-word” and I modified my macro as follows:
selection-begin-line
copy-to-clipboard
caret-forward
"="
paste-from-clipboard
And sure enough when placing the cursor at the end of the country name and pressing CTRL-L you get the desired completion of the line.
Definitely not an advanced Netbeans topic, but getting the hang of this can be a great time saver.
Tags: Netbeans
Would pressing the Home key to jump to the start of the line, work just as well?
Hi Gary, you are absolutely right. The Home key (plus the Shift key for selection) is equivalent to “selection-begin-line” and the obvious way of how this macro should have been recorded.
Nonetheless by taking the detour, my blog entry includes a quick description on how to open and use the Netbeans Macro Editor.