How to run Stata do file in Vim
-
02-01-2020 - |
Question
I am looking for a way to run .do file of Stata from Vim.
Here is what I have checked out so far:
- In this link: http://fmwww.bc.edu/repec/bocode/t/textEditors.html#vim, Vim functions were suggested;
- The rundo.exe file could be downloaded from here: http://huebler.blogspot.com/2008/04/stata.html
However, after also changing the .ini files as suggested, I still cannot run the following Vim function, which is defined in _vimrc:
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)
" *** CHANGE PATH AND NAME TO REFLECT YOUR SETUP. USE \\ INSTEAD OF \ ***
exec "!start C:\\Program Files\\Scripts\\rundo.exe " . temp
" Delete the temp file after Vim closes
au VimLeave * exe "!del -y" temp
endfun
As I have mapped this function to <F9>
using: map <F9> :<C-U>call RunDoLines() <Enter>
, here is the error message I get when pressing <F9>
when opening profile.do
through Vim in Stata's home directory.
My Vim is on Github: https://github.com/llinfeng/Vim-Win; the extracted folder containing rundo.exe
file is on my dropbox: https://www.dropbox.com/sh/5tneckhr1rfrp5x/AABLZ4HGElbdbIGBGrvE7Zfta
Where am I getting it wrong?
PS: the good news is, the following Rundo()
function works well, except for one issue:
fun! RunIt()
wa
!start /min "C:\Users\Linfeng\rundo.exe" "%:p"
endfun
Issue: if the .do file contains an error and Stata complains about it, the Ctrl
key (or Alt
key) will be held down. This makes pressing Tab
as if Alt-Tab
, which is shifting windows. The key-lock will only be released after pressing Ctrl
for one time.
Solution
Honestly, it is my misuse of the function RunDoLines()
that has caused the problem. The function is still compatible with Vim on Windows 8.1.
For calling this function:
- Add
vnoremap
to the<F9>
mapping. - Visually select (several) line(s).
- Press
F9
.
It will not work if you simply put the cursor on one line. The line has to be visually selected. The function needs to be filled with a "selected line", which should be visually selected as far as I am concerned. Selecting a line of code in insert mode by dragging the mouse will not generate the parameters that is needed by the function.
Stay in normal mode and press V
to run the chunk of code!