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.
Open the NppExec Console:
F6
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
IF $(extension) = .sh GOTO SHELL
IF $(extension) = .bat GOTO BATCH
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
goimports -w .
go fmt .
go vet .
go run .
EXIT
:SHELL
wsl sh $(FILE_NAME)
EXIT
:BATCH
$(FILE_NAME)
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.
Save the Script:
universal_run
.Assign a Shortcut:
universal_run
.Alt+R
).Test the Setup:
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!