Vim Tips Wiki
(move to category Latex)
m (Backward search for latex documents on Mac os x moved to Backward search for LaTeX documents on Mac OS X: Page moved by JohnBot to improve title)

Revision as of 10:39, 18 October 2007

Previous TipNext Tip

Tip: #1468 - Backward search for LaTeX documents

Created: January 11, 2007 10:14 Complexity: intermediate Author: Yonatan Amit Version: 5.7 Karma: 6/3 Imported from: Tip#1468

Hi.

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 finds himself reading the PDF and then going 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, this 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 a word in the PDFView and it will take you to the right place in your editor of choice.

The script 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

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.

The bulk of the important code is below:

#!/bin/bash

filename="$1"
lineNum="$2"

[ "${filename:0:1}" == "/" ] || filename="${PWD}/$filename"  

exec osascript \
 -e "set ESC to ASCII character of 27" \
 -e "tell application \"Vim\" to activate" \
 -e "tell application \"System Events\"" \
   -e "tell process \"Vim\"" \
   -e "keystroke ESC & \":set hidden\" & return " \
   -e "keystroke \":if bufexists('$filename')\" & return " \
   -e "keystroke \":exe \\\":buffer \\\" . bufnr('$filename')\"  & return " \
   -e "keystroke \":else \" & return " \
   -e "keystroke \":    edit ${filename// /\\\\ }\" & return " \
   -e "keystroke \":endif\" & return " \
   -e "keystroke \":$lineNum\" & return " \
   -e "keystroke \"zO\" " \
   -e "end tell" \
 -e "end tell"

--TedPavlic 22:40, 11 July 2007 (UTC)