Backward search for LaTeX documents on Mac OS X
From Vim Tips Wiki
Tip 1468 Previous Next created January 11, 2007 · complexity intermediate · author Yonatan Amit · version 5.7
Latex is a great language for typesetting scientific documents.
The content of the document is written in some wonderful editor (Vim) and compiled to a PDF with all the bells and whistles.
When editing a document, one often reads the PDF, then goes back to the editor to change a couple of words.
A great feature found in some other editors is the ability to jump to the word under the cursor directly from the viewer. That greatly reduces time spent while editing the document.
Luckily, there is a nice package called pdfsync that can be used to add this cool feature to Vim.
pdfsync and a supporting viewer (I strongly recommend PDFView) can be configured to run a custom script when the user command-clicks inside the document.
Sadly, the current version of Vim for Mac doesn't support clientserver feature of the standard version, so I could not use the --remote flag to send the command to Vim.
So I hacked together a small UI script that tells the windowing environment to perform the remote actions.
Together. you can now point to a word in the PDFView and it will take you to the right place in your editor of choice.
The AppleScript itself:
on run argv
set CR to ASCII character of 13
set ESC to ASCII character of 27
set filename to (item 1 of argv)
set lineNum to (item 2 of argv)
tell application "Vim" to activate
tell application "System Events"
tell process "Vim"
keystroke ESC & ":set hidden" & CR
keystroke ":if bufexists('" & filename & "')" & CR
keystroke ": buffer " & filename & CR
keystroke ":else " & CR
keystroke ": edit " & filename & CR
keystroke ":endif" & CR
keystroke ":" & lineNum & CR
keystroke "zO" -- open folds, if any exists.
end tell
end tell
end run
[edit] Comments
I have just uploaded a bash script that updates this AppleScript.
The improvements:
- It's a bash script, and so it's easier to use in PDFSync-compatible applications like Skim.
- It has no problems when you try to open two different files with the same base name.
- It has no problems with filenames with spaces.
TedPavlic July 2007
