How to compile and run C files from within Notepad++ using NppExec plugin?
How can I configure the NppExec plugin for Notepad++?
I would like NppExec to compile my C files, run them, and show their output, all within Notepad++.
Solution 1:
Here's a procedure for Perl, just adapt it for C. Hope it helps.
- Open Notepad++
- Type F6 to open the execute window
- Write the following commands:
-
npp_save
<-- Saves the current document -
CD $(CURRENT_DIRECTORY)
<-- Moves to the current directory -
perl.exe -c -w "$(FILE_NAME)"
<-- executes the command perl.exe -c -w , example:perl.exe -c -w test.pl
(-c = compile -w = warnings)
-
- Click on Save
- Type a name to save the script (e.g. "Perl Compile")
- Go to Menu Plugins -> Nppexec -> advanced options -> Menu Item (Note: this is right BELOW 'Menu Items *')
- In the combobox titled 'Associated Script' select the script recently created in its dropdown menu, select Add/Modify and click OK -> OK
- Restart Notepad++
- Go to Settings -> Shortcut mapper -> Plugins -> search for the script name
- Select the shortcut to use (ie Ctrl + 1), click OK
- Verify that you can now run the script created with the shortcut selected.
Solution 2:
For perl,
To run perl script use this procedure
Requirement: You need to setup classpath variable.
Go to plugins->NppExec->Execute
In command section, type this
cmd /c cd "$(CURRENT_DIRECTORY)"&&"$(FULL_CURRENT_PATH)"
Save it and give name to it.(I give Perl).
Press OK. If editor wants to restart, do it first.
Now press F6 and you will find your Perl script output on below side.
Note: Not required seperate config for seperate files.
For java,
Requirement: You need to setup JAVA_HOME
and classpath
variable.
Go to plugins->NppExec->Execute
In command section, type this
cmd /c cd "$(CURRENT_DIRECTORY)"&&"%JAVA_HOME%\bin\javac""$(FULL_CURRENT_PATH)"
your *.class
will generate on location of current folder; despite of programming error.
For Python,
Use this Plugin Python Plugin
Go to plugins->NppExec-> Run file in Python intercative
By using this you can run scripts within Notepad++.
For PHP,
No need for different configuration just download this plugin.
PHP Plugin and done.
For C language,
Requirement: You need to setup classpath variable.
I am using MinGW compiler.
Go to plugins->NppExec->Execute
paste this into there
NPP_SAVE
CD $(CURRENT_DIRECTORY)
C:\MinGW32\bin\gcc.exe -g "$(FILE_NAME)"
a
(Remember to give above four lines separate lines.)
Now, give name, save and ok.
Restart Npp.
Go to plugins->NppExec->Advanced options.
Menu Item->Item Name (I have C compiler)
Associated Script-> from combo box select the above name of script.
Click on Add/modify and Ok.
Now assign shortcut key as given in first answer.
Press F6 and select script or just press shortcut(I assigned Ctrl+2).
For C++,
Only change g++ instead of gcc
and *.cpp
instead on *.c
That's it!!
Solution 3:
Here is the code for compling and running java source code: - Open Notepadd++ - Hit F6 - Paste this code
npp_save <-- Saves the current document
CD $(CURRENT_DIRECTORY) <-- Moves to the current directory
javac "$(FILE_NAME)" <-- compiles your file named *.java
java "$(NAME_PART)" <-- executes the program
The Java Classpath variable has to be set for this...
Another useful site: http://www.scribd.com/doc/52238931/Notepad-Tutorial-Compile-and-Run-Java-Program
Solution 4:
I personally use the following batch script that can be used on many types of files (C, makefile, Perl scripts, shell scripts, batch, ...).
How to use it:
- Install NppExec plugin
- Store this file in the Notepad++ user directory (%APPDATA%/Notepad++) under the name runNcompile.bat (but you can name it whatever you like).
- while checking the option "Follow $(CURRENT_DIRECTORY)" in NppExec menu
- Add a NppExec command
"$(SYS.APPDATA)\Notepad++\runNcompile.bat" "$(FULL_CURRENT_PATH)"
(optionally, you can putnpp_save
on the first line to save the file before running it) - Assign a special key (I reassigned F12) to launch the script.
This page explains quite clearly the global flow: https://www.thecrazyprogrammer.com/2015/08/configure-notepad-to-run-c-cpp-and-java-programs.html
Hope it can help
@echo off
REM ----------------------
REM ----- ARGUMENTS ------
REM ----------------------
set FPATH=%~1
set FILE=%~n1
set DIR=%~dp1
set EXTENSION=%~x1
REM ----------------------
REM ----------------------
REM ------- CONFIG -------
REM ----------------------
REM C Compiler (gcc.exe or cl.exe) + options + object extension
set CL_compilo=gcc.exe
set CFLAGS=-c "%FPATH%"
set OBJ_Ext=o
REM GNU make
set GNU_make=make.exe
REM ----------------------
IF /I "%FILE%"==Makefile GOTO _MAKEFILE
IF /I %EXTENSION%==.bat GOTO _BAT
IF /I %EXTENSION%==.sh GOTO _SH
IF /I %EXTENSION%==.pl GOTO _PL
IF /I %EXTENSION%==.tcl GOTO _TCL
IF /I %EXTENSION%==.c GOTO _C
IF /I %EXTENSION%==.mak GOTO _MAKEFILE
IF /I %EXTENSION%==.mk GOTO _MAKEFILE
IF /I %EXTENSION%==.html GOTO _HTML
echo Format of argument (%FPATH%) not supported!
GOTO END
REM Batch shell files (bat)
:_BAT
call "%FPATH%"
goto END
REM Linux shell scripts (sh)
:_SH
call sh.exe "%FPATH%"
goto END
REM Perl Script files (pl)
:_PL
call perl.exe "%FPATH%"
goto END
REM Tcl Script files (tcl)
:_TCL
call tclsh.exe "%FPATH%"
goto END
REM Compile C Source files (C)
:_C
REM MAKEFILES...
IF EXIST "%DIR%Makefile" ( cd "%DIR%" )
IF EXIST "%DIR%../Makefile" ( cd "%DIR%/.." )
IF EXIST "%DIR%../../Makefile" ( cd "%DIR%/../.." )
IF EXIST "Makefile" (
call %GNU_make% all
goto END
)
REM COMPIL...
echo -%CL_compilo% %CFLAGS%-
call %CL_compilo% %CFLAGS%
IF %ERRORLEVEL% EQU 0 (
echo -%CL_compilo% -o"%DIR%%FILE%.exe" "%DIR%%FILE%.%OBJ_Ext%"-
call %CL_compilo% -o"%DIR%%FILE%.exe" "%DIR%%FILE%.%OBJ_Ext%"
)
IF %ERRORLEVEL% EQU 0 (del "%DIR%%FILE%.%OBJ_Ext%")
goto END
REM Open HTML files in web browser (html and htm)
:_HTML
start /max /wait %FPATH%
goto END
REM ... END ...
:END
echo.
IF /I "%2" == "-pause" pause
Solution 5:
I've made a single powerfull script that will:
-Compile and run multi language code like C
, C++
, Java
, Python
and C#
.
-Delete the old executable before compiling code.
-Only run the code if it's compiled successfully.
I've also made a very noob friendly tutorial Transform Notepad++ to Powerful Multi Languages IDE which contains some additional scripts like to only run or Compile the code, run code inside CMD etc.
npp_console 1 //open console
NPP_CONSOLE - //disable output of commands
npe_console m- //disable unnecessary output
con_colour bg= 191919 fg= F5F5F5 //set console colors
npp_save //save the file
cd $(CURRENT_DIRECTORY) //follow current directory
NPP_CONSOLE + //enable output
IF $(EXT_PART)==.c GOTO C //if .c file goto C label
IF $(EXT_PART)==.cpp GOTO CPP //if .cpp file goto CPP label
IF $(EXT_PART)==.java GOTO JAVA //if .java file goto JAVA label
IF $(EXT_PART)==.cs GOTO C# //if .cs file goto C# label
IF $(EXT_PART)==.py GOTO PYTHON //if .py file goto PYTHON label
echo FILE SAVED
GOTO EXITSCRIPT // else treat it as a text file and goto EXITSCRIPT
//C label
:C
cmd /C if exist "$(NAME_PART).exe" cmd /c del "$(NAME_PART).exe"//delete existing executable file if exists
gcc "$(FILE_NAME)" -o $(NAME_PART) //compile file
IF $(EXITCODE) != 0 GOTO EXITSCRIPT //if any compilation error then abort
echo C CODE COMPILED SUCCESSFULLY: //print message on console
$(NAME_PART) //run file in cmd, set color to green and pause cmd after output
GOTO EXITSCRIPT //finally exits
:CPP
cmd /C if exist "$(NAME_PART).exe" cmd /c del "$(NAME_PART).exe"
g++ "$(FILE_NAME)" -o $(NAME_PART)
IF $(EXITCODE) != 0 GOTO EXITSCRIPT
echo C++ CODE COMPILED SUCCESSFULLY:
$(NAME_PART)
GOTO EXITSCRIPT
:JAVA
cmd /C if exist "$(NAME_PART).class" cmd /c del "$(NAME_PART).class"
javac $(FILE_NAME) -Xlint
IF $(EXITCODE) != 0 GOTO EXITSCRIPT
echo JAVA CODE COMPILED SUCCESSFULLY:
java $(NAME_PART)
GOTO EXITSCRIPT
:C#
cmd /C if exist "$(NAME_PART).exe" cmd /c del "$(NAME_PART).exe"
csc $(FILE_NAME)
IF $(EXITCODE) != 0 GOTO EXITSCRIPT
echo C# CODE COMPILED SUCCESSFULLY:
$(NAME_PART)
GOTO EXITSCRIPT
:PYTHON
echo RUNNING PYTHON SCRIPT IN CMD: //python is a script so no need to compile
python $(NAME_PART).py
GOTO EXITSCRIPT
:EXITSCRIPT
// that's all, folks!