Emacs and Stata

Although I am not a great fan of multi-tasking I often use a lot of programs simultaneously for one task: For example, when I am writing a report in Word or with Emacs, I take notes in Emacs, search the internet and have my model results in the gdx-viewer.  Additionally my mail client is somewhere in the background and nowadays I have a messenger program open to talk to my collaborators. I don”t like this jumping from one program to another, but it is often necessary. This is also one of the reasons I switched to Emacs, because I can do my main work in Emacs (modelling, note taking, statistics and report writing).
For my statistical work I use R, SPSS, Eviews and Stata. Emacs has a nice mode for working with R (ESS: Emacs-Speaks-Statistics). You can edit your scripts and  run it  in R without leaving Emacs. ESS has highlighting and lots of other features. ESS supports Stata too, but you can”t run your Stata commands in Emacs. So you have to copy-paste from Emacs to Stata. The editor of Stata (I use version 10) is  a plain text editor without syntax highlighting and almost no other features (version 11 has a much better editor but the update is too expensive).
So what can you do, if you still want to use Emacs (or any other text editor other than the Stata editor)? Use a lot of keyboard strokes switching back and fort from one program to another is one option, but here you run into other problems: if you have multi line commands copy-paste doesn’t work for Stata as you have to strip off the linefeeds. OK, you can write your commands on one line, but then you have to adjust your output if you want to document your scripts nicely. Another more practical way is using autohotkey, a free, open-source utility for Windows
(there are similar programs for Unix or Mac). Here are the great features of Autohotkey. With Autohotkey, you can (taken from the manual):
  • Automate almost anything by sending keystrokes and mouse clicks. You can write macros by hand or use the macro recorder.
  • Create hotkeys for keyboard, joystick, and mouse. Virtually any key, button, or combination can become a hotkey.
  • Expand abbreviations as you type them. For example, typing “btw” can automatically produce “by the way”.
  • Create custom data-entry forms, user interfaces, and menu bars. See GUI for details.
  • Remap keys and buttons on your keyboard, joystick, and mouse.
  • Respond to signals from hand-held remote controls via the WinLIRC client script.
  • Convert any script into an EXE file that can be run on computers that don”t have AutoHotkey installed.
For the problem described above I wrote a small script and linked it to the key combination Windows+s.
The script looks like this:

#s::                               ; the short cut (Windows key + s
clipboard =                      ; Empty the clipboard.
Send !w                          ; Copy the highlighted region in Emacs (Alt-w)
; Replace /// (necessary in Stata for multiline commands) and line feeds:
StringReplace, clipboard, clipboard, ///rn,  , All
WinActivate, Stata/SE       ; Activate the Stata window
Send ^v                           ; Copy the clipboard (Ctrl-v)
Send {Enter}                     ; Execute the commands
; Jump back to Emacs (Renger”s Emacs Mode is my Emacs window title)
WinActivate, Renger”s Emacs Mode      
return

If I mark a region in my Stata script in Emacs and hit Windows+s, the line is copied to the clipboard, all line end feeds (only in multi line commands) are stripped off and the edited region is copied to Stata and executed. After the execution the cursor jumps back to Emacs. This with just one key stroke!
I use Autohotkey for lot of other tasks (one recently was taking a sample from a telephone directory and copying the addresses to an Excel sheet). Another nice feature I implemented is automating copy-paste from a pdf-file I am reading to a text file with my notes. Once again: one click and the text is in my notes.

12 thoughts on “Emacs and Stata

  1. Hi
    I stopped using Stata and now am solely working with R, so I didn’t check if my post is still up-to-date.

    Sorry
    Renger

  2. Your friend Jan (at the World Bank) directed me to your blog for tips with Emacs. It’s been very helpful so far!

    I recently use ESS with Stata and it seems like command can be send directly the an inferior ESS buffer running Stata now. Has this been your experience? I know that the blog post says the contrary, but it’s also quite old.

    One issue I still have with ESS / Stata is that commands are sent to stata, even though it ideally should be stata-se instead.

    What’s your setup with Emacs + Stata now? Do you run into the issue above?

  3. I made some improvements to your Autohotkey code that you, and your blog readers, might find useful. The main changes allow it to work with any text editor (no configuration needed) and allow it to remove comments that cause problems.

    Thanks for the great resource.

    ;Copy command and run in stata
    #d:: ; the shortcut (Windows key + d)
    ClipSaved := ClipboardAll ; Save the entire clipboard to a variable of your choice.
    Clipboard= ;empty the clipboard
    send ^c ; copy the highlighted region
    ClipWait, 4
    WinGetActiveTitle, active_editor_title ; save title of current active window so we can switch back
    ; Replace /// (necessary in Stata for multiline commands) and line feeds:
    StringReplace, clipboard, clipboard, ///rn, , All
    clipboard := RegExReplace(clipboard, “\/\*(?:.|[\r\n])*?\*\/”) ; Remove comments delimited by /* */ (helped by: http://blog.ostermiller.org/find-comment)
    clipboard := RegExReplace(clipboard, “\/\/.*”) ; Remove comments delimited by //
    clipboard := RegExReplace(clipboard, “\R+\R”, “rn”) ; Remove blank lines
    WinActivate, Stata ; Activate the Stata window
    Send ^v ; Paste the clipboard (Ctrl-v)
    Sleep, 1000
    Send {Enter} ; Execute the commands
    ; Jump back to previous window
    WinActivate, %active_editor_title%
    Clipboard := ClipSaved ; Restore the original clipboard. Note the use of Clipboard (not ClipboardAll).
    ClipSaved = ; Free the memory in case the clipboard was very large.
    return

  4. Thankyou very much for this help. It is very comfortable. Do you have a solution if the stata-code contains comments, for example:
    disp “x” // test
    The problem is that stata gives error if marked codes with comments are copied into the command windows.

  5. TY for posting, it had been very handy and told a great deal. Can I reference some of this on my page if I incorporate a mention of the this website?

  6. ado is not working properly at the moment with my Windows 7 setup: The +-sign is dropped when something like generate test = c01 + c02. I informed the develper but he hasn’t found a solution yet. As soon as it will be working again, I will switch to ado…

  7. Start autohotkey and there will be an icon with a white H on a green background on the lower right side. Right click this icon and choose “Edit this script”. Add the code and save the script. Reload it (once again by right clicking on the icon and choosing this option). That should do it.

Comments are closed.