3

My setup is running a command line interface vim (using iTerm) and Skim for my pdf viewer. I would like to set up synctex with Skim, but nothing I've tried has worked. Here is what I am doing:

  • I am compiling with pdflatex and -synctex=1
  • Skim requires a command and an argument for synctex. My command is /usr/bin/vim and my argument is --remote-silent +"%line" "%file"

This didn't work. I also tried the advice offered here, which said to make a bash script that ran an automator script which would manipulate my editor. But I couldn't get this to work either (this also seems pretty clunky).

I am willing to change pdf viewer as well, so if you have suggestions for how to get synctex working with iTerm vim and other pdf viewers that would be great too.

1
  • If you can run at a terminal vim +10 test.tex OR vi +:10 test.tex and the editor opens the file with the cursor at start of line 10 then that is what your scripts should be doing once you know the mix that works then in skim you are trying to do similar e.g. /path/to/vim +:%line "%file"
    – user170109
    Mar 21, 2019 at 17:45

2 Answers 2

3

After some headaches I have synctex working between CLI vim (on osX terminal) and Skim, so I can give you an overview of my setup:

I use latexmk for compilation and following their suggestion my .latexmkrc file looks like

$pdf_previewer = 'open -a Skim';                                                                                      
$pdflatex = 'pdflatex -synctex=1 -interaction=nonstopmode';
@generated_exts = (@generated_exts, 'synctex.gz');

This may not be completely necessary for synctex use, but it lets me use simple commands in my .vimrc for compiling and viewing from a .tex document, e.g.:

nnoremap <leader>c :w<CR> :silent !latexmk -pdf -pv -halt-on-error % <CR> <C-L><CR>

I'm using Skim version 1.6.3, downloaded directly from their site.

Forward search (.tex/vim to .pdf/Skim) is pretty straightforward, just using a slightly modified command (again in my .vimrc) from the source you mentioned:

nnoremap <leader>f :w<CR>:silent !/Applications/Skim.app/Contents/SharedSupport/displayline <C-r>=line('.')<CR> %<.pdf<CR> <C-L><CR>

EDIT (2023-01-28): Forward search if you have a multi-file document is more complicated but I just worked this out so I'm adding for posterity. The above command searches for a pdf file matching the current .tex document name. If you are instead working with a master .tex file that utilizes include{} with sections/chapters, you'll have to specify the name of the output pdf and the current .tex section/chapter document in order to forward search. This answer gave me enough to go on as far as using the .synctex.gz file to automatically name the output pdf, and the forward search in my .vimrc looks like:

nnoremap <leader>f :silent !/Applications/Skim.app/Contents/SharedSupport/displayline <C-r>=line('.')<CR> <C-r>=substitute(glob("*synctex.gz"),"synctex.gz","pdf","")<CR> % <CR><C-L>

If you might be compiling documents that don't have .synctex.gz files, you can combine the approaches in an if statement:

nnoremap <leader>f 
\ :if filereadable(glob("*.synctex.gz")) 
\ <bar> silent !/Applications/Skim.app/Contents/SharedSupport/displayline <C-r>=line('.')<CR> <C-r>=substitute(glob("*synctex.gz"),"synctex.gz","pdf","")<CR> % <CR>  
\ <bar> else 
\ <bar> silent !/Applications/Skim.app/Contents/SharedSupport/displayline <C-r>=line('.')<CR> %<.pdf<CR> 
\ <bar> endif <CR><C-L>

END EDIT (2023-01-28)

Reverse search (.pdf/Skim to .tex/vim) was a bit more tricky. I found that the native vim on my mac (version 8.0, High Sierra 10.13.6) does not have the --remote features, so I installed vim version 8.2 using macports and built with +huge +gtk2 +X11 +clientserver:

sudo port install vim +huge +gtk2 +X11 +clientserver

If you don't want/have macports (or homebrew, etc) or don't want to install another vim, you could probably upgrade your mac's native vim by building from source and including the necessary pieces, but I went this route because I like to leave the native stuff alone. Note that you will need X11 installed as well.

After that, I set the command in Skim (Skim > Preferences > Sync > PDF-Tex Sync support) to /opt/local/bin/vim (the new vim I installed) and the options to --servername VIM --remote-silent +%line "%file". I found that if I open my .tex file in vim with the same command (/opt/local/bin/vim --servername VIM --remote-silent <file>), then reverse search works from Skim, using Command+Shift+Click.

I'm really not sure why I have to specify the servername here, but it does seem necessary. For example, if I open vim without specifying the servername (or use a vim that is not built with remote support) this feature doesn't work. Skim also cannot open a new vim instance, so I have to already have opened the document (with the specified command). This setup works for me, but I think one could write a shell script that Skim calls, which looks for open vim instances and opens one correctly if none are found (similar to the source you cite, but actually functional for CLI vim). To make it a bit easier I did write a command in my .bashrc to specifically open .tex files for use with Skim:

lvim ()
{/opt/local/bin/vim --servername VIM --remote-silent "$1"}

So now I just open the .tex file I want from the terminal with $lvim <file>.tex and open Skim either manually (open -a Skim <file>.pdf) or from vim by compiling (<leader>c) or forward searching (<leader>f) and forward and reverse search features are good to go.

I wanted to lay it all out here because I haven't yet found a (working) description for pairing standard CLI vim with Skim via synctex. However, I should note that if you are just looking for some sort of vim/synctex/Skim combo on osX and aren't married to CLI vim, it is certainly easier to use macvim. I installed macvim with macports and used Skim's macvim preset and it immediately worked, including opening a new macvim instance when a reverse search was called and the .tex file was not open.

1
  • Thank you! I will try this out when I get a chance
    – gdavtor
    May 26, 2021 at 16:57
0

As far inverse search goes, one other option is to go through tmux (here is a bare-bones beginner's guide, if you are not familiar with the program). For example, you can configure Skim like so:

  • Command: /usr/local/bin/tmux
  • Arguments: send-keys -t vim "%line"gg

This set up requires that you have already opened the source .tex file by launching vim inside tmux, followed by a simple one-off interaction with vim (e.g. move the cursor), so that the tmux target window gets renamed to vim (which is what -t vim in the arguments above refers to). You could automate all this by writing a shell script and have Skim call that instead.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .