How can I clear previous output in Terminal in Mac OS X? - macos

I know the clear command that 'clears' the current screen, but it does this just by printing lots of newlines - the cleared contents just get scrolled up.
Is there a way to completely wipe all previous output from the terminal so that I can't reach it even by scrolling up?

To clear the terminal manually:
⌘+K
Command+K for newer keyboards
To clear the terminal from within a shell script;
/usr/bin/osascript -e 'tell application "System Events" to tell process "Terminal" to keystroke "k" using command down'

A better way to clear the screen from within a script...
If you're using the OS X Terminal app (as stated by the OP), a better approach (thanks to Chris Page's answer to How do I reset the scrollback in the terminal via a shell command?) is just this:
clear && printf '\e[3J'
or more concisely (hat tip to user qiuyi):
printf '\33c\e[3J'
which clears the scrollback buffer as well as the screen. There are other options as well. See Chris Page's answer to How do I reset the scrollback in the terminal via a shell command? for more information.
Original answer
The AppleScript answer given in this thread works, but it has the nasty side effect of clearing any terminal window that happens to be active. This is surprising if you're running the script in one window and trying to get work done in another!
You avoid this by refining the AppleScript to only clear the screen if it is frontmost by doing this (taken from MattiSG's answer to How do I reset the scrollback in the terminal via a shell command?):
osascript -e 'if application "Terminal" is frontmost then tell application "System Events" to keystroke "k" using command down'
... but as when it's not the current window, the output will stack up until it becomes current again, which probably isn't what you want.

To delete the last output only:
⌘ + L
To clear the terminal completely:
⌘ + K

The pretty way is printf '\33c\e[3J'

Put this in your .bash_profile or .bashrc file:
function cls {
osascript -e 'tell application "System Events" to keystroke "k" using command down'
}

On Mac OS X Terminal, this functionality is already built in to the Terminal Application as menu View → Clear Scrollback (the default is CMD + K).
So you can re-assign this as you like with Apple's Keyboard shortcuts. Just add a new shortcut for Terminal with the command "Clear Scrollback". (I use CMD + L, because it's similar to Ctrl + L to clear the current screen contents, without clearing the buffer.)
I am not sure how you would use this in a script (maybe AppleScript as others have pointed out).

With Mac OS X v10.10 (Yosemite), use Option + Command + K to clear the scrollback in Terminal.app.

Or you can send a page break (ASCII form feed) by pressing Ctrl + L.
While this technically just starts a new page, this has the same net effect as all the other methods, while being a lot faster (except for the Apple + K solution, of course).
And because this is an ASCII control command, and it works in all shells.

Command + K will clear previous output.
To clear entered text, first jump left with Command + A and then clear the text to the right of the pointer with Control + K.
Visual examples:

Do the right thing; do the thing right!
Clear to previous mark: Command + L
Clear to previous bookmark: Option + Command + L
Clear to start: Command + K

clear && printf '\e[3J'
clears out everything, and it works well on OS X as well. Very neat.

I couldn't get any of the previous answers to work (on macOS).
A combination worked for me -
IO.write "\e[H\e[2J\e[3J"
This clears the buffer and the screen.

Adding the following to your configuration file would get you a new command to do it.
alias clearwipe='printf "\33c\e[3J"'
After reload clearwipe would be the new command to completely wipe all previous output from the terminal so that you can't reach it even by scrolling up.

Typing the following in the terminal will erase your history (meaning using up arrow will get you nothing), but it will not clear the screen:
history -c

CMD + K works for macOS. It clears the entire terminal output, but the environment remains.

Related

Mac: gnome-terminal equivalent for shell script

