15

I'd like to use VIM as a logfile-viewer. Is it possible to reload the current file in a regular time interval (~1s)?

5 Answers5

10

use :set autoread

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
  • 1
    This will only update the file when (g)vim has focus though. It also won't scroll to the bottom automatically. So it's not really all that useful. – rkrzr Feb 17 '14 at 13:16
  • @rkrzr if you have a better option please feel free to provide an answer and I'll be glad to upvote. – Brian Rasmussen Feb 17 '14 at 15:51
  • 1
    For me it didn't even update the file when vim had focus. (I'm on Windows 7 with VIM 7.3 2010 August 15.) In order to get it to update I had to put another window into focus and then click back into gVIM. – feuGene Mar 26 '14 at 11:58
5

See this VIM tip. It offers tailing (like tail -f) together with log line numbering

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • From the description, it looks like this plugin has problems under Windows and in any case updates only when the cursor is moved in Vim. Is this correct? – Don Reba Apr 27 '09 at 04:30
3

I like it short and without a lot of hacking or external scripts. You can run this oneliner from ex (whithin vim) when needed (or put each command in vimrc, for when log-files are opened.)

: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)

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)

I like the idea to watch logfiles in vim (instead of tail -f), e.g. when you are working in an ssh session without screen/tmux. Additionally you can copy directly from the logfile, if needed, or save the output directly or ... whatever you can do with vim :)

*from this answer (refering to an answer by PhanHaiQuang and a comment by flukus)

MacMartin
  • 2,366
  • 1
  • 24
  • 27
1

With the timers in Vim 8 this can now much simpler and less of a kludge. For example:

:set autoread
:function! Tailf(id)
:   checkt
:   $
:endfunc
:let timer_id = timer_start(4000, 'Tailf', {"repeat":-1})

This has vim rereading the whole log every 4 seconds. For many purposes that's fine, but would not be for large, say several GB, log files. There's other capabilities, like jobs and channels, that could be used for more sophisticated log reading.

JohnLittle
  • 133
  • 2
  • 6
0

asyncrun.vim lets you run programs and see live output in the quickfix (essentially JohnLittle's answer, but with setqflist() instead of Tailf()). You can use it with tail (I'm on Windows and my tail comes from my git install):

AsyncRun tail -f C:\logs\plugin-info.log

It has the additional benefit of using 'errorformat' to parse filenames out of the logs.

idbrii
  • 10,975
  • 5
  • 66
  • 107