Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question

migrated from serverfault.com Jul 7 '13 at 0:58

This question came from our site for professional system and network administrators.

1  
Why must it be within vim? What's wrong with tail -f? –  SvW Jul 6 '13 at 14:16
    
Because I want the power of vim when I read logfiles. Like being able to search for pattern, and especially enjoy the syntax highlighting. I made my own syntax highlighting for some log files (bind, apache, etc.). –  Fox Jul 6 '13 at 14:21
    
1  
For just the searching, you could do less +F instead of tail -f. Doesn't give you the syntax highlighting though. –  Dennis Kaarsemaker Jul 6 '13 at 18:00
1  

3 Answers 3

You could be interested in autoread and FileChangedShell

:help autoread

:help FileChangedShell

share|improve this answer
    
I know these two commands. However I haven't succeeded to use them. I type "set autoread" on a file, then edit it outside of vim, and when I come back to vim, the file has not been reloaded... –  Fox Jul 6 '13 at 15:03
    
See here seem that with :au CursorHold * checktime you could update the file every time you press the cursor. Maybe you can fire that event in a timely way too. –  gsc-frank Jul 6 '13 at 15:29

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.

share|improve this answer

I found the answer thanks to Evan Teitelman's comment. My solution is inspired from /usr/share/vim/vimcurrent/macros/less.vim.

I made my own function, but it could be improved a lot.

function! Tailf()
    e
    normal G
    redraw

    sleep 1
    call Tailf()
endfunction

And just hit CTRL+C to exit Tailf.

To open a log file in Tailf mode: view -M "+call Tailf()" /path/to/logfile.log

What I don't like, is the sleep call, which doesn't allow to do anything in vim during the auto-refresh. The best would be if the buffer is autonomous and updates even if I'm in another split-window. Anyway, it's a good start. :)

share|improve this answer
    
It looks like it also rereads the entire file, which is not what you wanted, right? –  Bernhard Jul 7 '13 at 6:31
    
This does reload the entire file. It probably wouldn't be too difficult to make a plugin based on inotifywait and :read that waits until a file has been modified and if it has been appended to, reads in the lines at the bottom of the file. –  paraxor Jul 7 '13 at 13:46
    
Yes, it's true, it reloads the whole file, so this has to be a lot improved. When I say that I want this, I'm more talking about the result (what you see at the screen : a vim which updates your file alone and stays on the last line). Unfortunately I don't have enough vim knowledge to make a nice plugin... –  Fox Jul 7 '13 at 15:51
    
@EvanTeitelman OK, I see how to use inotifywait now. I can know whenever the file changes. But how to know if it has been appended (not completely modified), and how to know how many new lines have been appended? –  Fox Jul 7 '13 at 16:44
    
Read the number of lines in the file each time the file changes. If the number of lines increases, read in the changes. –  paraxor Jul 7 '13 at 17:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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