I am trying to run a shell script in a MAC terminal. I want to open a new terminal window that will execute the script separate from the program that is running. In Fedora, there is a gnome-terminal command which lets me execute a script in another terminal shell.
Does anyone know an equivalent on MAX OSX and how to use it?
For example say I have a script crazy.sh and I want to call this from a program that is executing but in a separate terminal from the one which is currently executing the program.
I like DigitalTrauma's answer but I found for my use, this worked better
open -a Terminal.app crazy.sh
Thanks for the answers.
One way to do it is to use an xterm instead of a terminal window:
xterm -e crazy.sh
If you want the xterm to stay open after the script completes, use the -hold option to xterm.
But if you really need to do this in a terminal, you can do it with applescript:
tell application "Terminal"
activate
tell application "System Events" to keystroke "n" using command down
repeat while contents of selected tab of window 1 starts with linefeed
delay 0.1
end repeat
do script "crazy.sh" in window 1 -- make sure the path to your script is right
end tell
(Credit to the answer here https://superuser.com/questions/466619/open-new-terminal-tab-and-execute-script)

Implicitly launch detached program (&) from terminal

I feel sure this will end up as a duplicate, but I don't know how to word it so that I can search for it...
I like to do my programming in emacs, which I launch from the terminal. When I use my mac I use aquamacs and the command
aquamacs Program.py
will launch aquamacs in a separate window ready to edit Program.py. When I work on my linux machine however to get the same result I must type
emacs Program.py &
And I'm always forgetting the "&". So 70% of the time I end up closing the emacs window, and relaunching again with the "&". Now I understand why that "&" is there, but is there a way to set up my system so that the command
emacs Program.py
always launches as a detached process? The only time I might not want that behavior is if I was SSHing in over a slow connection, in which case I usually use "-nw" anyway.
You can click on the terminal and press Ctrl-Z to move an already running foreground process to the background.
Alternatively you could add this function to your ~/.bashrc:
emacs() {
command emacs "$#" &
}
For the specific case of emacs, I use a shell script, which is more complicated than this but for your purposes boils down to
#!/bin/csh -f
/bin/emacs $*:q &
Put it in a directory which is earlier in your $path than where the "real" emacs is, and replace /bin/emacs with the real path to emacs on your system.

Mac OS X: Bring (non-bundle) GUI applications to foreground when launched from the command line

When a GUI process is launched from the OS X terminal, the window shows up in the background, and you have to use command-tab to give it focus.
Is there a way to make the terminal automatically give such GUIs focus after they are launched?
For example (assuming gitk is installed):
% gitk
should launch the GUI and then switch to it.
Note: For several reasons, using open as this answer suggests is not a general solution.
Update: To better explain why the open method isn't satisfactory, here's a sample bash session (with witty commentary).
% cd /my_repo
% gitk
Waiting for the GUI to appear ... any day now ... oh wait -- it's already open. I just didn't notice because it opened a window BEHIND my terminal. I wonder how long I was going to sit here waiting....
% open gitk
The file /my_repo/gitk does not exist.
Ah, of course.
% which gitk
/usr/bin/gitk
% open /usr/bin/gitk
What the ... it opened a new terminal window to run gitk, and it did so in my home directory, not /my_repo, so gitk complains that the current directory isn't actually a repository...
Do you need to invoke it synchronously? If not, you could start it asynchronously with & and then activate it with osascript -e 'tell application "gitk" to activate'.
If you are dealing with gitk specifically you can edit the gitk file to achieve this, I posted an answer on the apple stack exchange: https://apple.stackexchange.com/a/74917/35956
You can find the gitk file using the following command from the terminal
which gitk
In my gitk file I found a line that looks like the following near the top (line 3)
exec wish "$0" -- "$#"
I changed it to this
exec wish "$0" -- "$#" & exec osascript -e "tell application \"Wish\" to activate"
When I execute gitk from the command line the gitk window comes to the foreground, another side effect of this is that it executes asynchronously
You can wrap up #chris page's answer in a bash function and drop it in your .bashrc
function gitk() {
command gitk "$#"&
command osascript -e "delay .5" -e "tell application \"wish\" to activate"
}
There should be a way to get rid of the delay by looping and looking for 'wish' with a timeout.
NOTE: 'Wish' is the window title that shows up on my Mac for gitk.

Can I get terminal title? (or otherwise restore old one)

Setting terminal title is easy with echo -e "\e]0;some title\007". Works with pretty much every terminal program.
What I want is to set terminal title when some program starts - and restore old one when it finishes. Is this possible?
On xterm, the terminal control sequences 22 and 23 work fine, as in
#!/bin/sh
/bin/echo -ne '\033[22;0t' # Save title on stack
/bin/echo -ne "\033]0;$(date)\007"
sleep 1
/bin/echo -ne '\033[23;0t' # Restore title from stack
It looks like this isn't supported in the Mac OS X Terminal.App though.
There are some terminal programs that supporting it (xterm has compile time options for that, as mentioned by RWS), but most terminal programs simply lack such feature, including in particular Terminal.app.
Yes, that is possible indeed. See a xterm reference manual (like this for example) and wander your way through it. xterm even has a build in stack for this, so you don't have to store the title manually.
My solution was to set the window title during my script, then unset the window title when I completed. Unsetting the title reverted to the original value. Specifically, I did the following:
# Set the terminal title
printf "\e]2;%s\a" "running my script"
# Do whatever processing is required.
...
# Restore terminal title
printf "\e]2;\a"

Using keyboard to navigate the OS X terminal scrollback buffer

I hate using the mouse. When working in the OS X terminal, sometimes I want to navigate to a line in the bash shell a few rows up, copy a word or two. For this I always end up using the mouse. Any solution for this? Perhaps the terminal supports a key combination that puts it in to navigation/select/copy mode, where I can use the usual C-F, C-B, C-N and C-P keys.
If you feel comfortable with Vi key combinations, you can use this command to switch to vi-mode and use key strokes:
$ set -o vi
Or use C-xC-e to open current line in your $EDITOR.

Resources