Saturday, May 8, 2010

Notepad++ code auto format

What this does:
Set the Notepad++ so that we can format the selected code automatically by press the hotkey.

Software/plugin used:
Notepad++ v5.5
NppExec Plugin v0.3.3.1
SourceFormatX v2.56

How to:
  1. Install the required software/plugin.

  2. Start Notepad++ and open the NppExec Execute dialog.


    Enter the following commands:

    NPP_CONSOLE ?
    SET TMP_FILE = $(SYS.TEMP)\$(NAME_PART)_npp_tmp~$(EXT_PART)
    SET BIN_DIR = "C:\Program Files\SourceFormatX\SourceFormatX.exe"
    SEL_SAVETO $(TMP_FILE)
    $(BIN_DIR) $(TMP_FILE)
    SEL_LOADFROM $(TMP_FILE)
    cmd /c del /q $(TMP_FILE)*


    Note that the path BIN_DIR may depends on the directory where you install SourceFormatX.


  3. Save the commands with the name you like, e.g. Format by SourceFormatX.
    Now when you select the code and run the command in NppExec, the code is formatted automatically.


  4. Let's set a shortcut key for the command for easier access.
    Go to 'NppExec' -> 'Advanced Option', in the 'Menu items' we add the command we've just set.


  5. Then go to Notepad++ 'Settings' -> 'Shortcut Mapper' -> 'Plugin commands', set a unique shortcut for the command we've added.


    Now the hotkey is ready. Try it!

1 comment:

  1. The problems is that it doesn't refresh the file or make any change to the original file.
    I get the shortcut to work and I hear the sound of SourceFormatX, but my file remains unmodified...
    This is my NppExec console output:
    SET: TMP_FILE = C:\Users\grao3\AppData\Local\Temp\BeerGame_npp_tmp~.java
    $(TMP_FILE) = C:\Users\grao3\AppData\Local\Temp\BeerGame_npp_tmp~.java
    SET: BIN_DIR = "C:\Program Files\SourceFormatX\SourceFormatX.exe"
    $(BIN_DIR) = "C:\Program Files\SourceFormatX\SourceFormatX.exe"
    SEL_SAVETO: C:\Users\grao3\AppData\Local\Temp\BeerGame_npp_tmp~.java
    - OK, 0 bytes have been written to "C:\Users\grao3\AppData\Local\Temp\BeerGame_npp_tmp~.java"
    "C:\Program Files\SourceFormatX\SourceFormatX.exe" C:\Users\grao3\AppData\Local\Temp\BeerGame_npp_tmp~.java
    Process started >>>
    <<< Process finished.
    SEL_LOADFROM: C:\Users\grao3\AppData\Local\Temp\BeerGame_npp_tmp~.java
    cmd /c del /q C:\Users\grao3\AppData\Local\Temp\BeerGame_npp_tmp~.java*
    Process started >>>
    <<< Process finished.
    ================ READY ================

    ReplyDelete