ferdinandyb / bibtexcite.vim

Bib(la)tex citations for vim
MIT License
13 stars 1 forks source link

BIBTEXCITE.VIM

A simple vim integration for fzf-bibtex and bibtool for easy handling of bib(la)tex citations.

Features

The plugin is being developed to work well with Zotero as the citation manager, using the Better Biblatex plugin to synchronize a monolithic .bib file, but it should work with other workflows as well (also see tips below).

Fetch info fetch

Insert citation insert

INSTALLATION

For fetching citation info bibtool is enough, otherwise you need fzf-bibtex. Make sure all the binaries (bibtool, bibtex-ls, bibtex-cite, bibtex-markdown and fzf) are on your path.

Use your favorite plugin manager.

Requires at least vim 8.2.0 (I don't know the minimal nvim version, and you'll probably have a better experience on vim 9).

USAGE

Configuration

TIPS

Using one monolithical .bibfile managed by Zotero or Mendeley is the fastest way to work, but you might need to include a .bib file for sharing later. In that case, if you are working with latex the following will extract the entries from the .aux file and place the in new.bib.

bibtool -x main.aux -o new.bib

If you are working with pandoc markdown the following will do the same:

grep -rPo "@\K[a-zA-Z0-9\-&_]+" *.md | xargs \
    -I{} bibtool -r biblatex -X {} monolithical.bib > new.bib

Using fzf to export some records from the bibfile to a new one:

 bibtex-ls ~/org/zotero.bib | fzf --multi | sed -nr 's/.+\b@([a-zA-Z0-9\-\&_])/\1/p' | ansi2txt | xargs  -I{} bibtool -r biblatex -X {} ~/org/zotero.bib

You can bind vim's default help key (K) to get the help if it exists, otherwise show the citation info by putting this in your vimrc:

" Use K to show documentation in preview window.
nnoremap <silent> K :call <SID>show_documentation()<CR>

function! s:show_documentation()
  if (index(['vim','help'], &filetype) >= 0)
    execute 'h '.expand('<cword>')
  elseif (len(bibtexcite#getcitekey("pandoc")) > 1)
    call bibtexcite#showcite("pandoc")
  elseif (len(bibtexcite#getcitekey("latex")) > 1)
    call bibtexcite#showcite("latex")
  elseif (coc#rpc#ready())
    call CocActionAsync('doHover')
  else
    execute '!' . &keywordprg . " " . expand('<cword>')
  endif
endfunction

This also falls back to Coc.nvim-s show documentation, so if you are not using it remove the correspoding elseif.

This version of the function will first try to open the corresponding pdf and if there is none, then show the pop-up with the citation info.

function! myfunctions#show_documentation()
  if (index(['vim','help'], &filetype) >= 0)
    execute 'h '.expand('<cword>')
  elseif (len(bibtexcite#getfilepath("pandoc")) > 1)
    call bibtexcite#openfile("pandoc")
  elseif (len(bibtexcite#getfilepath("latex")) > 1)
    call bibtexcite#openfile("latex")
  elseif (len(bibtexcite#getcitekey("pandoc")) > 1)
    call bibtexcite#showcite("pandoc")
  elseif (len(bibtexcite#getcitekey("latex")) > 1)
    call bibtexcite#showcite("latex")
  elseif (coc#rpc#ready())
    call CocActionAsync('doHover')
  else
    execute '!' . &keywordprg . " " . expand('<cword>')
  endif
endfunction

If you want to use the abstract in a citation entry, you can either do something like this over a citekey:

:put =bibtexcite#getcite('pandoc')

or using vim-backscratch :Scratch BibtexciteEchocite to put it on a scratch buffer.

Placing it in one of the registers (e.g. + for system clipboard) can done with

:let @+ = bibtexcite#getcite('pandoc')

Possible mappings:

autocmd FileType markdown  nnoremap <buffer> <silent> <leader>nc :BibtexciteInsert<CR>
autocmd FileType markdown  inoremap <buffer> <silent> @@ <Esc>:BibtexciteInsert<CR>

Acknowledgments

The code for the popups was sourced from ALE, the code for the fzf chooser was pretty much taken from the fzf-bibtex README.

LICENSE

MIT

autoload/bibtexcite/floating_preview.vim is licensed separately in the file.