Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tail like functionality for gvim

Tags:

vim

I want to use gvim to view a log file which is being updated continuously, such that I always see the last updated line, much like tail command in unix. Is it possible?

like image 738
Canopus Avatar asked May 15 '09 09:05

Canopus


People also ask

What does the tail command do?

Tail is a command which prints the last few number of lines (10 lines by default) of a certain file, then terminates. Example 1: By default “tail” prints the last 10 lines of a file, then exits. as you can see, this prints the last 10 lines of /var/log/messages.

How do you tail a certain number of lines?

The tail command is used to print last 10 lines of a file by default. However, like the head command, we can change the number number of lines to be displayed by using the -n option, or just -<number> , to display a different number of lines as specified.

How do you get out of tail F command?

In less , you can press Ctrl-C to end forward mode and scroll through the file, then press F to go back to forward mode again. Note that less +F is advocated by many as a better alternative to tail -f . For difference and caveats between the two tools, read this answer: Is `tail -f` more efficient than `less +F`?


2 Answers

Open logfile and

:setlocal autoread 

There is a plugin (Tail Bundle) on the vim site.

like image 170
Mykola Golubyev Avatar answered Oct 09 '22 01:10

Mykola Golubyev


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

and additionally you can :set syntax=logtalk to color the log

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

*from this answer (refering to an answer by PhanHaiQuang and a comment by flukus)

like image 25
MacMartin Avatar answered Oct 09 '22 03:10

MacMartin