120

My question is similar to this how to monitor a text file in realtime but I want to do it in vim. I know I can read an opened file use tail -f sample.xml file, and when new content is written to the file, it'll also write the new content to my screen. Can I have vim automatically fill the new data when a file is updated?

1

10 Answers 10

114

You can :set autoread so that vim reads the file when it changes. However (depending on your platform), you have to give it focus.

From the help:

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. When the file has been deleted this is not done.

6
  • 7
    Thanks for the hint! This looks pretty promising, but it doesn't work on my system :( I'm using Mac10.6.2 iTerm with vim version 7.2.303 compiled with MacVim. Any additional comment I can try?
    – Patrick
    Commented Jan 29, 2010 at 4:05
  • erm, that's pretty weird. are you using the macvim gui or the terminal version? it works for me with macvim gui downloaded precompiled. (I do have to click on the app to give it focus though, as I mentioned.)
    – Peter
    Commented Jan 29, 2010 at 4:13
  • 1
    I was testing in terminal, after I used gvim (MacVim GUI) the function began to work! As you mentioned though, I need to focus gvim to update the content. Do you have any trick to update it even without focusing? Thanks for your help again.
    – Patrick
    Commented Jan 29, 2010 at 4:20
  • 3
    I don't know a solution to that, unfortunately. I'd be stunned if vim could do it without the change of focus - it would require vim to poll the filesystem to see when it changes. I think you'd need a plugin for that.
    – Peter
    Commented Jan 29, 2010 at 6:19
  • 2
    @Peter I made such a plugin a while ago. Also see this question and my answer to it. for more details as to how autoread works and its limitations. Commented Mar 3, 2016 at 20:08
65

Don't know about automatically, but you can type:

:e!

to reload the file

1
  • Although this approach does not auto update the content, it does display the updated content. Thanks for your answer!
    – Patrick
    Commented Jan 29, 2010 at 4:07
41

Put the following in your .vimrc:

" check one time after 4s of inactivity in normal mode
set autoread                                                                                                                                                                                    
au CursorHold * checktime                                                                                                                                                                       
6
  • 1
    Great! Works well, even with a warning if you have changed that file since last reloading.
    – Joey.Z
    Commented Jul 2, 2014 at 10:58
  • 2
    First answer didn't work for me, but this works! :-) Commented Oct 19, 2014 at 6:08
  • 5
    The "every 4 seconds" isn't true. This will only check one time after 4s of inactivity in normal mode. So if you don't do anything in another buffer for a long time, it won't be updated, but it would if you just move the cursor and wait 4s. Another option is to manually call ":checktime" to update (after setting autoread). Unfortunately there doesn't seem to be any sort of polling support in vim, so there is no true answer to the OPs question. Commented Feb 7, 2015 at 21:28
  • 1
    Just came across this. If you change checktime to call a custom function and add " call feedkeys("lh")" to the end of the function then it will fire every 4 seconds.
    – flukus
    Commented Mar 12, 2016 at 23:06
  • @DavidLjungMadison you are right, I am going to edit the post to avoid this mistake Commented Sep 4, 2016 at 6:51
19

