How to make Vim behave like “tail -f”

tailvim

I would like to know if there is a way to make Vim behave like tail -f.
Even the best Vim plugin I've found so far doesn't do what I expect.

I really want to see the file update in real-time. Even I'm away from keyboard, I want Vim to constantly reload the buffer and jump to the last line.

How to do this?
(I don't want to reload the whole file, since some log files are very big. The best is to only load the last lines, like tail -f does.)

Best Answer

You can't make vim behave like tail -f. You can make less behave like a combination of vim and tail -f though.

Forward forever (follow) mode

less has a forward forever mode that you can enter by pressing F or by passing +F to it as an argument.

$ less +F

In this mode, less behaves like tail -f in that it doesn't stop reading when it reaches the end of a file. It constantly refreshes with new data from the file. To exit this mode, press Ctrlc.

Syntax highlighting

less supports automatic filtering of the data it reads. There is a program called source-highlight that can perform basic source code highlighting. It comes with a script that works well with less. To use it, just set the LESSOPEN environmental variable appropriately.

 export LESSOPEN="| /path/to/src-hilite-lesspipe.sh %s"

You also have to tell less to pass raw terminal escape sequences (these tell your terminal how to color text) by passing it the -R flag. You can tell less to pretend it is always being passed the -R flag by setting the LESS environmental variable.

 export LESS=' -R '

When less isn't enough

Although less has vi-like keybindings, it just isn't the same as Vim. Sometimes it feels foreign and it lacks important features such as ctags integration and the ability to edit text.

You can make less call Vim (assuming EDITOR=vim) on the file it is currently viewing by pressing v. less will even put your cursor in the correct location within Vim. When you exit Vim, you will find yourself back at less. If you made any changes to the file while you were in Vim, they will be reflected in less.

Related Question