Making Vim run Stata and clean up after itself

Last week I mentioned that in the course of switching from Notepad++ to Vim I lost the ability to run Stata do-files or selected lines from within the text editor, and I asked my readers for help if they had a solution. What do you know, one of them did, and wrote to me all the way from China with a Vim script that solves both problems. I won't repeat it here because it is almost identical to this one. Clearly, I need to remember to always look into the Statalist archive first.

There was one line that I had to change, where Vim is ordered to clean up after itself. Every time you runDoLines() you produce a bunch of .tmp.do files in your %temp% directory, which Vim knows as $TEMP. Though this

au VimLeave * exe "!del -y " temp


should have deleted them, on my computer it did not. This one did:

au VimLeave * silent exe '!del /Q "'.$TEMP.'\*.tmp.do"'


Another thing I tinkered with was the way Vim handles these backup files that end in a tilde. You can disable them entirely, by adding

set nobackup nowritebackup


to _vimrc as explained here. You can also have Vim keep them somewhere you can delete them explicitly, in bulk, as explained here. Finally, you can use a combination of both: you save backup files while Vim is working, but then you make it delete them all when it starts next time. That is explained here. I went with it, so after I created a temp folder in $VIMRUNTIME, I added this to my _vimrc file:

" Keeps backups in $VIMRUNTIME\temp folder, cleans them up
silent execute '!mkdir "'.$VIMRUNTIME.'\temp"'
silent execute '!del /Q "'.$VIMRUNTIME.'\temp\*~"'
set backupdir=$VIMRUNTIME\\temp\\
set directory=$VIMRUNTIME\\temp\\


In the process of figuring this out, I also discovered that there's a quick way to clean up %temp% thoroughly, as shown here. Good to know. One of these days I might add that to _vimrc too. Now I'm on a roll.

3 Responses to “Making Vim run Stata and clean up after itself”

  1. “Edit with Vim” in Windows 7 | A Stata Mind writes:

    [...] write any backup files (the ones with the tilde after the extension) into the temp directory as I originally intended. I'm not sure how to fix this, so I cobbled together the workaround [...]

  2. another stata user writes:

    hey,

    I have also used this solution for windows, but I also work with R, usually in Ubuntu. I would like to completely switch to linux, do you know of any fix that lets you send code from vim to stata/xstata in linux?

  3. Gabi Huiber writes:

    R in Ubuntu is my alternative setup too. I don't yet have Stata there. But Andrew does. Ask him.

Leave a Reply