External Programs

From Notepad++ Wiki
Jump to: navigation, search
How to Run an external program from Notepad++


You may need to run the external tools (ex : compiler, interpreter or web browser) from Notepad++ (via Run dialog) by passing the current edited document as argument. To do so, you have to use environment variables. The NppExec plugin will give you plenty of extra flexibility.

File level environment variables

The usage of environment variable is :

$( ENVIRONMENT_VARIABLE ) 

Say the current file you edit in Notepad++ is:

E:\my Web\main\welcome.html 

There are more environment variables containing information on the current session:

FULL_CURRENT_PATH  : E:\my Web\main\welcome.html  
CURRENT_DIRECTORY  : E:\my Web\main\  
FILE_NAME  : welcome.html  
NAME_PART  : welcome  
EXT_PART  : html  
SYS.<var> :  system environment variable, e.g. $(SYS.PATH)

Note that you should put the double quote around the path environment variables :

"$(ENVIRONMENT_VARIABLE)" 

since they may contain some white space.

Examples

firefox "$(FULL_CURRENT_PATH)" 
iexplore "$(FULL_CURRENT_PATH)" 

These 2 user commands are also included in npp.3.0.installer.exe (or later version). You can launch them by Ctrl+Alt+Shift+X and Ctrl+Alt+Shift+I respectively.

Document level environment variables

There are still more variables:

CURRENT_WORD  : it gives the word(s) you selected in Notepad++.  
CURRENT_LINE  : it gives the current line number where your cursor position is in Notepad++.  
CURRENT_COLUMN  : it gives the current column number where your cursor position is in Notepad++.  
NPP_DIRECTORY  : this variable contains the absolute path of Notepad++'s directory.  

And even more if you go through the NppExec plugin:

PLUGINS_CONFIG_DIR :  full path of the plugins configuration directory
#N :  full path of the Nth opened document (N=1,2,3...)
#0 :  full path to notepad++.exe
LEFT_VIEW_FILE :  current file path-name in primary (left) view
RIGHT_VIEW_FILE :  current file path-name in secondary (right) view

Knowing the plugin directory enables you to call any plugin function by passing the plugin name, function name and possibly arguments to rundll.exe. Or to unload a rogue plugin using regsvr /u.

examples

http://www.php.net/%20$(CURRENT_WORD) 
http://www.google.com/search?q=$(CURRENT_WORD) 
http://en.wikipedia.org/wiki/Special:Search?search=$(CURRENT_WORD) 
$(NPP_DIRECTORY)\notepad++.exe $(CURRENT_WORD) 

For the examples 1 ~ 3, we pass the argument URL + the current selected word(s) to the default browser in order to perform the search on Internet.

Whereas for the 4th example, it'll be useful if you want to open a file from current document. Consider a php file which contains a line:

include("../myFuncs.php");

Selecting ../myFuncs.php then typing the hot key you assigne to this command will open myFuncs.php in Notepad++, of course the mentioned file should exist in the indcated path.

The variable CURRENT_WORD brings you a flexible solution to configure your external commands

Running a command

When you use NppExec to run a command, the following variables are set for the command to use, before it is actually issued:

CWD :  current working directory of NppExec (use "cd" to change it)
ARGC :  number of arguments passed to the NPP_EXEC command
ARGV :  all arguments passed to the NPP_EXEC command after the script name
ARGV[0] :  script name - first parameter of the NPP_EXEC command
ARGV[N] :  Nth argument (N=1,2,3...)
RARGV :  all arguments in reverse order (except the script name)
RARGV[N] :  Nth argument in reverse order (N=1,2,3...)
INPUT :  this value is set by the 'inputbox' command
INPUT[N] :  Nth field of the $(INPUT) value (N=1,2,3...)

The external process, if aware of NppExec, can set some variables as well (new in v0.3.1):

OUTPUT :  this value can be set by the child process, see npe_console v+
OUTPUT1 :  first line in $(OUTPUT)
OUTPUTL :  last line in $(OUTPUT)