开发者

How does Vim's autoread work?

:h autoread says:

When a fil开发者_如何学Goe has been detected to have been changed outside of Vim and it has not been changed inside of Vim, automatically read it again.

After putting set autoread in my vimrc, I open a file with Vim, switch to another editor, change the file, and wait to see the changes in Vim as well. Nothing happens. I have to use :e to reload the file with the new content.

What did I miss?                                                                I'm using Vim 7.2 on Mac 10.5.8


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.


Trigger when cursor stops moving

Add to your vimrc:

au CursorHold,CursorHoldI * checktime

By default, CursorHold is triggered after the cursor remains still for 4 seconds, and is configurable via updatetime.

Buffer change trigger

To have autoread trigger when changing buffers, add to your vimrc:

au FocusGained,BufEnter * checktime

Terminal window focus trigger

To have FocusGained (see above) work in plain vim, inside a terminal emulator (Xterm, tmux, etc) install the plugin: vim-tmux-focus-events

On tmux versions > 1.9, you'll need to add in .tmux.conf:

set -g focus-events on


for me it works when i use the command

:set autoread

Of course you have to save the file in the other editor before anything happens.


I know this post is a bit old but hopefully this will help someone like myself that came across a similar issue.

I don't know how autoload works but I do know how vimscript works which is usually how get things done. If you find this useful, upvote. To answer your question, don't edit the same file in other editors. Usually you will get a notification in vim if you want to reload the file. I was having this problem but my problem was different. I wanted to reload a css file after compiling it with hugo from scss. This is what I did, you can take my code ideas and make them work for your needs.

Usage

  1. Change style.scss to the file you're working on and add a pattern to autoload_files, these are the files you want to autoload. In my case I want to reload a file that ends with the extension 'content'.

  2. In the autocmd include the files that you're going to write, in my case 'style.scss'.

  3. Change the delay from 500 ms to whatever you want.

function! AutoLoadFiles(timer) abort
    let found_ids = []
    let autoload_files = ['content$'] "The files you want to autoload
    let openBuffers=map(gettabinfo(tabpagenr())[0].windows,{idx,val->getwininfo(val)[0]['bufnr']})
    for buf in openBuffers
    let name =  bufname(buf)
        for pat in autoload_files
            if match(name,pat) != -1 
                let fid = getbufinfo(buf)[0].windows[0]
                call add(found_ids,fid)
            endif
        endfor
    endfor
    for id in found_ids
        call win_execute(id,'edit')
    endfor
call timer_stop(a:timer)
endfunction
augroup AutoLoadFiles
    autocmd!
    au BufWrite *style.scss let s:delay_timer = timer_start(500, 'AutoLoadFiles',{'repeat':1})
augroup end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