Vim的autoread是如何工作的?

时间:2010-03-22 05:49:32

标签: vim

:h autoread说:

  

如果检测到文件已在Vim外部更改且Vim内部未更改,则会自动再次读取。

set autoread放入我的vimrc后,我用Vim打开一个文件,切换到另一个编辑器,更改文件,然后等待查看Vim中的更改。什么都没发生。我必须使用:e重新加载包含新内容的文件。

我错过了什么? 我在Mac 10.5.8上使用Vim 7.2

8 个答案:

答案 0 :(得分:36)

除非你做某事,否则Autoread不会重新加载文件 比如运行外部命令(比如!ls或!sh等) vim不会定期进行检查 您可以使用:e

手动重新加载文件

此主题中的更多细节: Click here

答案 1 :(得分:29)

我使用以下代码片段,每当我切换缓冲区或再次聚焦vim时触发autoread:

au FocusGained,BufEnter * :silent! !

此外,将它与下面的代码段结合使用是有意义的,这样在离开缓冲区或vim时总是保存文件,避免冲突情况:

au FocusLost,WinLeave * :silent! w

编辑:如果要通过禁用在save上运行的任何挂钩(例如linters)来加快写入速度,可以在w命令前加noautocmd

au FocusLost,WinLeave * :silent! noautocmd w

答案 2 :(得分:16)

根据my posting on superuser.com

Autoread只是不起作用。使用以下内容。

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

我通过直接调用setup函数得到了最好的结果,就像这样。

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

原因是我想运行ipython / screen / vim设置。

答案 3 :(得分:10)

在gvim之外,autoread对我不起作用。

为了解决这个问题,我使用了这个相当丑陋的黑客。

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

这似乎是脚本irishjava链接到的,但是这可以让你为缓冲区切换它。我只是想让它适用于所有事情。

答案 4 :(得分:2)

光标停止移动时触发

添加到您的vimrc

au CursorHold,CursorHoldI * checktime

默认情况下,CursorHold在光标保持静止4秒后触发,并可通过updatetime进行配置。

缓冲区更改触发器

要在更改缓冲区时触发autoread,请添加到vimrc

au FocusGained,BufEnter * checktime

终端窗口焦点触发器

FocusGained(见上文)在普通vim中工作,在终端模拟器(Xterm,tmux等)中安装插件: vim-tmux-focus-events

在tmux版本上> 1.9,您需要添加.tmux.conf

set -g focus-events on

答案 5 :(得分:1)

对我来说,当我使用命令

时它会起作用
:set autoread

当然,在发生任何事情之前,您必须将文件保存在其他编辑器中。

答案 6 :(得分:1)

参加聚会有点晚,但是现在vim带有计时器,您可以执行以下操作:

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

答案 7 :(得分:0)

我知道这篇文章有点旧,但希望这能帮助像我这样遇到类似问题的人。

我不知道自动加载是如何工作的,但我知道 vimscript 是如何工作的,这通常是获取事物的方式 完毕。如果您觉得这有用,请点赞。要回答您的问题,请不要编辑其他文件中的同一文件 编辑。如果要重新加载文件,通常会在 vim 中收到通知。我有 这个问题,但我的问题是不同的。我想在编译后重新加载一个 css 文件 来自 scss 的 Hugo。这就是我所做的,你可以采纳我的代码想法,让它们满足你的需求。

使用

  1. 将 style.scss 更改为您正在处理的文件,并向其添加模式 autoload_files,这些是您要自动加载的文件。在我的情况下,我想重新加载一个文件 以扩展名“内容”结尾。

  2. 在 autocmd 中包含您要编写的文件,在我的例子中是“style.scss”。

  3. 将延迟从 500 毫秒更改为您想要的任何值。

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
相关问题