19 January 2009

Vim/TeXShop integration

I like to edit my LaTeX documents in vim (with the VIM-LaTeX package), but for various reasons, I like to use TeXShop to actually compile and view the document. In TeXShop, make sure that the "Configure for External Editor" button is checked, and then quit it. Once you add the following lines to your ~/.vim/ftplugin/tex.vim file, you just have to hit Cmd-R for the document to automatically open in TeXShop, and compile. You can use everything else just as you would expect. It really works great.

" Run LaTeX through TexShop
function! SRJ_runLatex()
   if &ft != 'tex'
      echo "calling srj_runLatex from a non-tex file"
      return ''

   "write the file

   let thePath = getcwd() . '/'. expand("%")

   let execString = 'osascript -e "tell app \"TeXShop\"" -e "set theDoc to open ((POSIX file \"'.thePath.'\") as alias)" -e "tell theDoc to latexinteractive" -e "end tell"'
   exec 'silent! !'.execString
   return ''
no  <expr> <D-r> SRJ_runLatex()
vn  <expr> <D-r> SRJ_runLatex()
ino <expr> <D-r> SRJ_runLatex()

The <D-r> means that hitting Command-R will save and compile it. You can change it in the syntax of any of the other vim command shortcuts.

EDIT 3/24/2009: To make it compatible with 10.4, since apparently the "open" command does not return the opened document as it does in 10.5, change the osascript line to:

let execString = 'osascript -e "tell app \"TeXShop\"" -e "set theDoc to open ((POSIX file \"'.thePath.'\") as alias)" -e "try" -e "tell theDoc to latexinteractive" -e "on error" -e "set theDoc to front document" -e "tell theDoc to latexinteractive" -e "end try" -e "end tell"'

UPDATE 9/25/2011: I've switched from using TeXShop to Skim. See my new Vim-LaTeX/Skim scripts here


jdahm said...

Handy script! I'm using it now too.

Anonymous said...

cool trick, i prefer vi to the texshop editor, but the macvim latex-suite combination is a little much. this is great. i am having a problem coming back to the vi file in the terminal window though. it throws up a blank screen and when i :q to get quit, the command line has the error, "missing value". offhand, do you have any idea what that might be?

Max said...

Hmm, this seems to cause TeXShop to open the document in a new window each time you press Cmd-R. So, after compiling the document three times, TeXShop has three views of it open in three windows. Not quite usable.

Seth said...

@Max: you need to look at your TeXShop preferences again to confirm that "Configure for External Editor" is checked as stated in the above instructions.

Umar said...

Great script. I am having the same problem as Max. I have check the preferences and the Configure for External Editor is checked.

jott said...

I'm running vim 7.2 and os x 10.4. The patch worked for me. I couldn't get to work so I changed to t. That works fine. Apparently 7.2 doesn't like the key.

NOTE: Nothing worked until I put "filetype plugin on" in my ~/.vimrc file. This wasn't a default option when I compiled it. :scriptnames on command line was helpful in finding this problem.

Post a Comment