like @flukus said in a comment to a previous answer you can call feedkeys["lh"] (it moves the cursor to the right and back to the left, which normaly doesn't do harm when viewing a log file)

So, if you combine the rest of the answer you have a oneliner you can run from ex (whithin vim) when needed:

:set autoread | au CursorHold * checktime | call feedkeys("lh")


***(if you would want to jump (nearly) to the end of the file, just use "G" instead of "lh" with feedkeys)***

Explanation:

  • autoread: reads the file when changed from the outside (but it doesnt work on its own, there is no internal timer or something like that. It will only read the file when vim does an action, like a command in ex :!
  • CursorHold * checktime: when the cursor isn't moved by the user for the time specified in 'updatetime' (which is 4000 miliseconds by default) checktime is executed, which checks for changes from outside the file
  • call feedkeys("lh"): the cursor is moved once, right and back left. and then nothing happens (... which means, that CursorHold is triggered, which means we have a loop)

Additionally you can :set syntax=logtalk to color the log

To stop the scrolling when using call feedkeys("G"), execute :set noautoread - now vim will tell, that the file was change ans ask if one wants to read the changes or not)

(Does this have any sideeffects?)

Edit: I see one side-effect: if one uses "G" as the feedkey, it will scroll down every currently opened buffer?! So, it's not possible to work in the left buffer of a splittet window while having the right buffer scroll down a logfile automatically

Edit2: Another side effect is that when you enter the command line window(by using q:) an error message always pops up.

2
  • how to modify the above command to check for and change in every 1 second instead of 4 seconds
    – user8234870
    Commented Feb 18, 2022 at 2:50
  • set updatetime=1000 put this in your vimrc file
    – user8234870
    Commented Feb 18, 2022 at 3:16
4

Stick this in your .vimrc and it should work like a boss. (Taken from: 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)
4
  • 1
    This is the preferred answer to address terminal autoread shortcomings.
    – mcanfield
    Commented Sep 18, 2015 at 21:07
  • 3
    @mcanfield No it isn't as this still doesn't "watch for changes". You still need to have Vim active and it is still polling (not watching) on a limited set of events. CursorHold is run once. So if you go off and have a coffee, or are doing something in another Window it will not update. Commented Mar 3, 2016 at 20:12
  • 4
    That script is also bundled as a Vim plugin: vim-autoread. Commented Sep 11, 2018 at 19:43
  • 1
    @stephen.hanson Thanks for the link, that plugin is working well for me!
    – Tropilio
    Commented Apr 10, 2020 at 8:48
3

Tail Bundle should do what you want. Note, haven't used it myself.

0
2

There is a plugin also:

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

This was the only way I could make this work on OSX.

1

If unix + neovim

:term tail -f <filename>

Obviously this won't work for everyone, but it's how I do it.

1
  • All this does is open a terminal window within vim, and uses the original command. That doesn't let you use any of vim's features with the file - no syntax highlighting, folding, navigation etc etc Commented Jan 13, 2022 at 0:58
0

VIM will warn you when a file has been updated so that you don't overwrite changes that have been made since you opened it. It will prompt you at that point to reload the file.

1
  • 2
    Thanks for your answer, but vim did not warn me when the file was changed in my system :(
    – Patrick
    Commented Jan 29, 2010 at 4:06
0

To enable reloading if you open a specific file, you can add this to your .vimrc and use a modeline like vim: set ft+=.watch:. It takes advantage of the feature to set multiple filetypes for a buffer (see below):

vim9script

command WatchFiles {
    autocmd! AUWatchFile FocusGained,VimResume,BufEnter,WinEnter,CursorHold * checktime
    autocmd! AUWatchFile BufEnter,InsertEnter,CursorHold,CursorHoldI <buffer> checktime
    setlocal autoread
    checktime
}

command UnwatchFiles {
    autocmd! AUWatchFile
    set autoread<
}


# To enable this, you may use e.g. a modeline: `vim: set ft+=.watch`
def WatchAutomatically()
    # Check if the "list" of filetypes (a dot separated string) contains 'watch'.
    if -1 != match(&filetype, "\\(^\\|\\.\\)watch\\($\\|\\.\\)")
        WatchFiles
    endif
enddef


augroup AUWatchFile | augroup END
autocmd BufWinEnter * call WatchAutomatically()

More details

You are able to set multiple filetypes separated by .:

When a dot appears in the value then this separates two filetype names. Example: /* vim: set filetype=c.doxygen : */ ~

See :help 'filetype'. Keep in mind that you should manipulate the filetype first and set additional options after that in the modeline. Otherwise these options may be overridden by the settings specific for the filetype.

Btw, the above is a Vim9 script (which I discovered today). Translating it back to a good-old Vim script is trivial:

  • Use " for comments.
  • Transform multi-line commands like this:
    command UnwatchFiles
        \ autocmd! AUWatchFile
        \ | set autoread<
    
  • Remove the vim9script line.

See :help Vim9-script for details.

Shortcomings

  • Reloading is not limited to the buffer which contains the modeline. You may disable it again with :UnwatchFiles.
  • It is still dump polling.

Not the answer you're looking for? Browse other questions tagged or ask your own question.