Vim Tips Wiki
(Adjust previous/next navigation)
Tag: Visual edit
 
(8 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=1356
 
|id=1356
 
|previous=1354
 
|previous=1354
 
|next=1359
 
|next=1359
|created=October 8, 2006
+
|created=2006
 
|complexity=basic
 
|complexity=basic
|author=Erez Volk+Sridharv
+
|author=
 
|version=7.0
 
|version=7.0
 
|rating=41/26
 
|rating=41/26
Line 12: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
  +
This tip shows how to use Vim to view the text in a PDF document. That can be useful to use Vim to see the differences between the text in two versions of a pdf. You need to install [http://www.foolabs.com/xpdf/ xpdf] (available on all major platforms) as it provides the <code>pdftotext</code> utility to read the text from a pdf file.
1. Get [http://www.foolabs.com/xpdf/download.html xpdf] (for pdftotext) and [http://www.physik.uni-wuerzburg.de/~vrbehr/cups-pdf/download.shtml cups-pdf]. Xpdf package is available on all the major platforms. However cups-pdf is not available on Windows.
 
   
  +
==Procedure==
2. cups-pdf prints the pdf files and saves them in ~/PDF/ by default. It is assumed that these settings won't be changed.
 
 
Put one of the following mappings in your [[vimrc]]:
 
3. Put the following commands in your [[vimrc]]:
 
 
<pre>
 
<pre>
  +
:command! -complete=file -nargs=1 Rpdf :r !pdftotext -nopgbrk <q-args> -
autocmd BufReadPre *.pdf set ro nowrap
 
autocmd BufReadPost *.pdf silent %!pdftotext "%" -nopgbrk -layout -q -eol unix -
+
:command! -complete=file -nargs=1 Rpdf :r !pdftotext -nopgbrk <q-args> - |fmt -csw78
autocmd BufWritePost *.pdf silent !rm -rf ~/PDF/%
 
autocmd BufWritePost *.pdf silent !lp -s -d pdffg "%"
 
autocmd BufWritePost *.pdf silent !until [ -e ~/PDF/% ]; do sleep 1; done
 
autocmd BufWritePost *.pdf silent !mv ~/PDF/% %:p:h
 
 
</pre>
 
</pre>
   
  +
These define the <code>:Rpdf</code> command to read the result of converting a pdf document to text. The text is read into the current buffer after the current line. The first reads the pdf with each paragraph as a long line, while the second wraps long lines (if the <code>fmt</code> utility is available).
In a nutshell, the BufWritePost commands remove any file of similar name from ~/PDF, print the pdf to the directory, wait until the file is printed, then move the file to the current location. (Note: pdffg is the name of the pdf printer &ndash; check the name of the printer on your machine.)
 
   
  +
For example, the following commands open a new tab page and read the text from a pdf document into the buffer.
Note that if you open an existing pdf, it will be opened in readonly mode. You can write to it using <tt>:w!</tt> but that will remove existing formatting.
 
 
<pre>
  +
:tabnew
  +
:Rpdf example.pdf
 
</pre>
   
 
==See also==
 
==See also==
 
*[[VimTip790|View and diff MS Word files]] Uses a similar concept to open Word documents.
 
*[[VimTip790|View and diff MS Word files]] Uses a similar concept to open Word documents.
  +
*[[VimTip667|Working with CSV files]] Uses a similar concept to open Excel spreadsheets.
   
 
==Comments==
 
==Comments==
  +
You could create a ftdetect plugin and put instruction in it so you can just :edit filename.pdf and it'll set options for it. {{help|new-filetype}}
Just like for MS-Word files, this allows you to use Vim to diff two pdf files very nicely.
 
----
 
 
If you happen to be on Unix or have the fmt utility, a slight improvement is:
 
<pre>
 
autocmd BufReadPost *.pdf %!pdftotext -nopgbrk "%" - |fmt -cw78
 
</pre>
 
 
----
 
Or, better still:
 
<pre>
 
autocmd BufReadPost *.pdf %!pdftotext -nopgbrk "%" - |fmt -csw78
 
</pre>
 
 
----
 
Add silent and you don't need to press enter:
 
<pre>
 
autocmd BufReadPost *.pdf silent %!pdftotext -nopgbrk "%" - |fmt -csw78
 
</pre>
 
 
----
 
You may want to call pdftotext with parameter <tt>-layout</tt> -- tries to maintain the original layout of pages. This doesn't compress all the page into one chunk of text.
 
 
----
 
Win32 users: pdftotext comes as part of the CygWin Suite http://www.cygwin.com/
 
 
Please don't install cygwin just for pdftotext. Use the link to the xpdf-files, as the author suggests. There is a pdftotext for Win32 as well.
 
 
----
 

Latest revision as of 18:59, 19 June 2014

Tip 1356 Printable Monobook Previous Next

created 2006 · complexity basic · version 7.0


This tip shows how to use Vim to view the text in a PDF document. That can be useful to use Vim to see the differences between the text in two versions of a pdf. You need to install xpdf (available on all major platforms) as it provides the pdftotext utility to read the text from a pdf file.

Procedure[]

Put one of the following mappings in your vimrc:

:command! -complete=file -nargs=1 Rpdf :r !pdftotext -nopgbrk <q-args> -
:command! -complete=file -nargs=1 Rpdf :r !pdftotext -nopgbrk <q-args> - |fmt -csw78

These define the :Rpdf command to read the result of converting a pdf document to text. The text is read into the current buffer after the current line. The first reads the pdf with each paragraph as a long line, while the second wraps long lines (if the fmt utility is available).

For example, the following commands open a new tab page and read the text from a pdf document into the buffer.

:tabnew
:Rpdf example.pdf

See also[]

Comments[]

You could create a ftdetect plugin and put instruction in it so you can just :edit filename.pdf and it'll set options for it. :help new-filetype