<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML lang="en">
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<link href="style.css" rel="stylesheet" type="text/css">
<title>4.6.1. Applying external tool to selected text</title>
<script src="show_matches.js" type="text/javascript"></script>
</HEAD>
<BODY>
<h3>4.6.1. Applying external tool to selected text</h3>
<p>Let's suppose you want to apply some AWK program to your selected text in Notepad++. </p>
<p>The first thing you require is NppExec plugin ;) </p>
<p>The second thing you require is
    an AWK
executable, for example, &quot;awk95.exe&quot; or &quot;gawk.exe&quot;.</p>
<p>When you want to apply some AWK
    program to your selected text, this AWK program must exists, isn't it? So,
    create
a file &quot;do.awk&quot; in your Notepad++'s folder. The full path to this file is &quot;$(NPP_DIRECTORY)\do.awk&quot;. </p>
<p>OK, the file is created, now you
    need some AWK program. Open the &quot;do.awk&quot; in Notepad++ and type your program,
    for
example:</p>
<blockquote>
  <pre>{ print $0 }</pre>
</blockquote>
<p>This simple AWK program will print each record (i.e. text line) from the input file given. </p>
<p>Now, open another file in Notepad++ and select some text (few lines, for example). </p>
<p>Now you have the selected text and a tool (awk95.exe or gawk.exe) you want to apply to this selected text. </p>
<p>The only thing remaining is NppExec's script which will allow you to do what you want.</p>
<p>Press F6 (or Plugins -&gt; NppExec -&gt; Execute NppExec Script...)
and type: </p>
<blockquote>
  <pre>// full path to AWK executable 
set local AWK_EXE = C:\tools\awk\awk95.exe 
// this temporary file name will be used 
set local TEMP_FILE = $(SYS.TEMP)\npp_sel.txt 
// save current selection as ANSI text file 
SEL_SAVETO $(TEMP_FILE) :a 
// run do.awk for this file 
&quot;$(AWK_EXE)&quot; -f &quot;$(NPP_DIRECTORY)\do.awk&quot; &quot;$(TEMP_FILE)&quot;</pre>
</blockquote>
<p>Save this script as &quot;do.awk on selected text&quot; or
whatever you want. </p>
<p>Press OK.</p>
<p>You will see the AWK's output in the NppExec's Console window. You can copy it from there with Ctrl+C.</p>
<p>As you understand, such approach can be used for any command-line external tool such as grep, php and so on. </p>
<p>Good luck! </p>
<blockquote>
  <pre> </pre>
</blockquote>
<p>P.S. By the way, if you have nothing selected and you want to select a word under the caret, the following script can be used:</p>
<blockquote>
  <pre>// get current position...
SCI_SENDMSG SCI_GETCURRENTPOS
set local pos = $(MSG_RESULT)
// get start of a word near the pos...
SCI_SENDMSG SCI_WORDSTARTPOSITION $(pos) 1
set local wordStart = $(MSG_RESULT)
// get end of a word near the pos...
SCI_SENDMSG SCI_WORDENDPOSITION $(pos) 1
set local wordEnd = $(MSG_RESULT)
// set selection
SCI_SENDMSG SCI_SETSEL $(wordStart) $(wordEnd)
 </pre>
</blockquote>
<p>If you combine this script with the previous one, you will be able to apply an external tool to a word under the caret. (I.e. first it will select a word under the caret, and then will apply an external tool to that selected word.) Also consider ability to use $(CURRENT_WORD) instead of SEL_SAVETO in this case (see <a href="4.6.3.html">[4.6.3]</a>).</p>
<p>See also: Processing &amp; replacing the selection <a href="4.6.3.html">[4.6.3]</a>; Modify selected text and save to file <a href="4.6.17.html">[4.6.17]</a>.</p>
</BODY>
</HTML>
