Keith Kranker

Department of Economics
University of Maryland
College Park, MD 20742

Graduate Program:
301-405-3544

Undergraduate Program:
301-405-3266

Programs & Code

Running Stata code from Notepad++

Notepad++ is currently my favorite because it is so easy to add literally any computer language or modify how they are displayed. Here's a screenshot of a Stata .do file:

Image

Adding colors, underlining, and so makes the code much easier to stare at. It also help check your code since "replase" won't be highlighted but "replace" will. It also allows the enhancements many text editors offer: tabbed file viewing, saved sessions, and all that jazz.

Great! But what's this stuff for? When you are in Notepad++, editing your latest .do or .ado file and want to try out the code: if you've followed these directions, you can just hit the F8 or F9 button on your keyboard and send the file to Stata.

Credits

This is posted simply to add to Friedrich Huebler's excellent page explaining how to pass files into Stata. These are just a few notes on how to (only slightly) tweak the code for use with Notepad++. Friedrich Huebler deserves full credit for the code. I simply tweaked the code for this particular application. (after a little time to figure out AutoIt3). The only reason worth posting this page is for a few comments.

There was also a Statalist post from Jeffrey Arnold with some modifications of Huebler's code. The advantage is that the script feeds the commands into Stata via a text file (instead of cutting & pasting it in). This is the approach I use below. It also uses an .ini file to control program names.

The programs posted below are mostly composed of a verbatim combination of these scripts. I give full credit to them for the "heavy lifting." Looking for Help

For a while, I've had the idea to write a plug-in to extend the capability of Stata and Notepad++. What I would like to do is write a plugin for Notepad++ to be able to create "Cells" of data which can be sent to Stata with one keyboard shortcut. This already exists in Matlab's built-in editor. In theory, this wouldn't be Stata-specific; it could be used for Matlab, SAS, and other programs.

See more on the idea here.

I haven't started trying, mainly because I don't have experience writing Notepad++ plug-ins. If you are interested in teaming up on this, contact me by email.

Steps to run with Notepad ++
  1. If you haven't installed Notepad++, do so first.
  2. In principle, you can download the scripts from Friedrich Huebler's page can be dowloaded an installed as-is, using his directions and a little guidance from below.

    However, I have changed the code somewhat, (see "Credits" above) and therefore recommend downloading the files listed at the bottom of this page. Download them all into the same folder.
  3. Edit the .ini file to match your installation of Stata.

    You will need to change the StataExe and StataWin keys.
  4. Use AutoIt3 to compile these scripts.

    Huebler provides detailed instructions. In short, this amounts to downloading and installing Autoit, open the .au3 files, and hitting Ctrl+F7 on your keyboard. In the same folder as your .au3 files, you will now find .exe files of the same filename.
  5. Use Notepad++'s "Run" capability to run your script file.

    - Find the shortcuts.xml file for your installation of Notepad ++

    Open this file with a text editor and insert the following lines of text:

    <Command name="Stata: Do Current File" Ctrl="no" Alt="no" Shift="no" Key="120">&quot;C:\{your filepath}\SEND2STATA.KAK.EXE&quot; do &quot;$(FULL_CURRENT_PATH)&quot;</Command>

    <Command name="Stata: Include Current File" Ctrl="no" Alt="no" Shift="no" Key="120">&quot;C:\{your filepath}\SEND2STATA.KAK.EXE&quot; include &quot;$(FULL_CURRENT_PATH)&quot;</Command>

    <Command name="Stata: Run Current File" Ctrl="no" Alt="no" Shift="no" Key="120">&quot;C:\{your filepath}\SEND2STATA.KAK.EXE&quot; run &quot;$(FULL_CURRENT_PATH)&quot;</Command>

    <Command name="Stata: Run Selected Text" Ctrl="no" Alt="no" Shift="no" Key="119">&quot;C:\{your filepath}\RUNDOLINES.KAK.EXE&quot;</Command>

    <Command name="Stata: Help for selected text" Ctrl="yes" Alt="no" Shift="no" Key="119">&quot;C:\{your filepath}\HELPDOLINES.KAK.EXE&quot;</Command>


    • Obviously, replace {your filepath} above with the actual filepath to your new .exe files. The quotation marks to surround the filepaths as &quot;. These will be replaced (by Notepad++) with actual quotes.
    • The shortcuts.xml file is often C:\Documents and Settings\{Windows User Name}\Application Data\Notepad++\ in Windows XP or C:\Users\{Windows User Name}\AppData\Roaming\Notepad++ in Windows Vista. You should be able to find it by typing the following address into Windows Explorer: %APPDATA%\Notepad++
    • After saving shortcuts.xml, you will need to restart Notepad++
    • The use of $(FULL_CURRENT_PATH) is explained on this Notepad++ FAQ page

  6. It should now work:
    • Hit F9 --> Run the .do or .ado file in Notepad++
    • Select some text, hit F8 --> runs the text in the current Stata window (via a temporary text file and the include command)
    • Select a word, hit Ctrl+F8 --> look up the word in Stata's help menu.
    • Note: Some people seem to be having trouble with the F8 button. If so, open Notepad++ and go to Settings, Shortcut Mapper. Make sure that you do not already have F8 assigned to something else. This is a problem with the latest versions of Notepad++.

  7. Add your own custom syntax highlighting for .do and .ado files.

    In Notepad++, go to View -> User Define Dialog...
    Then, set up syntax highlighting according to your own preferences.

    Hint: in Stata, use the command net search getcmds since it's not part of the Stata package. Then, run the command. Next simply cut and paste everything from the output file into one of the boxes on the "Keywords List" tab.

    I have also posted an example userDefineLang.xml file for Stata below.  
  8. You can use a similar process to set up auto-completeion.
