I'm trying to do something very simple:
I have two buffers in Vim, one with source code (B1), another one with a text file a.txt
(B2). The source code in B1 is run with a custom shortcut in Vim, filling a.txt
with text. I want Vim to automatically scroll B2 every time it updates, even if my cursor is in B1. So, I just want that the buffer B2 to behave the way tail -f
does.
Doing this with ConqueTerm is not an option, since buffer B2 can be a preview buffer from other plugin.
This command moves to the window containing
a.txt
, reloads it (edit
), moves to the end (+$
) and then jumps back to the previous window. You can add it to the shortcut that runs your source code to geta.txt
to update whenever the code is run.:exe bufwinnr("a.txt") . "wincmd w" | edit +$ | exe winnr("#") . "wincmd w"
Remember to escape the bars (
\|
) and add a<CR>
at the end if you use this in a mapping.This will require your shortcut to run the code in foreground, and Vim will be unresponsive till it is done (unless you press Ctrl-C). If you want Vim to reload
a.txt
automatically whenever it is changed (thus bypassing the need for running the code in foreground), you will need an external mechanism for file change detection (such asinotifywait
on Linux). As far as I know, Vim only checks for external changes (withautoread
) when you move the cursor to that window.In general, vim events fire in response to user input. They don't just run continuously in the background.
This post details some tricks you could repurpose to write some customization code to reload your "tail -f" buffer and scroll to the bottom periodically.
Rather than trying to do this all in vim, here is a different proposal that would achieve a similar effect using GNU Screen (one terminal area with vim editing a file
file1
, another runningtail -f
against a different filefile2
):apt-get install screen
)screen
vim file1
Ctrl-A S
to split the terminal down the middle horizontally (or, in recent versions ofscreen
,Ctrl-A |
to split it down the middle vertically)Ctrl-A Tab
to switch to the other side of the splitCtrl-A c
to start a new terminal on this sidetail -f file2
Now one side of the split shows
vim file1
and the other side showstail -f file2
. Here's an image of the result.