Skip to content

Instantly share code, notes, and snippets.

@vext01 vext01/vimura.md
Last active Nov 13, 2019

Embed
What would you like to do?
Vim+Zathura+Synctex

Vim+Zathura+Synctex

  • In a script called 'vimura':
#!/bin/sh
echo $1
zathura -s -x "gvim --servername $1 -c \"let g:syncpdf='$1'\" --remote +%{line} %{input}" $*

For zathura >= 0.3.2 remove -s.

  • In your vimrc:
function! Synctex()
        " remove 'silent' for debugging
        execute "silent !zathura --synctex-forward " . line('.') . ":" . col('.') . ":" . bufname('%') . " " . g:syncpdf
endfunction
map <C-enter> :call Synctex()<cr>
  • Build your latex doc with synctex enabled
pdflatex -synctex=1 ...
  • Open PDF with vimura script.

  • CTRL+Click in Zathura jumps to source in vim.

  • CTRL+Enter in vim jumps to corresponding position in PDF.

@natema

This comment has been minimized.

Copy link

natema commented Sep 17, 2016

Thank you so much, I tried to make synctex work with evince but eventually gave up. Now I can finally fully enjoy the power of vim without any regret. Only one question: is there any way to make this work by starting gvim first and then opening zathura with \ll?

@scemama

This comment has been minimized.

Copy link

scemama commented Feb 10, 2017

Thanks! Very useful!

@fmeynadier

This comment has been minimized.

Copy link

fmeynadier commented Sep 5, 2017

To make it work without starting a new vim instance, allowing to link to an already open file, I think you just have to use a fixed servername, i.e.

vimura:

#!/bin/sh
echo $1
zathura -s -x "vim --servername vim -c \"let g:syncpdf='$1'\" --remote +%{line} %{input}" $*

and put this alias in .bashrc (or equivalent)
alias vim='/usr/bin/vim --servername vim'
At least it works for my basic usage of vim, I don’t know if there are side effects with gvim and/or more advanced usage of vim.

You could also keep the "vimura" script as it is and launch vim with the corresponding servername (i.e. the .tex filename).

@fmeynadier

This comment has been minimized.

Copy link

fmeynadier commented Sep 6, 2017

Also, if you prefer not to use the g:syncpdf variable defined in the vimura script, leaving you free to display it independently and still get forward search :

function! Synctex()
  " remove 'silent' for debugging
  execute "silent !zathura --synctex-forward " . line('.') . ":" . col('.') . ":" . bufname('%') . " " . bufname('%')[:-5]. ".pdf"
  redraw!
endfunction
map <C-enter> :call Synctex()<cr>

This assumes of course corresponding root names between the tex file and the pdf file.
Also, with vim I have to redraw terminal after that, hence the "redraw!" that might not be needed with gvim.

I realize this may be going away from the initial intent, and may be less robust, but I thought it could be useful to others...

@hringriin

This comment has been minimized.

Copy link

hringriin commented Dec 7, 2018

With gVim it seems to work fine, but I'd really like to use normal vim, but when I execute the function Synctex() from within normal vim, I get the terminal messed up. Is there any way to debug that in order to use normal vim?

screenshot_2018-12-07_23-53-51

Regards
hringriin

@perelo

This comment has been minimized.

Copy link

perelo commented Jul 8, 2019

An update for this great gist in order to use it with NeoVim

NeoVim don't have --servername syntax natively, but you can use neovim-remote :

pip3 install neovim-remote
alias nvim="NVIM_LISTEN_ADDRESS=/tmp/nvimsocket nvim"

then the vimura script :

#!/bin/bash
if [ $1 == "neovim" ]; then
    synctex_command="nvr --remote +%{line} %{input}"
else
    synctex_command="vim --servername vimd --remote +%{line} %{input}" 
fi
zathura -x "$synctex_command" ${@:2}

Also, in order to execute background jobs from neovim, we must use jobstart() instead of terminating the command by &. Thus, in .vimrc:

function! Synctex()
    let vimura_param = " --synctex-forward " . line('.') . ":" . col('.') . ":" . expand('%:p') . " " . substitute(expand('%:p'),"tex$","pdf", "")
    if has('nvim')
        call jobstart("vimura neovim" . vimura_param)
    else
        exe "silent !vimura vim" . vimura_param . "&"
    endif
    redraw!
endfunction

Note that I added redraw! at the end, and I use full path with %:p so I can deal with multiple pdf in multiple directories.

@bruno-caldas

This comment has been minimized.

Copy link

bruno-caldas commented Oct 10, 2019

In order to work fine with normal vim, you can reload the file, i.e add at the end of the line of your .vimrc which you call Synctex() with the
:e <CR>

With gVim it seems to work fine, but I'd really like to use normal vim, but when I execute the function Synctex() from within normal vim, I get the terminal messed up. Is there any way to debug that in order to use normal vim?

screenshot_2018-12-07_23-53-51

Regards
hringriin

@vext01

This comment has been minimized.

Copy link
Owner Author

vext01 commented Oct 11, 2019

Hi,

I wonder if we should make a git repo for this. Perhaps if I import what I started with, people can raise PRs to have stuff merged in?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.