Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Vim's autoread work?

Tags:

vim

Autoread does not reload file unless you do something like run external command (like !ls or !sh etc) vim does not do checks periodically you can reload file manually using :e

More details in this thread: Click here


I use the following snippet which triggers autoread whenever I switch buffer or when focusing vim again:

au FocusGained,BufEnter * :silent! !

Also, it makes sense to use it in combination with the following snippet so that the files are always saved when leaving a buffer or vim, avoiding conflict situations:

au FocusLost,WinLeave * :silent! w

EDIT: If you want to speed up the write by disabling any hooks that run on save (e.g. linters), you can prefix the w command with noautocmd:

au FocusLost,WinLeave * :silent! noautocmd w

As per my posting on superuser.com

Autoread just doesn't work. Use the following.

http://vim.wikia.com/wiki/Have_Vim_check_automatically_if_the_file_has_changed_externally

I got the best results by calling the setup function directly, like so.

let autoreadargs={'autoread':1} 
execute WatchForChanges("*",autoreadargs) 

The reason for this, is that I want to run a ipython/screen/vim setup.


A bit late to the party, but vim nowadays has timers, and you can do:

if ! exists("g:CheckUpdateStarted")
    let g:CheckUpdateStarted=1
    call timer_start(1,'CheckUpdate')
endif
function! CheckUpdate(timer)
    silent! checktime
    call timer_start(1000,'CheckUpdate')
endfunction

Outside of gvim, autoread doesn't work for me.

To get around this I use this rather ugly hack.

set autoread
augroup checktime
    au!
    if !has("gui_running")
        "silent! necessary otherwise throws errors when using command
        "line window.
        autocmd BufEnter        * silent! checktime
        autocmd CursorHold      * silent! checktime
        autocmd CursorHoldI     * silent! checktime
        "these two _may_ slow things down. Remove if they do.
        autocmd CursorMoved     * silent! checktime
        autocmd CursorMovedI    * silent! checktime
    endif
augroup END

This seems to be what the script irishjava linked to does, but that lets you toggle this for buffers. I just want it to work for everything.