Search code examples
shellvimneovim

Launching Zathura from Vim


I have the following little shell script, called md:

#!/bin/sh
if [ -z $1 ]; then
    echo "Error: No argument supplied"
else
    (pandoc -t pdf $1 | zathura - &)
fi

It works fine in my terminal, allowing me to run, say, md foo.md and launch Zathura with the generated pdf. However, I've tried adding a custom Vim command to do the same, and it doesn't work! Here's the command from my init.vim:

command Md ! md %:p

When I run :Md in Vim in a Markdown file I get the following output from Vim:

:! md /absolute/path/to/file.md

and Zathura launches, but with an "empty" file, i.e. it says "[No name]" down at the bottom. If I copy-paste the command that Vim says it ran, and run it in my terminal, Zathura launches just the same, but with the file displayed.

Why isn't it working?


Solution

  • Finally got it working. The problem wasn't with the difference between reading from a file or from a buffer, (although I did implement @amphetamachine's answer because reading from buffer is more nice), but instead it was the interaction between pandoc and zathura when running as background processes.

    I think the fault here lies with Zathura, which when spawned as a background process doesn't properly receive pipe input. You're supposed to use --fork instead.

    The final version looks like this:

    init.vim

    command! -buffer -range=% Md w !md
    map <F5> :silent Md<CR>
    

    The md script

    #!/bin/sh
    if [ -z $1 ]; then                                                                                            
        pandoc -t pdf | zathura --fork -
     else
        pandoc -t pdf $1 | zathura --fork -
    fi
    

    This properly gives command back to Vim and works both there, and in my terminal.