简体   繁体   English

Vim 的自动阅读是如何工作的?

[英]How does Vim's autoread work?

:h autoread says: :h autoread说:

When a file has been detected to have been changed outside of Vim and it has not been changed inside of Vim, automatically read it again.当检测到一个文件在 Vim 之外被更改而它在 Vim 内部没有被更改时,自动再次读取它。

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.set autoread放入我的vimrc ,我用 Vim 打开一个文件,切换到另一个编辑器,更改文件,并等待查看 Vim 中的更改。 Nothing happens.没发生什么事。 I have to use :e to reload the file with the new content.我必须使用:e用新内容重新加载文件。

What did I miss?我错过了什么? I'm using Vim 7.2 on Mac 10.5.8我在 Mac 10.5.8 上使用 Vim 7.2

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除非您执行诸如运行外部命令(如 !ls 或 !sh 等)之类的操作,否则自动读取不会重新加载文件 vim 不会定期进行检查,您可以使用: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:我使用以下代码段,每当我切换缓冲区或再次聚焦 vim 时,它都会触发自动读取:

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:此外,将它与以下代码段结合使用是有意义的,以便在离开缓冲区或 vim 时始终保存文件,避免冲突情况:

au FocusLost,WinLeave * :silent! w

EDIT: If you want to speed up the write by disabling any hooks that run on save (eg linters), you can prefix the w command with noautocmd :编辑:如果您想通过禁用在保存时运行的任何挂钩(例如 linter)来加快写入速度,您可以使用noautocmdw命令前加上noautocmd

au FocusLost,WinLeave * :silent! noautocmd w

As per my posting on superuser.com根据我在 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 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.我通过直接调用 setup 函数得到了最好的结果,就像这样。

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

The reason for this, is that I want to run a ipython/screen/vim setup.这样做的原因是我想运行 ipython/screen/vim 设置。

A bit late to the party, but vim nowadays has timers, and you can do:聚会有点晚了,但现在 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

Outside of gvim, autoread doesn't work for me.在 gvim 之外,autoread 对我不起作用。

To get around this I use this rather ugly hack.为了解决这个问题,我使用了这个相当丑陋的 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.这似乎是链接到的脚本 irishjava 所做的,但这可以让您为缓冲区切换它。 I just want it to work for everything.我只是想让它适用于一切。

Trigger when cursor stops moving光标停止移动时触发

Add to your vimrc :添加到您的vimrc

au CursorHold,CursorHoldI * checktime

By default, CursorHold is triggered after the cursor remains still for 4 seconds, and is configurable viaupdatetime .默认情况下, CursorHold在光标保持静止 4 秒后触发,并且可以通过updatetime 进行配置。

Buffer change trigger缓冲区变化触发器

To have autoread trigger when changing buffers, add to your vimrc :要在更改缓冲区时触发自动autoread ,请添加到您的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要让FocusGained (见上文)在普通 vim 中工作,请在终端模拟器(Xterm、tmux 等)内安装插件: vim-tmux-focus-events

On tmux versions > 1.9, you'll need to add in .tmux.conf :在 tmux 版本 > 1.9 上,您需要添加.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.我不知道自动加载是如何工作的,但我知道 vimscript 是如何工作的,这通常是完成工作的方式。 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.如果要重新加载文件,通常会在 vim 中收到通知。 I was having this problem but my problem was different.我遇到了这个问题,但我的问题是不同的。 I wanted to reload a css file after compiling it with hugo from scss.我想在用 scss 中的 Hugo 编译后重新加载一个 css 文件。 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.将 style.scss 更改为您正在处理的文件,并向 autoload_files 添加模式,这些是您要自动加载的文件。 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'.在 autocmd 中包含您要编写的文件,在我的例子中是“style.scss”。

  3. Change the delay from 500 ms to whatever you want.将延迟从 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM