Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 system and network administrators.

Why must it be within vim? What's wrong with tail -f? – Sven 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
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

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

vim --servername TAIL_VIM /tmp/somefile

Now, in another shell (like bash), you can do:

    while true
        inotifywait --event modify /tmp/somefile \
        && vim --servername TAIL_VIM --remote-send '<C-\><C-N>:edit!<CR>G';

Where the < C->< C-N> forces vim to go to normal mode, "edit!" tells vim to reload the current file (the CR simulates pressing enter), and G goes to the bottom of the file. Removing the (G) makes it easier to insepct the file while input is coming in.

share|improve this answer
I see the param "--servername" in the man, however I got following error message: VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Feb 10 2013 02:28:47) Option inconnue: "--servername=toto" Plus d'info avec: "vim -h" – Fox Aug 5 '14 at 10:52
if you do vim --version do you see +clientserver (not -clientserver, with a hypen)? If not, many people compile vim from source using ./configure --with-features=huge – crutux Aug 5 '14 at 17:35
Yes, there is +clientserver. I have the Ubuntu version. – Fox Aug 5 '14 at 20:34

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

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()
    normal G

    sleep 1
    call Tailf()

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. – Evan Teitelman 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. – Evan Teitelman Jul 7 '13 at 17:25

Your Answer


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.