ilp*_*ipe 8 python linux vim bash stata
我一直在大学使用Vim在Windows中编写Stata脚本一段时间.我正在学习R,我想完全切换到Linux作为我的操作系统(我最近在笔记本电脑上切换到Ubuntu).R在Windows和Linux中都可以与Vim一起使用,但是我有时候还需要使用Stata.在Windows中,我一直使用Stata用户提供的简单AutoIt脚本将行/整个文件发送到stata进行评估.这个脚本在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
Run Code Online (Sandbox Code Playgroud)
在我的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>
Run Code Online (Sandbox Code Playgroud)
这非常实用,实际上是我仍然坚持使用Windows的唯一原因.我如何为Ubuntu获得类似的东西?我是linux新手,除了统计数据之外,对编程还不太了解.任何帮助是极大的赞赏.(请不要建议emacs,emacs对stata的支持是错误的,虽然它与R的集成要好得多,但我现在想继续使用Vim.)
关于一个可能相关的话题:我正在考虑学习Python,因为我可能会处理数据并进行更长时间的实证分析,我认为它可能对某些任务有用,例如解决这样的问题或解析来自网站的数据.这是推荐的,还是我应该看另一种语言(或完全忘记这个想法)?
我很少再使用 stata,但在某些时候在 bash 中找到了一个快速解决方案。这个脚本在我的 .vimrc 中用几行执行,工作得很好。您可能需要根据您的系统调整延迟。
#!/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
Run Code Online (Sandbox Code Playgroud)
调整这个并将其放入你的 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>
Run Code Online (Sandbox Code Playgroud)