Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending code from vim to stata

I have been using Vim to write Stata scripts in Windows for a while now at the university. I am learning R at the moment, and I want to switch completely to Linux as my OS (I've recently switched to Ubuntu on my laptop). R works fine with Vim in both Windows and Linux, however I still need to use Stata sometimes. In Windows I have been using a simple AutoIt script provided by a Stata user to send lines / the whole file to stata for evaluation. This script doesnt work in Linux.

This is what the script looks like

; AutoIt v3 script to run a Stata do-file from an external text editor
; Version 3.1, Friedrich Huebler, [email protected], www.huebler.info, 30 March 2009

; Declare variables
Global $ini, $statapath, $statawin, $dofile, $winpause, $keypause, $clippause

; File locations
; Path to INI file
$ini = @ScriptDir & "\rundo.ini"
; Path to Stata executable
$statapath = IniRead($ini, "Stata", "statapath", "C:\Program Files\Stata10\wsestata.exe")
; Title of Stata window
$statawin = IniRead($ini, "Stata", "statawin", "Stata/SE 10.1")

; Path to do-file that is passed to AutoIt
; Edit line to match editor used, if necessary
$dofile = $CmdLine[1]

; Delays
; Pause after copying of Stata commands to clipboard
$clippause = IniRead($ini, "Delays", "clippause", "100")
; Pause between window-related operations
$winpause = IniRead($ini, "Delays", "winpause", "200")
; Pause between keystrokes sent to Stata
$keypause = IniRead($ini, "Delays", "keypause", "1")

; Set SendKeyDelay and WinWaitDelay to speed up or slow down script
Opt("WinWaitDelay", $winpause)
Opt("SendKeyDelay", $keypause)

; If more than one Stata window is open, the window 
; that was most recently active will be matched
Opt("WinTitleMatchMode", 2)

; Check if Stata is already open, start Stata if not
If WinExists($statawin) Then
  WinActivate($statawin)
  WinWaitActive($statawin)
  ; Activate Stata Command Window and select text (if any)
  Send("^4")
  Send("^a")
  ; Run saved do-file
  ; Double quotes around $dofile needed in case path contains blanks
  ClipPut("do " & '"' & $dofile & '"')
  ; Pause avoids problem with clipboard, may be AutoIt or Windows bug
  Sleep($clippause)
  Send("^v" & "{Enter}")
Else
  Run($statapath)
  WinWaitActive($statawin)
  ; Activate Stata Command Window
  Send("^4")
  ; Run saved do-file
  ; Double quotes around $dofile needed in case path contains blanks
  ClipPut("do " & '"' & $dofile & '"')
  ; Pause avoids problem with clipboard, may be AutoIt or Windows bug
  Sleep($clippause)
  Send("^v" & "{Enter}")
EndIf

; End of script

with the following in my vimrc

" STATA DO-FILE SCRIPTS

fun! RunIt()
  w
  !start "C:\Programme\Stata10\integvim\rundo3\rundo.exe" "%:p"
endfun
map <F8> :<C-U>call RunIt()<CR><CR>
imap <F8> <Esc>:<C-U>call RunIt()<CR><CR>

fun! RunDoLines()
  let selectedLines = getbufline('%', line("'<"), line("'>"))
 if col("'>") < strlen(getline(line("'>")))
  let selectedLines[-1] = strpart(selectedLines[-1], 0, col("'>"))
  endif
 if col("'<") != 1
  let selectedLines[0] = strpart(selectedLines[0], col("'<")-1)
  endif
 let temp = tempname() . ".do"
  call writefile(selectedLines, temp)
    exec "!start C:\\Programme\\Stata10\\integvim\\rundo3\\rundo.exe " . temp
    au VimLeave * exe "!del -y" temp
endfun
map <F9> :<C-U>call RunDoLines()<CR><CR> 
imap <F9> <Esc>:<C-U>call RunDoLines()<CR><CR> 

This is really practical and virtually the only reason I'm still sticking to Windows. How would I go about getting something like that for Ubuntu? I'm new to linux, and dont really know much about programming besides statistics. Any help is greatly appreciated. (Please dont suggest emacs, emacs support for stata is faulty, and though its integration with R is much better, I would like to keep using Vim for now.)

On a possibly related topic: I'm considering learning Python, as I'll probably be working with data and doing empirical analysis for a longer time, and I think it might be useful for some tasks, e.g. to solve problems like this or parsing data from websites. Is this recommended, or should I look at another language (or forget the idea completely)?

like image 348
ilprincipe Avatar asked Nov 19 '10 14:11

ilprincipe


1 Answers

If you're going to switch to linux (for good) you should 1. call stata and switch your license to a linux license (which they'll do for free) and install it natively, then starting stata from vim just requires a bash script (i'm no expert on vim) 2. or, you could buy stata 11, which has one license for all supported platforms 3. you could install stata with wine (play on linux made it easier for me) but I couldn't get wine to give stata more than half a gig of ram, so i installed native stata10

I use gedit for python, stata, R, latex, et cetera, it handles anything, and gtksourceview is pretty easy to add syntax highlighting and styles et cetera..

Python is great for data analysis, please see

http://www.scipy.org/ http://openopt.org/Welcome http://www.macworld.com/appguide/app.html?id=63924&expand=false

For scraping websites theres

http://scrapy.org/ http://wwwsearch.sourceforge.net/mechanize/

and for parsing data out of text generally we have, http://pyparsing.wikispaces.com/

I have a bunch of files you might find useful (gtksoureview language definitions for stata and others, pdf copies of some books (some copyleft)) but i don't know how to attach files on this website

like image 83
justin cress Avatar answered Sep 18 '22 09:09

justin cress