FAQ Desk: How do I use Notepad++ to Compile my Source Code (or Convert My Text)?

  • Hello, and welcome to the FAQ Desk. You have likely been directed to this page because you asked a question about compiling your source code from within Notepad++ (or similar conversion processes).

    In general: almost exactly the same as if you were compiling it from the command line.

    Please note that Notepad++ is not a compiler: if you do not have a separate executable that can compile your source code into an executable, Notepad++ cannot compile it for you.

    • If all you need is a single command based on the filename, for example you normally type "c:\path with spaces\to\compiler.exe" "FILENAMEHERE.cs" at the command line, where FILENAMEHERE.cs is your desired file to compile, then you can just use the builtin Run menu’s Run command, where you could type "c:\path with spaces\to\compiler.exe" "$(FULL_CURRENT_PATH)" (using the quotes around each term, because there can be spaces in those paths)

      • the official docs have a section on User Defined Commands, which includes a description of the “environment variables” like $(FULL_CURRENT_PATH) that the Run command recognizes.

      • you can click the Save button in the Run dialog to save that command for later, and even assign a keyboard shortcut for later use. It will show up in the Run menu from now on. You can even use the Shortcut Mapper to assign a keyboard shortcut to the compile command.

    • If you have a more complicated build need, which needs to set other environment variables, or run multiple commands (compile and link as separate steps, for example; or compile then run the resulting executable) you can either create a windows batch file or use a Notepad++ plugin.

      • For a windows batch file (.bat or .cmd or .ps1 file – whatever you are most comfortable with), you can pass the filename as an argument to your batch file instead of directly to the compiler: c:\path\to\batchfile.bat "$(FULL_CURRENT_PATH)"

      • You can use a plugin like the NppExec plugin, which has its own batch scripting language. For example, the following is an NppExec script which takes the the current .c file, saves any changes, compiles, and then runs the resulting .exe (with any output going into the NppExec console inside Notepad++):

          cd "$(CURRENT_DIRECTORY)"
          c:\path\to\gcc.exe "$(FILE_NAME)" -o "$(NAME_PART)"
          npp_run cmd.exe /k "$(NAME_PART)"
        • Note: I recommend using the full path to your compiler in your script

        • The NppExec script can be added to the Macro menu using Plugins > NppExec > Advanced Settings, and once in the Macro menu, you can use the Macro > Modify Shortcut command to add a keyboard shortcut for that script.

    This same procedure will work for tasks that are similar to compiling, like “how can I use Notepad++ to launch some external converter program?” – examples might be encryption/decryption, or a program that converts Markdown to HTML, etc. If you want to keep the source code, and have the output of the conversion in a new file, then the instructions above will work. However, if you want to convert the text in place, so that the result of the conversion replaces the text currently in the editor, here are a few options:

    • There is a Plugin available in Plugins Admin called “Pork to Sausage”, whose entire purpose in life is to convert text from one form to another, using an external program.

    • If you want to use NppExec, it is similar to the script above. One technique is to save output of your converter to a temporary file, and then copy the contents of that temporary file overtop the file active in Notepad++. (This example uses the gpg executable to encrypt your file, but the ideas are similar for other processes as well. The comments after the // should tell you the purpose of each line, so you can use those to help you develop your own script to run your converter program.)

        cmd.exe /c exit %RANDOM%                                                        // cannot access $(SYS.RANDOM) directly through NppExec, but can tell cmd.exe to return it as a value
        set tempfile = $(SYS.TEMP)\NppGpgFile_$(EXITCODE).tmp                           // create random tempfile name
        set ascfile = $(SYS.TEMP)\NppGpgFile_$(EXITCODE).asc                            // create associated ascfile name
        sci_sendmsg SCI_SELECTALL                                                       // select all
        sel_saveto $(ascfile) :a                                                        // save selection to the ascfile    (use :a to save as ansi, to prevent unicode prefix ÿþ getting embedded)
        gpg.exe --output "$(tempfile)" --decrypt "$(ascfile)"                           // decrypt
        sel_loadfrom $(tempfile)                                                        // replace selection with results
        sci_sendmsg SCI_DOCUMENTSTART                                                   // deselect
        rm -rf "$(tempfile)" "$(ascfile)"                                               // cleanup temp files
    • There might already exist a dedicated plugin for your specific conversion task.

Log in to reply