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?
Open logfile and
:setlocal autoread
There is a plugin (Tail Bundle) on the vim site.
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)
Or a less elegant solution on a vim 7.x would be just do :e! whenever you need the update .
Related
What I want to do:
Inside a file/buffer in vim, especially an unnamed one (for example, created with :enew), sometimes I feel the content I'm writing is worth saving (say, I suddenly want to send it via an email), but don't feel the particular need to save it to a temp file, nor do I trust myself enough to "remember" to save it upon exit.
So I thought, what if I run
autocmd BufLeave * ggvG"+y
in the vim command line once and be free from the fear of losing this content.
But it doesn't work. When I exit vim, the system clipboard reminds intact.
My questions:
does it do anything if we run autocmd on the fly, as opposed to in vimrc?
is there a way to tell vim to "hey, when you exit, run these"?
Thanks a ton!
Two problems:
1) You didn't leave the buffer to go to another buffer (BufLeave); you left Vim (VimLeave).
2) autocmd expects a command, not normal mode keystrokes.
With that in mind,
autocmd VimLeave * normal gg"+yG
Sometimes I need to substitute across multiple buffers. For the purpose I use :bufdo %s/old/new/gec. Recently I noticed that the command fails when there is non-modifiable buffer in the buffer list (in my case it's opened file explorer/netrw). After running the command vim leaves me with E21: Cannot make changes, 'modifiable' is off and opened Netrw window.
Are the ways to :bufdo only on modifiable buffers? I've already tried :bufdo!, but the behaviour was the same (just without showing the error).
UPDATE
I find the line of .vimrc that poses this problematic behaviour:
let g:netrw_liststyle=3
I don't know what the magic here, but when I set this option neither of the suggested solutions/commands work for me. Now, the question is how to keep this line and make the :bufdo behaviour skip the buffer created by Netrw.
Well, if :bufdo sil! :%s/old/new/gec does not work for you (this silently ignores errors). you need to wrap the command into an if statement. Something like this:
:bufdo if &ma | :%s/old/new/gec | endif
which checks for each buffer, if it is modifiable and only then attempts to replace old by new.
Note: You might also want to check for the 'readonly' option in addition to the 'modifiable' setting.
If I have this line in my vimrc
au VimLeave %bdelete
Then whenever vim starts it says
--- Auto-Commands ---
Press ENTER or type command to continue
I have that line there to empty the buffers from gvim, because new gvim instances have massive :ls output from previous runs. Notably, gvim doesn't actually produce this prompt.
Obviously I can set this instance up to only occur during gvim startup and not console vim, but I'd like to understand this rather than avoid it. Mostly I'm confused that VimLeave seems to cause things to happen on startup.
TIA
Altreus
The problem is that this is an incomplete :autocmd definition, so Vim attempts to list all VimLeave autocommands defined for the pattern %bdelete. You need to specify the any file pattern to make it work:
au VimLeave * %bdelete
Also, check whether you have % in your 'viminfo' option; that one enables the saving and restoring of the buffer list you're complaining about. The f option of file marks may also result in buffers being restored; you could try :set viminfo+=f0.
New Vim instances don't inherit the buffer list of the previous instance unless you add % to the viminfo option.
Setting that option to a sane value will remove the need for your broken fix. Reading the documentation before adding options to your config will prevent you from similar issues.
See :help 'viminfo'.
Is there something like a close-window-hook in vim/vimscript, so that I can call a function every time a window is closed?
I want to use it for the following scenario:
I use an extra scratch window to display information about the file, and when I close the file I want the scratch window to be closed automatically so that vim exits.
If you have any ideas how to achieve that without a hook that will be just as fine.
edit:
I know about :qa[ll], but I am lazy and only want to type :q or ZZ.
edit2.71828183:
I accepted the autocommand answer as it was closest to the original question, but found another solution in using a preview window instead of a split window. A preview window is automatically closed when the last "normal" window is closed..
autocommands are amazing! I'm pretty sure that in this case, BufLeave will do the job, but you might want BufWinLeave? Have a look at :help autocmd-events for a full list of events.
The other bit you'll care about is: you can have buffer-local autocommands! (:help autocmd-buflocal)
You can define one for the current buffer using au BufLeave <buffer> .... My best guess is that you could run this in whatever command creates the scratch window. You should be able to cache the scratch window's buffer number in a global variable when you open the scratch window, then your autocommand could just delete that buffer (:help :bdelete).
au BufLeave <buffer> bdelete g:scratch_buffer
call CreateScratchWindow()
function CreateScratchWindow() {
...
let g:scratch_buffer = bufnr("")
}
There's also a function winbufnr, for getting buffer numbers by window. You can use either one - just make sure that the scratch window/buffer is current when you use it! (The "" means current window/buffer).
Add a mapping.
:nnoremap :q :qa
then, if you only want to use :q one day, you can just
:qa<backspace>
and normally, you can just
:q<enter>
and get
:qa<enter>
for free.
Ok, not really a new question, but I found it looking for, what I now know, BufWinLeave. However, the scenario the author is describing is something I have in my vimrc with NerdTree, e.g. when the last buffer is closed, I want vim to close the NerdTree "buffer" and then quit.
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
Replace the NERDTree specific stuff with how the scratch buffer is identified.
You can use
:quitall
or
:qall
to close all windows at once.
add a ! if you want to ignore unsaved changes.
:qall!
I'd like to use VIM as a logfile-viewer. Is it possible to reload the current file in a regular time interval (~1s)?
use :set autoread
See this VIM tip. It offers tailing (like tail -f) together with log line numbering
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")
(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)
I like the idea to watch logfiles in vim (instead of tail -f), e.g. when you are working in an ssh session without screen/tmux. Additionally you can copy directly from the logfile, if needed, or save the output directly or ... whatever you can do with vim :)
*from this answer (refering to an answer by PhanHaiQuang and a comment by flukus)
With the timers in Vim 8 this can now much simpler and less of a kludge. For example:
:set autoread
:function! Tailf(id)
: checkt
: $
:endfunc
:let timer_id = timer_start(4000, 'Tailf', {"repeat":-1})
This has vim rereading the whole log every 4 seconds. For many purposes that's fine, but would not be for large, say several GB, log files. There's other capabilities, like jobs and channels, that could be used for more sophisticated log reading.
asyncrun.vim lets you run programs and see live output in the quickfix (essentially JohnLittle's answer, but with setqflist() instead of Tailf()). You can use it with tail (I'm on Windows and my tail comes from my git install):
AsyncRun tail -f C:\logs\plugin-info.log
It has the additional benefit of using 'errorformat' to parse filenames out of the logs.