I love the marked and Mou editor which have the great capability to preview the rendering results on the fly. So I'm wondering, is there a way to do the same thing in Vim?
问题:
回答1:
I recently found a Chrome extension that makes Chrome able to properly open and display markdown files: Markdown preview.
Then it was just a matter of mapping a key in Vim to open the file with Chrome. Mine looks like this:
" Open markdown files with Chrome.
autocmd BufEnter *.md exe 'noremap <F5> :!start C:\Users\tomas\AppData\Local\Google\Chrome\Application\chrome.exe %:p<CR>'
This command would have to be edited, of course, if your files don't have the ".md" extension, you want your mapping on a different key or if Chrome is located at a different location.
Now whenever I'm editing a ".md" file I can hit <F5>
to open the file in Chrome. A perfect solution would be to get Chrome to auto reload every few seconds, but I can't seem to find an such an extension that works for local files.
Pros:
- The ability to hit a button to preview your markdown file, without the need for any running servers or special code.
- Works on all platforms that supports Vim and Chrome - which pretty much covers all platforms.
Cons:
- No auto-refresh, you have to hit
<F5>
every time you want to preview the file. - No Github-flavoured markdown.
回答2:
You're in luck - I've just written a vim plugin with real-time Markdown previewing. It uses github Markdown and styles too: https://github.com/suan/vim-instant-markdown
回答3:
I use Marked.app to preview Markdown files. And have mapped <leader>p
to do a preview:
function! s:setupMarkup()
nnoremap <leader>p :silent !open -a Marked.app '%:p'<cr>
endfunction
au BufRead,BufNewFile *.{md,markdown,mdown,mkd,mkdn} call s:setupMarkup()
If you don't want to drop $4 on Marked.app then you can try Hammer.vim. Change the line to:
map <buffer> <leader>p :Hammer<CR>
回答4:
You can actually use pandoc to compile to document format of your choice and view it using external default command. For example i like to preview it as pdf and i use following setup on Ubuntu.
" pandoc , markdown
command! -nargs=* RunSilent
\ | execute ':silent !'.'<args>'
\ | execute ':redraw!'
nmap <Leader>pc :RunSilent pandoc -o /tmp/vim-pandoc-out.pdf %<CR>
nmap <Leader>pp :RunSilent gnome-open /tmp/vim-pandoc-out.pdf<CR>
I compile a document using ,pc
and preview using ,pp
. Since in ubuntu evince
is default viewer and automatically loads changed files. You end up doing only ,pc
for your changes to reflect.
PS: I have Leader mapped to ,
回答5:
No, there are several Vim plugins for syntax highlighting, snippets and so on, but Vim displays text and text only, therefore you cannot have Markdown (or any other kind of) preview in Vim.
If you're interested in real-time preview, maybe MarkdownPad would suit your needs better.
回答6:
Adding to @Codemonkey 's answer, if you are using OSX, you can use the following in your .vimrc file.
" Open markdown files with Chrome.
autocmd BufEnter *.md exe 'noremap <F5> :!open -a "Google Chrome.app" %:p<CR>'
回答7:
Here's a simple vim plugin to view Markdown in Chrome - it doesn't require ruby or anything fancy and will refresh the preview when you write to the vim buffer (it can also be activated with a hotkey)
Also, the previewing takes place in the background so you never have to leave vim.
https://github.com/JamshedVesuna/vim-markdown-preview
回答8:
I have a function in my .vim dir:
function! markdown#preview()
silent update
let output_name = tempname() . '.html'
let file_header = ['<html>', '<head>',
\ '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">',
\ '<title>'.expand('%:p').'</title>',
\ '<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.3.0/build/cssreset/reset-min.css">',
\ '<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.3.0/build/cssbase/base-min.css">',
\ '<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.3.0/build/cssfonts/fonts-min.css">',
\ '<style>body{padding:20px;}div#container{background-color:#F2F2F2;padding:0 20px;margin:0px;border:solid #D0D0D0 1px;}</style>',
\ '</head>', '<body>', '<div id="container">']
call writefile(file_header, output_name)
silent exec '!markdown "' . expand('%:p') . '" >> "' . output_name . '"'
silent exec '!echo "</div></body></html>" >> "' . output_name . '"'
silent exec '!sensible-browser "' . output_name . '" &'
endfunction
With that I have the following mapping too:
inoremap <buffer> <F7> <ESC>:call markdown#preview()<CR>
nmap <buffer> <F7> :call markdown#preview()<CR>
It's not perfect and it's not realtime but it's good enough for my limited needs.
回答9:
I came across a nice plugin which worked great out of the box https://github.com/waylan/vim-markdown-extra-preview
回答10:
I don't think preview markdown in vim is a good idea.
But you can convert your markdown in HTML just by typing %!markdown
, presumably you have markdown installed. Then save that file and open it in a browser.