Thursday, March 26, 2009

Saving a file being viewed in less

Sometimes I have a long sequence of command line pipes that end in less command. For e.g.
cvs log main.c | egrep "^revision " | less
So what is the best way to save the contents that are being viewed in less? You cannot view those contents by typing the letter "v". If you do so you will get an error message saying "Cannot edit standard input (press RETURN)".

The solution is simple. First type "g" to go to the beginning of the file. Then type "|". Then you will be prompted for the "mark:" and enter "$" (which denotes the end of file). Then you can enter a command for which the entire contents of less will be piped. For e.g. I will give the following:
cat > /tmp/savefile.txt
Thats it! You have saved the contents to the file /tmp/savefile.txt. Now you have a problem. Since you have moved to the beginning of the contents, you might want to go back to the same place where you were before saving the contents. Just type "''" i.e. single quote twice. You should be back at where you were before you started saving the file.

Hope this little trick saves a lot of time for you! If yes, please drop me a word :-)

No comments: