Image displaying notepad++.

How to run your code with a Shortcut in Notepad++

Tuesday, September 3, 2024 | Written by fishbowlforever

The article explains how to run code in Notepad++ with a convenient shortcut.

Notepad++ is a versatile text editor with extensive customization options. For developers who often work with multiple programming languages, it’s convenient to execute code directly from the editor. With the NppExec extension, you can set up a universal script to run code based on the file type, all triggered by a simple shortcut.

In this guide, we’ll walk through setting up a universal run script in Notepad++ using the NppExec plugin. The script will automatically detect the file extension and run the corresponding command.

Prerequisites

  • Notepad++: Make sure you have Notepad++ installed. You can download it from the official website 🔗.
  • NppExec plugin: This plugin is essential for executing scripts and commands within Notepad++. If you don’t have it installed, follow these steps to add it:
  1. Open Notepad++.
  2. Go to Plugins > Plugins Admin.
  3. Search for “NppExec” and check the box next to it.
  4. Click Install and restart Notepad++ if prompted.

Creating the Universal Run Script

  1. Open the NppExec Console:

    • Go to Plugins > NppExec > Execute NppExec Script. or use F6
  2. Enter the Script:

    • In the Execute dialog, enter the following script:

      cd "$(CURRENT_DIRECTORY)"
      NPP_SAVE
      SET extension = $(EXT_PART)
      IF $(extension) = .py GOTO PYTHON
      IF $(extension) = .hs GOTO HASKELL
      IF $(extension) = .pl GOTO PROLOG
      IF $(extension) = .c GOTO C
      IF $(extension) = .html GOTO HTML
      IF $(extension) = .go GOTO GO
      EXIT
      :PYTHON
      cmd /c python -Wdefault "$(FILE_NAME)"
      EXIT
      :HASKELL
      ghci -Wno-tabs "$(FILE_NAME)"
      EXIT
      :PROLOG
      swipl -s $(FILE_NAME)
      EXIT
      :C
      make
      cmd /c $(NAME_PART).exe
      EXIT
      :HTML
      start $(FULL_CURRENT_PATH)
      EXIT
      :GO
      go run .
      EXIT
      

    This script checks the file extension and executes a corresponding command. The commands are the commands i personally use, but they will not work in every case. You might have to modify them to your needs. You can add other Programming languages using the same Pattern.

  3. Save the Script:

    • After entering the universal script, click Save and name it something like universal_run.
  4. Assign a Shortcut:

    • To make the script easily accessible, assign it a shortcut:
      1. Go to Plugins > NppExec > Advanced Options….
      2. Under “Associated script”, select universal_run.
      3. Set Item Name to something sensible, like “Universal Run Command”
      4. Click Add/Modify.
      5. Restart Notepad++
      6. Go to Settings > Shortcut Mapper.
      7. Find “Universal Run Command” in the “Plugin Commands” using the search/filter bar at the bottom and assign it a shortcut key (I like to use Alt+R).
  5. Test the Setup:

    • Open a file in Notepad++ with one of the supported extensions and press the shortcut key. The script should detect the file type and execute the appropriate command.

Conclusion

With the NppExec plugin and a universal run script, you can streamline your workflow in Notepad++ by running different types of code with just a shortcut. This setup is customizable, so you can easily add support for more languages or modify existing commands to suit your needs. Happy coding!

Related Posts