You could use !cp /dev/null %
, to truncate the currently viewed file. But as others have noted, it may not work correctly (e.g. if the application writing to the file holds the file open instead of opening/writing/closing for each entry). You might also try !mv -f % %.old && cp /dev/null %
, which will move the file out of the way then recreate it.
Either way, I see some strange behavior from less when trying these. (R does not refresh from disk, G does not seek to and display the current EOF (usually used to “pull in” new lines without going into ‘tail mode’), hqG reads in new lines from the file, but appends the entire contents of the file to the end of the buffer instead of just adding the new lines to the end (file has lines 1, then 1/2, then 1/2/3, etc. over time, less might show 1/1/1/2/1/2/1/2/3/1/2/3/1/2/3/4/1/2/3/4/1/2/3/4/5 if I was doing “hqG” twice after every new line)).
These same problems seem to happen with the “use v to truncate-by-editor” method, too.
could you provide a short explanation of how it works? I am not particularly familiar with either of the signs? – Jesper Rønn-Jensen – 2010-01-23T23:55:59.753
1I'll try. My english is not so great, though. And since we have little space for remarks, it will have to be split. Here we go: "!"/Exclamation mark invokes a shell to run what we type next (see man less). ">"/greater sign is the shell redirection command (see man bash). "%"/Percentage sign is the name of the current file (see man less). So in simple words we tell less to "start a shell (!) and redirect (>) to this file (%)". But what does it redirect? Since no file is given, it redirects "standard input" to the file. Standard input is empty, so "nothing" is redirected to the file... – Wolf – 2010-02-01T08:37:39.067
... so the file - in the end - has "nothing". It's empty. You can clean up all sorts of files that way. just type ">filename" on the shell prompt to empty the file "filename". See also "man bash" under "Redirecting Output" (bash has a long manpage...) Hope I could help. – Wolf – 2010-02-01T08:40:38.170
@Wolf why don't you edit that explanation directly into the answer? – Michael – 2015-06-03T19:50:41.477
+1 Nice trick. Also nicely cryptic ;-). – sleske – 2010-07-01T20:47:53.500