Speed Up Development: NetBeans Code Templates, AutoHotKey, and Ditto

    Chris Roberts
    Share

    Writing an application in PHP or any other language is both a rewarding and sometimes challenging endeavor. It’s also a time consuming process depending on the complexity of the project you’re working on. Some IDEs have built in tools or plugins that aim to speed up the programming process. There are also other helpful pieces of software such as libraries, code snippets, and third party applications. In this article I’ll show you how I use a feature built into NetBeans along with two other applications to increase my productivity when programming on Windows. These tips and techniques offer a different way of thinking about things which should be helpful to any developer, regardless of his skill level.

    I’m working with the NetBeans IDE for the purposes of this article. If you don’t already have it installed, you’ll need to make sure the Java JDK is installed first since it is a requirement for NetBeans. You can download the latest JDK from Oracle. Once the JDK is installed, you can download NetBeans from target=”_blank”>netbeans.org/downloads. There are several versions of NetBeans available; I downloaded the All bundle because in addition to PHP I also do some C and Java development. If you only need PHP support, then feel free to install the PHP bundle instead.

    NetBeans’ Code Templates

    Go ahead and start NetBeans and create a new project; a dummy project will be sufficient for our purposes right now. Select File > New Project from the main menu bar, and in the New Project dialog select PHP Application and click Next. Since you are only creating a dummy project, you can click Finish on the next screen. NetBeans creates the new project and opens the new index.php file for you to edit.

    Code Templates is NetBeans’ code snippets functionality. Using templates allows you to write an abbreviation for a common piece of code and then press a trigger key to replace the abbreviation with a code snippet. Let’s try one of the templates that are already defined…

    In index.php which was opened after creating the new project, place the cursor after the comment in the PHP block and press Enter to insert a new line. Then type the word if and, instead of pressing the space bar, press the Tab key. NetBeans will add the beginnings of an if statement to your script.

    Now let’s create a new code snippet. Select Tools > Options from the main menu, and click on the Editor icon at the top of the Options window. Choose the Code Templates tab and then select HTML from the Language drop down. Click on the New button, type pre in the Abbreviation field, and click OK. In the Expanded Text field, type the following:

    <pre>
    <?php print_r(${cursor}); ?>
    </pre>

    I’ve chosen this as a simple example because it’s a common method to view the contents of arrays and objects in PHP. NetBeans is code aware, so because you created the snippet in the HTML templates it won’t work if you’re inside a PHP block. The ${cursor} variable in the snippet tells NetBeans where to place the cursor after the trigger key is pressed. After you’ve typed the code, click OK and test it out.

    AutoHotKey

    Now that you know how to define Code Templates in NetBeans, let’s move on to another piece of helpful software– a program called AutoHotKey. AutoHotKey is a scripting language to automate tasks on Windows. You can download it from www.autohotkey.com/download.

    Once AutoHotKey is installed, right click on the desktop and select New > AutoHotKey script and name the file anything you like. Open the AHK file in a text editor like notepad, and type the following:

    #^p::
    Send pre{tab}
    Return

    The pound sign tells AutoHotKey to listen for the Windows key, the caret signifies the CTRL key, and P is just the P key. The two colons afterwards tell AutoHotKey this is a batch of commands. The next line sends the keystrokes p, r, e, and a Tab to whichever window is active at the moment. Those keystrokes also match your Code Template abbreviation and the trigger Key in NetBeans. Return tells AutoHotKey to stop processing lines and start listening for other keys.

    Save the file, then right-click on the AHK file and select Run script. Go back to NetBeans and delete the previous pre block you inserted using Code Templates. Now on your keyboard press CTRL + WIN + P and the pre block should be inserted and the cursor placed in the correct location.

    AutoHotKey has a simple but effective syntax for scripting Windows. You can read more about its hotkey and see more sample scripts at www.autohotkey.com/docs/Hotkeys.htm.

    Ditto

    The final piece of software I’d like to bring to your attention is Ditto. Ditto is a clipboard manager, and is one of the better ones I’ve tried as it is unobtrusive and has a small memory footprint. You can download it from ditto-cp.sourceforge.net. Once Ditto is downloaded, extract the contents of the archive file to any location of your choosing. Open the folder and double-click on the Ditto.exe file. This runs Ditto and places it in the System Tray. Right click on the Ditto icon and click on Options.

    I set a couple of options but most of the defaults have proven to be sufficient in the time I’ve been using it. For example, Ditto defaults to a storage size of 500 items which is plenty in my opinion, but you can change this number to suit your needs. The options I have changed however are to start Ditto on system startup and its shortcut key to CTRL + SHIFT + V. This allows me to use Ditto by pressing one additional key to paste.

    Let’s operate under the assumption you’ve copied and pasted several variable names for arrays, and now you want to see the contents of one of them. In NetBeans, press CTRL + WIN + P to insert your pre block, and then press CTRL + SHIFT + V until the variable name you wanted to use is shown. You’ve cut down a lot of your keystrokes and have managed to keep your hands in similar positions on the keyboard!

    Ditto allows you to sync your clipboards across multiple computers, which is great if you can forward ports with your router, adjust your firewall settings, etc. Unfortunately I don’t have access the port forwarding settings in the router at the office. To get around this limitation, I copied the ditto.db from the Ditto folder and placed it in a Ditto directory in my Dropbox account. Then I change the database path in Ditto’s options to point to the Dropbox folder. I just have to remember to close Ditto before I leave my office computer and the database stays synced between my other computers. I also make use of Dropbox to store my AutoHotKey scripts.

    Summary

    In this article I’ve shown you how I use NetBeans’ Code Templates with AutoHotKey and Ditto to boost my productivity when programming. If you weren’t familiar with these programs before, I hope you find them useful and helpful. If you have any productivity tips you’d like to share, feel free to do so in the comments section below.

    Image via Gunnar Pippel / Shutterstock