Wikia

Vim Tips Wiki

View and diff MS Word files

Talk0
1,612pages on
this wiki
Revision as of 10:08, October 19, 2011 by JohnBeckett (Talk | contribs)

Tip 790 Printable Monobook Previous Next

created 2004 · complexity basic · version 5.7


antiword (http://www.winfield.demon.nl/) is written by Adri van Os.

Windows version can be found at http://www.informatik.uni-frankfurt.de/~markus/antiword/

Download and install it (and put it in your path).

antiword takes an MS-Word filename from the command line and outputs it in text/plain. It does not take input from stdin.

To view MS-Word documents in Vim, add lines to your vimrc similar to:

autocmd BufReadPre *.doc set ro
autocmd BufReadPre *.doc set hlsearch!
autocmd BufReadPost *.doc %!antiword "%"

Now you can do vim resume.doc to view a text version of your document (hitting Enter on .doc file in your .vimprojects list works too).

If you're on a Windows system, in Explorer, try -> selecting resume-1.1.doc and resume-1.2.doc -> right click -> "Diff with Vim".

You could probably also use this command to diff documents:

vim -d resume-1.1.doc resume-1.2.doc

Comments

By the way, you get antiword as part of www.cygwin.com. Remember you cannot actually update the Word Doc.


To get around adding antiword to your PATH and to prevent the dreaded "hit-enter" prompt, I added the following to my vimrc, I hope everything displays properly due to HTML escaping.

I keep a directory of Tools specific to Vim (ctags, cscope, grep) in a Tools directory under Vim.

" If an environment variable has not already setup Antiword
if !isdirectory(expand('$ANTIWORDHOME'))
  let $ANTIWORDHOME=expand('$VIM/Tools/antiword')
endif
" If the directory exists, create the autocommands
if isdirectory(expand('$ANTIWORDHOME'))
  autocmd BufReadPre *.doc set ro
  autocmd BufReadPre *.doc set hlsearch!
  " This prevents the hit-enter prompt and allows me to operate
  " without anitword being in the system path
  exec 'autocmd BufReadPost *.doc silent %!'.expand($ANTIWORDHOME).'\antiword "%"'
endif

" Works like a charm
gvim testdoc.doc

catdoc does the same thing.


I do not think including

set hlsearch!

in the autocommand is a good idea. The end result is that the value of 'hlsearch' will depend on what it was when the autocommand was triggered, and I think you will get different values if you open one or two .doc files with vim. If you want to set this option to a particular value when opening .doc files, usae either

set hlsearch

or

set nohlsearch

See :help :set and :help 'hlsearch'.


See VimTip1356 for the same thing for PDF files.


On Windows XP, I had to use single quotes around the '%' argument instead of double quotes. I was getting errors about opening a Windows tmp file (maybe related to the document's Word template). I also add the "silent" keyword so I don't have to hit return. I didn't use the function posted above. So my vimrc entry looks like:

" View word documents in Vim (good for diff'ing).
autocmd BufReadPre *.doc set ro
autocmd BufReadPre *.doc set hlsearch
autocmd BufReadPost *.doc silent %!/usr/bin/antiword '%'

Around Wikia's network

Random Wiki