24

I'm editing HTML files. I use this keymap to launch firefox to preview the page:

map <F6> :update<CR>:silent !xdg-open %:p<CR>

But after this command, the Vim window becomes completely black. I need to scroll every line to make it appear again.

So I want to write a function to fresh the buffer. Here's what i did:

function! Refresh()                                                                                                                                               
    " code
    set noconfirm
    bufdo e!
    set confirm
endfunction

nmap <F5> :call Refresh()<CR>

I got the idea from this post. But it doesn't work.

Community
  • 1
  • 1
McBear Holden
  • 5,741
  • 7
  • 33
  • 55

3 Answers3

45

Use CtrlL to redraw the screen. You can also use :redraw.

Check the help on these.

sidyll
  • 57,726
  • 14
  • 108
  • 151
8

How about adding a "redraw" do your function (which does the same as ctrl-L)

:redr[aw][!]    Redraw the screen right now.  When ! is included it is
                cleared first.
                Useful to update the screen halfway executing a script
                or function.  Also when halfway a mapping and
                'lazyredraw' is set.
Benj
  • 31,668
  • 17
  • 78
  • 127
2

Here is my bindings for previewing HTML documents with resolved empty screen issue:

autocmd BufRead *.html map <F5> :exe ':silent !open -a /Applications/Firefox.app %'<CR>:redr!<CR>

I prefer use autocmd for bindings, because I can map same key for various things depending what type of file I am working in. For example with autocmd I can map F5 for previewing document in Firefox on html-files and map same key F5 on js-files for executing node.

Alexander Myshov
  • 2,881
  • 2
  • 20
  • 31