Skip to content
Enno edited this page Sep 17, 2024 · 7 revisions

If you have lesspipe (or run-mailcap with a suitable ~/.mailcap file) installed, as by default on Debian (and derivatives such as Ubuntu), adding these lines

if exists('g:did_load_filetypes')
  if executable('run-mailcap')
    let s:lesspipe_cmd = 'run-mailcap --action=cat'
  elseif executable('lesspipe.sh')
    let s:lesspipe_cmd = 'LESSQUIET=1 lesspipe.sh'
  elseif executable('lesspipe')
    let s:lesspipe_cmd = 'lesspipe'
  endif
  if exists('s:lesspipe_cmd') && executable('file')
    augroup lesspipe
      autocmd!
      autocmd BufReadPost *
            \ if  empty(&l:buftype) && !did_filetype() && !&l:binary && filereadable(bufname('%')) &&
            \     system('file --mime --brief ' . fnamemodify(resolve(expand('%')), ':p:S')) !~# '^text/' |
            \   silent exe '%!' . s:lesspipe_cmd . ' ' . expand('%:S') |
            \   setlocal filetype=text nomodifiable readonly |
            \ endif
	  augroup END
  endif
endif

to your vimrc lets you view all unrecognized files in Vim through lesspipe, if not explicitely intended otherwise, for example, by passing the parameter -b (for binary) on the command line.

Since Vim sets a filetype for pdf files, to read their text by lesspipe, too, add

    autocmd Filetype pdf
          \ exe '%!' . s:lesspipe_cmd . ' ' . expand('%:S') |
          \ setlocal filetype=text nomodifiable readonly |

before augroup END above.

To ensure that Vim displays the text contained in a Microsoft Office or EPUB document instead of listing the files it archives (by the zipPlugin, see :help zip), edit your .vimrc choosing one of these 2 options:

  • either exclude these file types from those handled by zipPlugin
  • or disable the 'zipPlugin' altogether

Example of how to exclude some file types from those handled by zipPlugin

Check what is currently handled by the zipPlugin:

echo g:zipPlugin_ext
*.aar,*.apk,*.celzip,*.crtx,*.docm,*.docx,*.dotm,*.dotx,*.ear,*.epub,*.gcsx,*.glox,*.gqsx,*.ja,*.jar,*.kmz,*.odb,*.odc,*.od
f,*.odg,*.odi,*.odm,*.odp,*.ods,*.odt,*.otc,*.otf,*.otg,*.oth,*.oti,*.otp,*.ots,*.ott,*.oxt,*.potm,*.potx,*.ppam,*.ppsm,*.p
psx,*.pptm,*.pptx,*.sldx,*.thmx,*.vdw,*.war,*.wsz,*.xap,*.xlam,*.xlam,*.xlsb,*.xlsm,*.xlsx,*.xltm,*.xltx,*.xpi,*.zip

Remove the desired extensions, for example, jar?|epub|doc[xm]|xls[xmb]|pp[st][xm], so that they can be handled by lesspipe inside vim

" Remove extensions jar?|epub|doc[xm]|xls[xmb]|pp[st][xm] from g:zipPlugin_ext
" from Sep 13, 2016 and afterwards add back whenever converter unavailable
let g:zipPlugin_ext='*.apk,*.celzip,*.crtx,*.ear,*.gcsx,*.glox,*.gqsx,*.kmz,*.oxt,*.potm,*.potx,*.ppam,*.sldx,*.thmx,*.vdw,*.war,*.wsz,*.xap,*.xlam,*.xlam,*.xltm,*.xltx,*.xpi,*.zip'

Example of disabling it altogether

  let g:loaded_gzip = v:true

See also the vim-office plug-in to display meaningful text of a binary files read by Vim but without lesspipe.

Clone this wiki locally