15

Occasionally someone sends me a .odt, .doc, and .docx file; opening them in OpenOffice is always a pain because, well, OpenOffice.

Right now I use a variety of commandline tools (such as odt2txt) to convert these files to plain text, save the plain text to a temporary file, and then view (and maybe edit) it in Vim.

Can this be made easier? Perhaps something with autogroups? Or maybe there are better ways? I would like to just do:

$ vim file.odt

And have Vim take care of the converting for me?

Even better (bonus points) would be if I was also able to save the file (to file.txt), but that this would not be created until I do a write...

1 Answer 1

15

You should be able to add something like this to your vimrc:

autocmd BufReadPost *.odt :%!odt2txt %

That will send the entire buffer through the odt2txt program after it's read in by vim, but only if the file name ends with .odt.

There's also the textutil.vim plugin that says it can do what you're talking about for a few of those file types (but I haven't personally used it).

If you're willing to edit in something slightly more structured than plain text, like markdown, you could also use pandoc to do the conversion:

autocmd BufReadPost *.docx :%!pandoc -f docx -t markdown
autocmd BufWritePost *.docx :!pandoc -f markdown -t docx % > tmp.docx

I haven't actually tested these conversions, but they should work. You may need to use different tools depending on the file format.

2
  • does autocmd BufReadPost *.docx :%!pandoc -f docx -t markdown basically mean, that if I open a .docx file in vim, it will instead pass that file through :%!pandoc -f docx -t markdown, before displaying it on the VIM window?
    – alpha_989
    Dec 19, 2017 at 16:50
  • 1
    @alpha_989 yes.
    – xthrd
    Dec 20, 2017 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.