4.6.3. Processing & replacing the selection

There are two ways (or forms) to get the text selected in Notepad++:

  1. Using pre-defined [3.8.1] variable $(CURRENT_WORD) which can store limited number of selected characters;
  2. Using SEL_SAVETO "FilePathName" to save the whole selected text into a text file specified.

Then the selected text can be processed by some external tool as shown in [4.6.1].

There are two ways (or forms) to replace the selected text with the text you want:

  1. Using SEL_SETTEXT "TextToReplaceWith" which can also use some variable as its argument e.g. SEL_SETTEXT $(FILE_NAME);
  2. Using SEL_LOADFROM "FilePathName" to load the text from a text file specified.

NppExec allows you to use both ways to process the selected text with some external tool and then to replace the text with the tool's output. Several steps are required:

1) Pass the selected text to the external tool. The selected text is the external tool's input:

// input: command line, output: command line
tool.exe <args> "$(CURRENT_WORD)"

or

// input: command line, output: file
cmd /c tool.exe <args> "$(CURRENT_WORD)" >$(SYS.TEMP)\out.txt

or

// input: file, output: command line
SEL_SAVETO $(SYS.TEMP)\npp_sel.txt
tool.exe <args> $(SYS.TEMP)\npp_sel.txt

or

// input: file, output: file
SEL_SAVETO $(SYS.TEMP)\npp_sel.txt
cmd /c tool.exe <args> C:\npp_sel.txt >$(SYS.TEMP)\out.txt

2) Replace the selected text with the tool's output:

// replace the selected text with the output file's content
SEL_LOADFROM $(SYS.TEMP)\out.txt

or

// replace the selected text with the command line's output:
SEL_SETTEXT $(OUTPUT)

The very last approach needs several additional steps. The $(OUTPUT) variable stores the whole external tool's output while NPE_CONSOLE V+ has been set. Let's see an example of such approach.

Let's assume you select a word such as "copy", "dir", "cd" etc. and want to replace this word with its description from cmd's help. You can use $(CURRENT_WORD) variable to pass the selected word to cmd.exe. Also you want to replace the selected word with cmd's output. You can use $(OUTPUT) variable to do it. Here is the full NppExec's script which allows you to get the desired result:

// enable $(OUTPUT) variable
NPE_CONSOLE v+
// run cmd.exe with the selected word as parameter
cmd.exe /c $(CURRENT_WORD) /?
// (optional) replace the selected text: previously selected word
SEL_SETTEXT $(CURRENT_WORD)
// (optional) replace the selected text: new line
SEL_SETTEXT+ \n
// replace the selected text: cmd's output
SEL_SETTEXT $(OUTPUT)
// finally, disable $(OUTPUT) variable
NPE_CONSOLE v-

That's all. Now try the following:

  1. Create new file in Notepad++ (don't even save it yet)
  2. Type, for example, "dir" (without quotes)
  3. Select the word "dir" (without quotes)
  4. Open NppExec's Execute window, select "<temporary script>"
  5. Copy the script shown above to the Execute window's edit-box
  6. Press OK
  7. Enjoy the result :-)

The $(OUTPUT) variable is disabled by default to minimize the memory usage as this variable stores the whole external process'es output when enabled.

You can clear this variable by assigning an empty value to it:

SET $(OUTPUT) =

To free the memory allocated for this variable, use the following:

UNSET $(OUTPUT)