vim可以监视文件的实时更改

我的问题类似于这个如何实时监控一个文本文件,但我想在vim中完成。 我知道我可以读取一个打开的文件,使用tail -f sample.xml文件,当新的内容写入文件时,它也将新的内容写入我的屏幕。 当文件更新时,我可以自动填充新的数据吗?

您可以:set autoread以便vim在更改时读取文件。 然而(取决于你的平台),你必须给它的焦点。

从帮助:

当一个文件被检测到在Vim之外被更改,并且在Vim里面没有被更改时,自动再次读取它。 当文件被删除时,这个没有完成。

不知道自动,但你可以键入:

 :e! 

重新加载文件

将以下内容放在.vimrc

 “在正常模式下静止4秒后检查一次
设置autoread                                                                                                                                                                                    
 au CursorHold * checktime                                                                                                                                                                       

尾巴捆绑应该做你想要的。 请注意,自己并没有使用它。

还有一个插件:

https://github.com/djoshea/vim-autoread

这是我可以在OSX上完成这个工作的唯一方法。

当文件被更新时,VIM会警告你,这样你就不会覆盖自打开文件以来所做的更改。 它会提示你在这个时候重新加载文件。

坚持这个在你的.vimrc中,它应该像老板一样工作。 (摘自: http : //vim.wikia.com/wiki/Have_Vim_check_automatically_if_the_file_has_changed_externally )

 " Function to Watch for changes if buffer changed on disk function! WatchForChanges(bufname, ...) " Figure out which options are in effect if a:bufname == '*' let id = 'WatchForChanges'.'AnyBuffer' " If you try to do checktime *, you'll get E93: More than one match for * is given let bufspec = '' else if bufnr(a:bufname) == -1 echoerr "Buffer " . a:bufname . " doesn't exist" return end let id = 'WatchForChanges'.bufnr(a:bufname) let bufspec = a:bufname end if len(a:000) == 0 let options = {} else if type(a:1) == type({}) let options = a:1 else echoerr "Argument must be a Dict" end end let autoread = has_key(options, 'autoread') ? options['autoread'] : 0 let toggle = has_key(options, 'toggle') ? options['toggle'] : 0 let disable = has_key(options, 'disable') ? options['disable'] : 0 let more_events = has_key(options, 'more_events') ? options['more_events'] : 1 let while_in_this_buffer_only = has_key(options, 'while_in_this_buffer_only') ? options['while_in_this_buffer_only'] : 0 if while_in_this_buffer_only let event_bufspec = a:bufname else let event_bufspec = '*' end let reg_saved = @" "let autoread_saved = &autoread let msg = "\n" " Check to see if the autocommand already exists redir @" silent! exec 'au '.id redir END let l:defined = (@" !~ 'E216: No such group or event:') " If not yet defined... if !l:defined if l:autoread let msg = msg . 'Autoread enabled - ' if a:bufname == '*' set autoread else setlocal autoread end end silent! exec 'augroup '.id if a:bufname != '*' "exec "au BufDelete ".a:bufname . " :silent! au! ".id . " | silent! augroup! ".id "exec "au BufDelete ".a:bufname . " :echomsg 'Removing autocommands for ".id."' | au! ".id . " | augroup! ".id exec "au BufDelete ".a:bufname . " execute 'au! ".id."' | execute 'augroup! ".id."'" end exec "au BufEnter ".event_bufspec . " :checktime ".bufspec exec "au CursorHold ".event_bufspec . " :checktime ".bufspec exec "au CursorHoldI ".event_bufspec . " :checktime ".bufspec " The following events might slow things down so we provide a way to disable them... " vim docs warn: " Careful: Don't do anything that the user does " not expect or that is slow. if more_events exec "au CursorMoved ".event_bufspec . " :checktime ".bufspec exec "au CursorMovedI ".event_bufspec . " :checktime ".bufspec end augroup END let msg = msg . 'Now watching ' . bufspec . ' for external updates...' end " If they want to disable it, or it is defined and they want to toggle it, if l:disable || (l:toggle && l:defined) if l:autoread let msg = msg . 'Autoread disabled - ' if a:bufname == '*' set noautoread else setlocal noautoread end end " Using an autogroup allows us to remove it easily with the following " command. If we do not use an autogroup, we cannot remove this " single :checktime command " augroup! checkforupdates silent! exec 'au! '.id silent! exec 'augroup! '.id let msg = msg . 'No longer watching ' . bufspec . ' for external updates.' elseif l:defined let msg = msg . 'Already watching ' . bufspec . ' for external updates' end echo msg let @"=reg_saved endfunction let autoreadargs={'autoread':1} execute WatchForChanges("*",autoreadargs) 

防弹(跨OS)解决scheme:

 :autocmd WinEnter <buffer> :view 

这不是实时的,但每次进入时刷新缓冲区。

如果unix + neovim

:term tail -f <filename>

显然这不适用于每个人,但这是我如何做到的。