从VIM发送code与Stata [英] sending code from vim to stata

查看:314
本文介绍了从VIM发送code与Stata的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用议会编写在Windows Stata的脚本,而现在在大学。我此刻学习R,我想我的OS完全切换到Linux(我最近改用Ubuntu的我的笔记本电脑)。 ř工作正常的Vim在Windows和Linux,但我还需要有时使用Stata的。在Windows中我一直在使用由Stata的用户提供了一个简单的AutoIt脚本送线/整个文件与Stata进行评估。在Linux中该脚本不工作。

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.

这是剧本的样子

; AutoIt v3 script to run a Stata do-file from an external text editor
; Version 3.1, Friedrich Huebler, fhuebler@gmail.com, 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

在我的vimrc以下

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> 

这是真的实用,几乎是唯一的原因,我仍然坚持到Windows。我将如何去获得类似的东西的Ubuntu?我是新来的Linux,和真的不知道很多有关统计数据除编程。任何帮助是极大的AP preciated。
(请不要提出的Emacs,对于STATA emacs的支持是错误的,虽然它与R集成是好多了,我想使用议会现在保持。)

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.)

在一个可能的相关主题:我正在考虑学习Python的,因为我可能会使用数据和做实证分析更长的时间,我想可能对于某些任务是有用的,例如要解决这样或从网站分析数据问题。这是建议,或者我应该看看另一种语言(或者忘记的想法完全)?

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)?

推荐答案

我很少用STATA了,但在某些时候摸索出在bash的快速解决方案。这个脚本,在我的.vimrc几行执行的,做工精细。你可能根据你的系统延迟进行调整。

I rarely use stata anymore, but at some point worked out a quick solution in bash. this script, executed with a few lines in my .vimrc, works fine. you might have to adjust the delays depending on your system.

#!/bin/bash

# needs wmctrl, xte and xsel
# to get them run
# sudo apt-get install wmctrl xautomation xsel
# in debian/ubuntu linux

#copy to clipboard "do filename"
echo 'do ' '"'$1'"' | xsel -b

# get current window id
winid = $(xprop -root | awk '/_NET_ACTIVE_WINDOW\(WINDOW\)/{print $NF}')

# check for stata window, if found activate else execute
# use correct version here
if [ "$(pgrep stata)" ] 
then
    wmctrl -a 'Stata/MP 11.2'
else
    xstata &
    sleep .1 
fi

# delay depends on window manager etc
# .1 ok with xmonad in 10.04
sleep .1 

# switch to command line, ctrl-4 in stata 10, ctrl-1 in 11/12
# and select existing text via ctrl-a
xte 'keydown Control_L' 'key 1' 'key A' 'usleep 100' \
    'key V' 'keyup Control_L' 
sleep .1
xte 'key Return'
sleep .3


# go back to editor window
wmctrl -i -a $winid 

调整此并把它放在你的vimrc。

adjust this and put it in your vimrc.

"" STATA DO-FILE SCRIPTS
fun! RunIt()
  w
  "adjust this path to the bash script
  !sh "/home/username/.rundo.sh" "%:p"
endfun
au FileType stata noremap <F8> :<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)
          "adjust this path to the bash script
          exec "!sh /home/username/.rundo.sh " . temp
    "au VimLeave * exe "!del -y" temp
    au VimLeave * silent exe '!del /Q "'.$TEMP.'\*.tmp.do"'
endfun
au FileType stata noremap <F9> :<C-U>call RunDoLines()<CR><CR> 

这篇关于从VIM发送code与Stata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