I am regularly using stata at work. My text editor of choice is (g)vim. I have been using the scripts provided here or here to send code from vim to stata. This functionality is immensely practical and virtually the only thing the keeps me from switching to linux completely. The scripts are written in AutoIT, so I cannot use them in linux. They are also basically independent from the choice of text editor, the people who wrote them are using notepad++.
Essentially, these scripts together with a few lines in my vimrc allow me to send selections or the whole file to a running stata window (if none is open, stata is started first).
I am looking for a solution that does this in linux, but I have no idea where to start. In linux there are two different stata versions, stata for the command line and xstata is the gui version. I need to use the gui version as the functionality of the command line version is limited unfortunately, so screen/tmux are ruled out.
If this is trivial, I'm really sorry for missing it and would be immensely grateful for a solution. I also wasn't able to find an existing plugin for vim I could utilize. If not, I am willing to invest some time and figure out how to implement a solution. A pointer in the right direction would however be quite helpful. I am relatively new to linux and programming in general, but willing to learn.
Regarding tools: I don't know bash, but it is something I want to look into anyways at some point. I have dabbled a bit in python, so that would also be ok. In case there is anything else absolutely superior for this task, please let me know.
Any help is greatly appreciated. The AutoIT scripts are hosted on the website, but I can post my windows setup here if necessary.
EDIT
Ok, after some debate in the comments, here is the essential AutoIT script that I need to translate. (I would prefer a solution that doesn't overwrite the content of the system clipboard each time though.)
Edit2 I guess this is what the script does essentially: It checks for an open stata window, selects it (or executes one), pastes the contents that are to be executed into a temporary file, switches to the stata window, selects the command line with ctrl-1 (and anything that might already be written there with ctrl-a) and then pastes do "tempfile" into the commandline, which then executes the code that was sent. At least that is how I understand it.
FINAL COMMENT
I have worked up a solution in bash some time ago, it is posted here as an answer to a previous version of this question.
; Declare variables
Global $ini, $statapath, $statawin, $statacmd, $dofile, $clippause, $winpause, $keypause
; File locations
; Path to INI file
$ini = @ScriptDir & "\rundo.ini"
;; contents of ini file are the following
;[Stata]
;; Path to Stata executable
;statapath = "C:\Program Files\Stata11\StataSE.exe"
;; Title of Stata window
;statawin = "Stata/SE 11.2"
;; Keyboard shortcut for Stata command window
;statacmd = "^1"
;[Delays]
;; Pause after copying of Stata commands to clipboard, in milliseconds
;; Use higher number if script fails (default: 100, recommended range: 0 - 200)
;clippause = 100
;; Pause between window-related operations, in milliseconds
;; Use lower number to speed up script, higher number if script fails (default: 200)
;winpause = 200
;; Pause between key strokes sent to Stata, in milliseconds
;; Use lower number to speed up script, higher number if script fails (default: 1)
;keypause = 1
; Path to Stata executable
$statapath = IniRead($ini, "Stata", "statapath", "C:\Program Files\Stata11\StataSE.exe")
; Title of Stata window
$statawin = IniRead($ini, "Stata", "statawin", "Stata/SE 11.2")
; Keyboard shortcut for Stata command window
$statacmd = IniRead($ini, "Stata", "statacmd", "^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 WinWaitDelay and SendKeyDelay 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($statacmd)
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($statacmd)
; 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
While in Vim, you can select a range of text and run that text through an external command. In visual mode, just highlight the text you want to work with, then run :! commandname . The highlighted text will be replaced by the output of the command.
Open a new or existing file with vim filename . Type i to switch into insert mode so that you can start editing the file. Enter or modify the text with your file. Once you're done, press the escape key Esc to get out of insert mode and back to command mode.
To start using vim, just run the "vim" command on the Linux shell followed by the path of the file that you want to edit. [enter] means to press the return or enter key on your keyboard. The word --insert-- will appear at the bottom of the editor window to show that you are in insert mode now.
IronAHK is a Linux/Mono rewrite of the AutoHotKey scripting language, which is similar to AutoIt (a GUI automation / keyboard remapping tool). I haven't used IronAHK, but AutoHotkey can run AutoIt v2 scripts.
You can also look @Project Sikuli: "Sikuli is a visual technology to automate and test graphical user interfaces (GUI) using images (screenshots). Sikuli includes Sikuli Script, a visual scripting API for Jython, and Sikuli IDE, an integrated development environment for writing visual scripts with screenshots easily" (from the sikuli front page)
Another good option is Linux Desktop Testing Project (LDTP), scriptable with Python:
example:
from ldtp import *
from ldtputils import *
try:
launchapp("gedit")
if waittillguiexist("*.gedit")==0:
raise LdtpExecutionError("Gedit window does not exist")
selectmenuitem("*-gedit", "mnuFile;mnuOpen")
selectrow("dkgOpenFiles...", "tblFiles", fileName[0])
...
Maybe you could use a mechanism similar to used by this vim plugin, which perform a similar task:
http://www.vim.org/scripts/script.php?script_id=1048
This plugin sends R code to a R tool, under unix and windows (R programming language is widely used for statistical software development and data analysis).
I don't know about Stata or R language, but it seems that you could control Stata using R, as stated in http://www.r-bloggers.com/why-use-r/ :
You can easily use it anywhere. It's platform-independent, so you can use it
on any operating system. And it's free, so you can use it at any employer
without having to persuade your boss to purchase a license.
:
:
R allows you to integrate with other languages (C/C++, Java, Python) and
enables you to interact with many data sources: ODBC-compliant databases
(Excel, Access) and other statistical packages (SAS, Stata, SPSS,
Minitab).
Some Stata commands translated to R:
http://www.r-bloggers.com/stata-or-r/
If you could perform the desired task through R then probably you could use the vim plugin above unchanged.
Hope this help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With