msprev / fzf-bibtex

a BibTeX source for fzf
BSD 3-Clause "New" or "Revised" License
129 stars 15 forks source link

feature: export bibtex #10

Closed fredcallaway closed 5 years ago

fredcallaway commented 5 years ago

I have a master .bib file for my own usage, but use separate .bib files for each paper (makes collaboration easier). Thus, I frequently need to grab a full bibtex entry from my main bib to put into the individual paper bibs. This should be pretty easy, just a simplified version of the bibtex-markdown command. Any chance this could happen?

(I'd do a pull request, but I have no experience with go so it would be a time sink for me.)

msprev commented 5 years ago

If I understand correctly, you want a new command that takes a selection of items from the fzf listing of entries as input and yields raw BibTeX corresponding to each entry as output? Is that right?

fredcallaway commented 5 years ago

Yes, that's right!

msprev commented 5 years ago

It would be fairly straightforward to write. It would not make sense to bother to cache the results. The command would take a list of BibTex keys as input over stdin, a list of BibTex source files as options on the command line, and yield the raw text entries from those files as output to stdout.

You can write this command in any language you like, eg Python. I only use Go because it is so fast.

I’m happy to consider PRs for a Go command to do this to be included in this repo. But if it’s for you own use, I suggest you just write the command yourself in whatever language you feel most comfortable.

ferdinandyb commented 2 years ago

I came up with this commandline monstrosity to do this, if anybody else stumbles here with the same question:

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

The ansi2txt seemed a clearer solution than the various sed commands floating around for clearing color codes.

But in general I agree with @fredcallaway that a bibtex-bib sink for fzf would feel right at home in this repo.

tobiasrenkin commented 1 year ago

Just want to +1 this request - it would be extremely useful to have a general purpose command that just returns selected bibtex entries. A lot of other functionality (including some previous feature requests such as opening pdfs or different formatting) could be easily implemented from that output with a couple of lines of bash.

msprev commented 1 year ago

bibtool is your friend here (as exploited by the script above). It can do this kind of extraction based on key (or virtually anything else) fairly easily. To extract the bibtex entries with keys 'key1' and 'key2' from refs.bib:

bibtool -X "key1\|key2" refs.bib

Look at the bibtool manual on selecting and extracting entries sections 1.2.5 and A.9 for more info.

It is trivial to hook this up to fzf-bibtex. All you need to do is write a wrapper script that takes the output from bibtex-ls | fzf and outputs a command like the one above to bibtool with the appropriate keys inserted. (NB. to make it easy to parse, the key is always the last word on each line output by bibtex-ls and it is always prefixed by a '@'.)

This wrapper could be done in a couple of lines of Python/Perl/bash/whatever. Post any solutions here!

msprev commented 1 year ago

Couldn't resist it... It is very easy. Below is what you need using Python. You would just have to then write an instruction to call bibtool using bibtool_searchterm.

incoming below is a string variable that contains all the newline separated text output to stdout by bibtex-ls | fzf. It's easy to read this in Python with sys.stdin.read().

bibtool_searchterm = "\|".join([(l.split()[-1])[1:] for l in incoming.splitlines()]))