Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is there a way to clear up what is displayed on the shell console (in ubuntu linux) in just a single key or two? When I get flood of lines from grep and that exceeds the height of the window, I want to quickly scroll back to the position where I typed the command after the prompt. I can do so if I had opened a fresh terminal before I type the command, and just go to the initial position. But if keep using the same terminal window, I have to manually find the position. Ctrl+l is not an answer because it just scrolls, and the contents of the terminal remains. I cannot use less because the colored output of grep does not show up.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You can actually stick with using less - try this:

grep --color=always foo * | less -r

That will tell grep to ignore that its output is being piped, and the "-r" on less tells it to pass through raw control characters, which in this case are the color codes.

share|improve this answer
    
I tried it, but it does not work. Is it interfering with other options? I did grep -nrI --color=always –  sawa Mar 31 '11 at 19:12
    
Hm, I did: grep -nrI --color=always foo * | less -r and it worked the same, showing colors in less. This was using bash as a shell with TERM set as 'xterm' –  Code Friendly Mar 31 '11 at 19:17
    
I am using gnome terminal. Do you use that terminal? –  sawa Mar 31 '11 at 19:21
    
I just tried it in gnome-terminal, the XFCE terminal, and Konsole and it worked in all three. Maybe try with '-R' instead of '-r' on less? That tells it to only accept only ANSI color codes. Maybe some other misc control characters are sneaking through the output of grep. If that does not work, I'm not sure what else would be causing the colors to get eaten. BTW, I am testing this with an up-to-date installation of 10.10 in case versions are the culprit here. –  Code Friendly Mar 31 '11 at 19:30
    
I did work. Actually, in my .bashrc, I had grep aliased to grep -nrI --color-always and less aliased to less -r, and typing the combination of grep and less with a pipe on the console did not work, but when I typed the whole line that you gave me directly in the terminal, it worked. –  sawa Mar 31 '11 at 19:36

If the console gets jammed/full then:

  • reset will clear and reset the state. You can also cause this to be sent via the magic sequence Ctrl-v Esc c Enter
  • clear if you just want to clear the screen; you might combine it with the output you're after: clear ; grep -r …

(BTW, reboot is a separate command, so don't worry about accidentally rebooting your machine by using clear or reset!).

share|improve this answer
    
Thank you for your suggestion. –  sawa Apr 1 '11 at 1:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.