I use vim to edit latex, but latex is bit too heavy for taking notes casually on daily basis, so I decided to use markdown instead.

So, I want to configure vim so that I could view markdown formatted file using \lv command

I am using Mac OS X 10.11. How should I modify .vimrc file?

  • 3
    Have you searched for some plugins like this one? – statox Jul 1 '16 at 12:01
  • 3
    You don't need to preview markdown. – romainl Jul 1 '16 at 12:01
  • 1
    @romainl It would be nice if I can. – Tom Jul 1 '16 at 12:37
  • 2
    @romainl Yeah and I don't need syntax highlighting. Doesn't discount the fact that it's useful. – Tommy A Jul 1 '16 at 12:49
  • 2
    @romainl I was drawing a parallel of "need vs. want". – Tommy A Jul 1 '16 at 13:10

While I mostly agree with @romainl comment (markdown was made to be explicit enough not to need a preview) you can do this in different ways:

[OSX / Unix] The instant-markdown plugin is a solution. You need to have node.js installed and to use the following command:

[sudo] npm -g install instant-markdown-d

You also need to have the packages xdg-utils, curl and nodejs-legacy installed.

With that when you open a markdown buffer, an html page will be opened in your web browser and you'll see the changes in real time.

[Unix] This answer suggests to use the daringfireball markdown program.

Once you have downloaded it you can use the following command to see the file in lynx:

./Markdown.pl ~/testfile.markdown | lynx -stdin

You could use it in a Vim command with something like that:

:!./Markdown.pl % | lynx -stdin

[Unix] Another plugin which could be useful is vim-preview it seems to have some dependencies but should work on markdown, rdoc, textile, ronn and rts files.

[Windows / OSX] Finally the github user natesilva made a very good gist which only requires the markdown command in your path. Once you sourced the script ,p should open your markdown in the default browser.

" While editing a Markdown document in Vim, preview it in the
" default browser.
" Author: Nate Silva
" To install: Place markdown.vim in ~/.vim/ftplugin or
" %USERPROFILE%\vimfiles\ftplugin.
" To use: While editing a Markdown file, press ',p' (comma p)
" Tested on Windows and Mac OS X. Should work on Linux if you set
" Requires the `markdown` command to be on the system path. If you
" do not have the `markdown` command, install one of the following:
" http://www.pell.portland.or.us/~orc/Code/discount/
" http://www.freewisdom.org/projects/python-markdown/
    " **************************************************************
    " Configurable settings

    let MARKDOWN_COMMAND = 'markdown'

    if has('win32')
        " note important extra pair of double-quotes
        let BROWSER_COMMAND = 'cmd.exe /c start ""'
        let BROWSER_COMMAND = 'open'

    " End of configurable settings
    " **************************************************************

    silent update
    let output_name = tempname() . '.html'

    " Some Markdown implementations, especially the Python one,
    " work best with UTF-8. If our buffer is not in UTF-8, convert
    " it before running Markdown, then convert it back.
    let original_encoding = &fileencoding
    let original_bomb = &bomb
    if original_encoding != 'utf-8' || original_bomb == 1
        set nobomb
        set fileencoding=utf-8
        silent update

    " Write the HTML header. Do a CSS reset, followed by setting up
    " some basic styles from YUI, so the output looks nice.
    let file_header = ['<html>', '<head>',
        \ '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">',
        \ '<title>Markdown Preview</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)

    let md_command = '!' . MARKDOWN_COMMAND . ' "' . expand('%:p') . '" >> "' .
        \ output_name . '"'
    silent exec md_command

    if has('win32')
        let footer_name = tempname()
        call writefile(['</div></body></html>'], footer_name)
        silent exec '!type "' . footer_name . '" >> "' . output_name . '"'
        exec delete(footer_name)
        silent exec '!echo "</div></body></html>" >> "' .
            \ output_name . '"'

    " If we changed the encoding, change it back.
    if original_encoding != 'utf-8' || original_bomb == 1
        if original_bomb == 1
            set bomb
        silent exec 'set fileencoding=' . original_encoding
        silent update

    silent exec '!' . BROWSER_COMMAND . ' "' . output_name . '"'

    exec input('Press ENTER to continue...')
    exec delete(output_name)

" Map this feature to the key sequence ',p' (comma lowercase-p)
map ,p :call PreviewMarkdown()<CR>

Edit @TommyA also suggested grip this is not a vim plugin but it can be installed with pip install grip. The main advantage of this program is that it uses GitHub's markdown API so the render should be pretty close to what your markdown will look like on Github pages.

This program will serve your markdown files on localhost:6419 by default. You should be able to invoke it from Vim with :!grip

  • There is also grip which renders GitHub flavored markdown. – Tommy A Jul 1 '16 at 12:54
  • @TommyA instant-markdown also renders GitHub flavored markdown but another alternative is good too. Grip seems nice because it doesn't have to many dependencies (once you've used pip it seems like you're good to go). Thanks for your comment! – statox Jul 1 '16 at 12:58
  • Ah missed that. However, grip uses GitHub's markdown API. So, it should always be rendered almost exactly how it'll appear on GitHub, including language highlighting and emoji. There's also a pull request to get it to render WikiLinks. – Tommy A Jul 1 '16 at 13:08
  • @statox Is there a way to set vim such that instant-markdown plugin gets triggered for .md extensions? – Tom Jul 1 '16 at 13:09
  • @TommyA: Oh that's good to know. Do you want me to add it to my answer or do you want to make your own answer – statox Jul 1 '16 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.