Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Clear the terminal screen & scrollback buffer

# make sure access for assistive devices is enabled in the "Universal Access" System Preference
open /System/Library/PreferencePanes/UniversalAccessPref.prefPane

# cf. Terminal -> Scrollback -> Clear Scrollback [cmd-k]
function clear_screen() {
   /usr/bin/open -a Terminal
   /usr/bin/osascript -e 'tell application "System Events" to tell process "Terminal" to keystroke "k" using command down'
   return 0
}

clear_screen


# you may use clear instead if scrollback is disabled in Terminal.app
defaults read com.apple.Terminal Scrollback
defaults write com.apple.Terminal Scrollback -string NO
#defaults write com.apple.Terminal Scrollback -string YES
kill -HUP $$
[cmd-n]
ls -l
clear

# in addition it may be useful to disable adding commands to the history list
#open -e ~/.bash_history
#set +o history   

Clear the entire current line in Bash

"\ed": kill-whole-line
#bind '"\M-d"':kill-whole-line

# Clear the entire current line by pressing the [esc-d] key sequence;
# in contrast to ctrl-u & ctrl-k the cursor position does not matter.
# cf. http://www.bigsmoke.us/readline/shortcuts

echo [esc-d] string   


#-------------------------


/bin/cat >> ~/.inputrc <<-'EOF'

"\ed": kill-whole-line
#"\M-d": kill-whole-line

EOF

# reinitialize ~/.inputrc
exec /bin/bash    
source ~/.bash_login ~/.bashrc

echo [esc-d] string

Clear form with Javascript

Purpose: Clear all text fields in a form

Other important features:
- Resulting page must be valid XHTML 1.1
- JS must work in Firefox 1.0 and MS Internet Explorer 6.0
- JS must never generate any errors, even non-fatal ones, in the Firefox Javascript Console
- JS should only clear text input (no hidden fields or visual controls)
- Code must be readable and properly indented

Looking around the web, this was not easy to find. After a few hours of searches, and some back'n'forth at Webmaster World, I got a working version.

In XHTML header:

<script type="text/javascript" src="clear-form-input.js"></script>

In html/form/div

<script type="text/javascript">
  // <![CDATA[
    var el = document.createElement("input");
    el.setAttribute("id", "clearButton");
    el.setAttribute("type", "button");
    el.setAttribute("value", "Clear");
    document.getElementById("deliciousFormDiv").appendChild(el);
    addEvent(el, 'click', function(){ clearForm('delicious');} );
  // ]]>
</script>

In clear-form-input.js:

function clearForm(formIdent) 
{ 
  var form, elements, i, elm; 
  form = document.getElementById 
    ? document.getElementById(formIdent) 
    : document.forms[formIdent]; 

	if (document.getElementsByTagName)
	{
		elements = form.getElementsByTagName('input');
		for( i=0, elm; elm=elements.item(i++); )
		{
			if (elm.getAttribute('type') == "text")
			{
				elm.value = '';
			}
		}
	}

	// Actually looking through more elements here
	// but the result is the same.
	else
	{
		elements = form.elements;
		for( i=0, elm; elm=elements[i++]; )
		{
			if (elm.type == "text")
			{
				elm.value ='';
			}
		}
	}
}
	
function addEvent(elm, strEvent, fnHandler)
{
	return ( elm.addEventListener
	? elm.addEventListener( strEvent, fnHandler, false)
	: elm.attachEvent( 'on'+strEvent, fnHandler)
	);
}