Just today I overwrote an important file accidentally. Fortunately, I had another terminal running `less’ that had been reading the overwritten file up to the state just before it was overwritten (i.e., I read the file in another terminal with `less’ and kept it open after I had overwritten the file). So, I wanted to save the content of the display buffer of `less’ to a file. But, how?
Googgling for `unix less buffer save to a file’ didn’t turn up any useful result. I was aware that pressing `v’ to invoke a text editor would make `less’ read the content of the overwritten file, and therefore, I would lose my original one. So, no, that was not the way to save the buffer to a file. At the end, desperately I started to copy-and-paste the screen manually. Unfortunately, I would lose all of the whitespace formatting characters by doing it that way. But, what else could I do?
After I finished copying-and-pasting the whole file, I deleted the overwritten file and pressed `v’ as to see whether `less’ would complain that the original file was missing and then, perhaps, offer me to save the buffer somewhere else. But, `less’ instead said: `cannot edit standard input’. Standard input? Oh, I did `cat file.txt | less’ then. But, standard input reminded me that `less’ can do piping of its buffer through a shell command! Oh, yes, so I went to the top of the buffer and pressed `|’ followed by `$’. When it asked for the command, I entered: `tee /home/eus/precious_file.txt’. Yeah, it did the trick and saved the day!
In short, to save the buffer that is being displayed by a session of `less’, use its pipe-to-shell-command capability by scrolling to the top of the file and press `|’ followed by `$’ as well as entering `tee DESTINATION_FILE’ when prompted for the shell command.