HerrHongo
HerrHongo

Reputation:

Using VIM as a logfile-viewer

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

Upvotes: 15

Views: 9013

Answers (5)

idbrii
idbrii

Reputation: 11966

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.

Upvotes: 0

JohnLittle
JohnLittle

Reputation: 173

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.

Upvotes: 1

MacMartin
MacMartin

Reputation: 2876

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)

Upvotes: 3

Brian Agnew
Brian Agnew

Reputation: 272427

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

Upvotes: 5

Brian Rasmussen
Brian Rasmussen

Reputation: 116501

use :set autoread

Upvotes: 9

Related Questions