Whenever I open a terminal, the last used commands appear.

I'd like to get a clean terminal when I open it, but I haven't found how to do it.

For example if while using the terminal I do:

defaults write com.apple.Finder...etc

and then I close the terminal. Next time I open the terminal I will see this same line grayed out (to indicate it's a command I used last time I opened the terminal) and a line to start writing underneath.

  • Can you give an example of what you see when you open the terminal? – slhck Jul 26 '11 at 9:14
  • I aliased clear to a proper output window clearing command on my machine. Will look it up in about six hours unless someone else provides an answer before then. – Daniel Beck Jul 26 '11 at 9:28
  • @Daniel I would really appreciate that, thanks! – Proxify Jul 26 '11 at 9:36

Add the following at the very end of .bash_profile:

printf '\033c'

This will really clear the Terminal. I even defined the following function, since I didn't like the scrolling behavior of the default clear:

function clear {
    printf '\033c'

I think it's the same as a hard reset of the Terminal (Cmd-Opt-R), but I'm not entirely sure.

| improve this answer | |
  • this is not the answer though - Lion restores windows from the previous Terminal session. What you need to do is turn off Resume for Terminal like Josh says below. Also, you need to learn to close windows you're no longer using before quitting the application in Lion. – w00t Aug 13 '11 at 15:08
  • 1
    @w00t Well I guess you're understanding the question differently than me. The user is only concerned about the grey lines related to his previous session; nowhere are the windows actually mentioned. Sessions are also new; which is easily visible when cding somewhere, then closing and reopening Terminal. There's just the grey lines from the end of the previous session. Between my answer and the others', he surely can find a solution to his problem, whichever interpretation is correct. By the way, my answer has the checkmark, although it wasn't the first one, so it can't be that bad. – Daniel Beck Aug 13 '11 at 15:11
  • ESC c is "Full Reset (RIS)", which is the same as Shell > Send Hard Reset; however, that merely clears the display and resets the terminal state. It doesn't clear the scrollback. If all Proxify is looking for is a way to clear the screen, just use the normal clear command (not the clear function given in this answer). Otherwise, ESC c is insufficient, and performs a completely unnecessary terminal reset. – Chris Page Feb 10 '12 at 18:02
  • @ChrisPage I could have sworn that it cleared the scrollback too. Do you have still have a 10.6 system around? When I posted this, I was still on Snow Leopard. On Lion it doesn't make a difference, just like you wrote. – Daniel Beck Feb 10 '12 at 18:18
  • @DanielBeck Prior to 10.7, the Erase in Display (ED) escape sequence (ESC [ … J) could only clear the screen, not the scrollback. Starting in 10.7, Terminal supports the xterm ED variant that clears the scrollback: ESC [ 3 J invisible-island.net/xterm/ctlseqs/ctlseqs.html – Chris Page Feb 13 '12 at 16:11

From my own experience, when I disabled Lion's Resume feature, this stopped Terminal storing session history.

| improve this answer | |
  • 1
    It looks like this solves this problem. Thanks. – Mike L. Jul 26 '11 at 13:06

Lion's window resume feature is set on an application by application basis. You can stop this in Terminal with the following command:

defaults write com.apple.Terminal NSQuitAlwaysKeepsWindows -bool false

Because you're editing the Terminal settings while having Terminal open, you'll need to close and restart Terminal twice before seeing the effect.

| improve this answer | |
  • This should be the answer in my opinion. This sets the defaults for all of Mac OS X. – KVISH Jan 20 '15 at 18:25

Command-K clears the Scrollback.

| improve this answer | |
  • This is the simplest and best solution for just a permanent clear to a few windows, but not a permanent disable of the feature. – Simone Manganelli Dec 26 '11 at 9:06

I think what you want is not play with workarounds, but you want to know where is stored on disk the "grey text" you see in Terminal and remove it.

This text is stored here : ~/Library/Saved Application State/com.apple.Terminal.savedState.

Remove folder and you will be happy.

| improve this answer | |

One solution: export TERM with a reasonable value like "xterm", and place the clear command on the last line of your ~/.bash_profile. Other solutions could be devised.

| improve this answer | |
  • 2
    clear doesn't clear the Terminal, it only scrolls down a page. – Daniel Beck Jul 26 '11 at 9:26
  • @DanielBeck clear clears the terminal display. In some cases it does so by moving the screen contents into the scrollback (but not always, e.g., when the alternate screen is selected). – Chris Page Feb 10 '12 at 18:08
  • It's unnecessary to modify the value of TERM unless one has already modified it from the default. By default, Terminal sets TERM to xterm-color (versions prior to 10.7) or xterm-256color (Lion 10.7 and later), both of which support clear. – Chris Page Feb 10 '12 at 18:10
  • @ChrisPage How often is that the case when launching Terminal? – Daniel Beck Feb 10 '12 at 18:16
  • @DanielBeck I don't understand your question. Unless the user has customized the "Declare terminal as" preference setting to change the value that Terminal sets TERM to, it will be the default, xterm-256color (or xterm-color prior to 10.7). Terminal always sets TERM to the "Declare terminal as" value when creating a terminal session. – Chris Page Feb 13 '12 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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