Skip to content

Instantly share code, notes, and snippets.

@jashmenn
Created May 8, 2009 19:00
Show Gist options
  • Save jashmenn/108933 to your computer and use it in GitHub Desktop.
Save jashmenn/108933 to your computer and use it in GitHub Desktop.
" Vim Markdown preview
"
" * compile vim with ruby support
" * sudo gem install rdiscount (update load path to absolute path below)
" * add this file to
" ~/.vim/ftplugin/mkd.vim
function! PreviewMKD()
ruby << EOF
require 'rubygems'
$:.unshift("/usr/local/lib/ruby/gems/1.8/gems/rdiscount-1.3.4/lib")
require 'rdiscount'
t = ""
VIM::Buffer.current.count.times {|i| t += "#{VIM::Buffer.current[i + 1]}\n"}
html_file = VIM::Buffer.current.name.gsub(/.(md|mkd|markdown)$/, '.html')
if html_file == VIM::Buffer.current.name
print "Error! - This file extension is not supported for Markdown previews"
end
File.open(html_file, 'w') do |f|
f.write(RDiscount.new(t).to_html)
end
system("open #{html_file}")
EOF
endfunction
map <Leader>p :call PreviewMKD()<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment