"Auto Yes" to Warnings On Save (Scriptability)

28 views
Skip to first unread message

Joe Clark

unread,
Mar 26, 2020, 11:58:25 AM3/26/20
to vim_use
I am looking for a way to periodically remove the first N lines of a file, to implement a "quick and dirty" log file size management scheme.

I have found that vi will effectively do this, and (somewhat surprisingly) the logging application seems to be okay with its log file being modified.

I have a vim script (del25.keys) with this command: d25dZZ (delete 25 lines and write/quit).

I run vi like this: vi -n -s del25.keys FILE

The problem I'm running into is that in the corner case that FILE is modified by the logging application at the same instant that vi is doing its processing, vi issues W11 on save (file externally modified).  I'm not too concerned about losing one or two log lines occasionally, so I want it to "auto yes" (suppress the warning) to avoid stopping my script.

I've tried a few different mechanisms to address this, including slightly different commands and FileChangedShell, but the warning remains.  Is there a way to actually suppress this warning so my script will run without "getting stuck in this corner case?  Thanks for your thoughts.

aro...@vex.net

unread,
Mar 26, 2020, 12:07:41 PM3/26/20
to vim...@googlegroups.com
> I am looking for a way to periodically remove the first N lines of a file,
> to implement a "quick and dirty" log file size management scheme.
>
Have you looked at tail and its options? sed would probably also work, but
tail is probably more obvious.

Gary Fritz

unread,
Mar 26, 2020, 12:29:43 PM3/26/20
to vim_use
Yes, I think tail is a much simpler option.  Just be aware you can't do this:

tail -n 25 myfile > myfile

... because the "> myfile" opens and truncates the file (throwing away the contents) before the tail command runs.

Something like this would work:

tail -n 25 myfile > /tmp/tail$$
mv /tmp/tail$$ myfile

Joe Clark

unread,
Mar 26, 2020, 12:58:16 PM3/26/20
to vim...@googlegroups.com
The problem with using tail, sed, or similar is that they use intermediate files.  For the use case I'm describing, I need to be modifying the SAME file (same inode), because I'm trying to change a "live" file that the other process is writing to.  Living dangerously, I know.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/1c8d9b61-45b1-4977-a285-59a8a3cc9045%40googlegroups.com.

Ed Blackman

unread,
Nov 18, 2021, 8:46:47 PM11/18/21
to vim_use
On Thu, Mar 26, 2020 at 07:59:38AM -0700, Joe Clark wrote:
> I am looking for a way to periodically remove the first N lines of a file,
> to implement a "quick and dirty" log file size management scheme.
>
> I have found that vi will effectively do this, and (somewhat surprisingly)
> the logging application seems to be okay with its log file being modified.

This is a very late response, but thought it might be good for the
archive in case someone was searching.

Several responders to the OP suggested sed or tail, which the OP didn't
want because he needed an in-place change that *didn't change the
inode*.

This is a good fit for... 'ed'! No, not me, ed the line editor.

$ seq 1000 > /tmp/nums.txt
$ wc -l /tmp/nums.txt
1000 /tmp/nums.txt
$ stat --printf "%i\n" /tmp/nums.txt
285890
$ printf '1,500d\nw\n' | ed /tmp/nums.txt
3893
2001
$ wc -l /tmp/nums.txt
500 /tmp/nums.txt
$ head -n 3 /tmp/nums.txt
501
502
503
$ tail -n 3 /tmp/nums.txt
998
999
1000
$ stat --printf "%i\n" /tmp/nums.txt
285890

You can see that ed is scriptable in a limited way (no loops or branches), but unlike vim doesn't assume interactive input, and saves in-place.

--
Ed Blackman
Reply all
Reply to author
Forward
0 new messages