Other Notepad++ Help
  • For (1) general information about text editors and Stata and (2) directions about syntax highlighting with Notepad++ for Stata .do and .ado files: check out this page
  • Following recommendations from this page , I found it useful to added the following two lines to my profile.do file:
    window menu append item "stUser" "Notepad++" "winexec C:\PROGRA~1\NOTEPA~1\NOTEPA~1.exe"

    global F9 winexec C:\PROGRA~1\NOTEPA~1\NOTEPA~1.exe;
Download files here

- helpdolines.kak.au3
- rundolines.kak.au3
- send2stata.kak.au3
- send2stata.ini
- userdefineLang_stata.xml

- userdefineLang_current.xml (the file I am using 11-25-08, with SAS, Stata, and Stat-transfer)
- Stata.xml Place in C:\Program Files\Notepad++\plugins\APIs for auto-completion (1-12-09)

Running Stat-Transfer command files from Notepad++

Similar to writing and running Stata do files from Notepad++ (above), I have begun to do the same for Stat-Transfer Files.

  • I have posted a userdefinelang.xml file here: userdefinelang_stat_txfr.xml
  • To your shortcuts.xml file, add something similar to the following:
<Command name="Stat-Transfer command file" Ctrl="no" Alt="yes" Shift="no" Key="120">"C:\Program Files\StatTransfer9\st.exe" "$(FULL_CURRENT_PATH)"</Command>

LaTeX

LaTeX formatting is built-in to Notepad++. If you've installed Miktex 2.7 and Adobe Reader on windows, here's the Run command:

<Command name="PDFLaTex" Ctrl="yes" Alt="no" Shift="no" Key="190">cmd /c cd /d "$(CURRENT_DIRECTORY)" && pdflatex.exe -shell-escape "$(FILE_NAME)" && DEL "$(NAME_PART).log" && DEL file "$(NAME_PART).aux"</Command>
<Command name="PDFLaTex" Ctrl="yes" Alt="no" Shift="yes" Key="190">"C:\Program Files\Adobe\Reader 8.0\Reader\acrord32.exe" "$(CURRENT_DIRECTORY)\$(NAME_PART).pdf"</Command>

Run anything from one key

I recently received an email from Jelmer Ypma, who has posted an AutoIt script that can be used to run the file currently open in Notepad++ in a program of choice (based on the file extension). You can quickly see how the .ini file works; it appears you could extend the idea to nearly any file extension.