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!)

Simple command line editor with Readline support

# for alternatives see http://codesnippets.joyent.com/posts/show/1625
unset -f edit

function edit() {
   declare -a ar
   declare -i i=-1
   while read -e -d $'\n' line; do let i++; ar[$i]="${line}"$'\n'; done   # -e enables readline
   printf "%s\n" "${ar[@]}" >> ~/.bash_history
   #printf "%s\n" "${ar[@]}" | /usr/bin/tr '\n' ' '  >> ~/.bash_history
   #printf "%s\n"  >> ~/.bash_history
   history -n
   eval "${ar[@]}"
   return 0
}

edit
echo "!";
[ctrl-r]
...
[ctrl-d]

edit
if [[ 5 -gt 3 ]]; then
   echo 12345
fi
[ctrl-d]

[ctrl-d]  # quit and execute the command
[ctrl-c]  # do not execute the command and abort

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

Python - Processing Large Text Files One Line At A Time

// process some very large text files one line at a time

fh = open('foo.txt', 'r')
line = fh.readline()
while line:
    # do something here
    line = fh.readline()


// Update

for line in open('foo.txt', 'r'):
    # do something here