Clear terminal output of last command only

13

2

Rather than scroll past hundreds of lines of output to see the previous command history, can the output of the last command be cleared? E.g. after executing ls, clear its output because you don't need it any more.

EDIT:

This is not your usual clear or ctrl+L operation. The idea is to scroll back though the history looking for the output of a previous command, but without having to have to scroll past a load of ls, or cat, or sudo apt-get install output unless I really want to. It might look something like this:

cat fileIWantToRemember
ls directoryIWantToForget
# some command that wipes the output of ls
# scroll back to see the output of cat immediately

nacnudus

Posted 2013-09-24T23:19:18.693

Reputation: 271

1it is not your exact answer but in such cases to view content of big files I use 'less' command instead of 'cat'. Another thing is if any command give huge number of lines as output to console you can avoid it by writing "$ the-command | less". – Devasish – 2020-01-29T11:40:26.373

Do you mean to clear the output, or to remove the command from your command history? If you want to modify your command history, maybe this thread addresses what you're looking for?

– ernie – 2013-09-24T23:40:04.920

The terminal just displays a stream of text, and stores some in its own internal buffer. It has no way of knowing, or discriminating between command output. The rxvt-unicode terminal does have a perl interpreter that might be able to implement something like this. But as far as I know it's not done. – Keith – 2013-09-25T04:06:18.210

A long time ago, I used a terminal emulator on Mac OS X which had this feature, but I can't remember what it was called any more. It wasn't free, though. However, while doing a quick google search for it, I ran into http://finalterm.org, which looks like it is trying to implement that feature. However, "Final Term is in heavy development and neither stable nor feature complete!", so I didn't try it. Looks interesting, though. The google search, for what it's worth, was terminal emulator output folding

– rici – 2013-09-25T04:38:43.497

Final Term is heavily-enough developed to fold commands (and lots of other amazing stuff), and once there's a keyboard shortcut to do that then problem solved. I was beginning to think I was crazy... – nacnudus – 2013-09-25T10:50:55.810

@nacnudus: If you try it and it works for you, you should self-answer the question. That will provide guidance for other people with the same problem. – rici – 2013-09-25T15:28:37.747

Answers

4

With thanks to @rici, this behaviour can be called "folding". The output of commands isn't forgotten, but you can hide it until you need it again. Folding is common in text editors but seems to be rare in terminal emulators.

Final Term is a new terminal emulator that includes folding among many other fancy tricks. Beware: "Final Term is in heavy development and neither stable nor feature complete!" It was heavily-enough developed to fold my commands when I tested it, but too unstable not to crash. One to watch.

nacnudus

Posted 2013-09-24T23:19:18.693

Reputation: 271

0

I think you are looking for the clear command...

clear

"clear" clears your last command from view. You can scroll up in the terminal to see it again if need be.

Justin C.

Posted 2013-09-24T23:19:18.693

Reputation: 11

The point is to not have to scroll past it when looking through your history for something else. – nacnudus – 2013-09-24T23:34:12.770

Do you mean you want to suppress the output from the command? If yes, this may help you: http://stackoverflow.com/questions/617182/with-bash-scripting-how-can-i-suppress-all-output-from-a-command

– phe0113 – 2013-09-24T23:41:29.437

2That's great if I never want to see output. I want to see the output the first time, and then forget about it. So in the example I gave, I want to inspect the output of ls for some reason, but then I want to get rid of it so that I don't have to keep scrolling past it to find the output of cat. Obviously I could just do cat again, but I can imagine wanting to retain a sequence of commands, and it would be nice not to have to repeat them all. – nacnudus – 2013-09-25T00:00:04.203

0

In bash, you can press the shortcut Ctrl + L to clear the screen, similar to the clear command. (http://ss64.com/bash/syntax-keyboard.html)

Or, you can use history | tail -3 to see the last three commands. (http://www.howtogeek.com/howto/44997/how-to-use-bash-history-to-improve-your-command-line-productivity/)

phe0113

Posted 2013-09-24T23:19:18.693

Reputation: 161

See my comment to @Justin below, the point is not to have to scroll past it when looking through the history for something else. Something like HISTCONTROL=ignorespace in your second link that you can use after the fact would be good. – nacnudus – 2013-09-24T23:42:10.473

I'm a bit lost on what you want to achieve. Another thought, in many consoles I used, you can usually use arrow keys to browse command history in bash, does this apply to your situation? – phe0113 – 2013-09-24T23:47:40.233

Does my edit clarify the question? – nacnudus – 2013-09-24T23:56:37.523

@nacnudus, yes, I get your point now :) – phe0113 – 2013-09-25T00:22:15.993

0

It sounds as if you want to remove items from your history. To do this, you'll first need to enable the writing of history on the fly, rather than on logout by editing your ~/.bashrc and adding:

shopt -s histappend; export PROMPT_COMMAND='history -a';'

Then, to delete items you can do:

history -d offset, where offset is the history entry you want to delete

Based on this forum thread.

ernie

Posted 2013-09-24T23:19:18.693

Reputation: 5 938

This seems to delete it from the history file, which is almost the right idea, but can it wipe it from the current terminal? – nacnudus – 2013-09-24T23:56:09.407