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

Handling control characters on the command line

# cf. http://en.wikipedia.org/wiki/Control_character and 
# http://ascii.cl/control-characters.htm

printf '\033'"\n" | cat -vet -
printf '\x0d'"\n" | cat -vet -

[ctrl-v][esc]
[ctrl-v][ctrl-m]

printf '[ctrl-v][ctrl-m]' | cat -vet -; echo


# cf. http://codesnippets.joyent.com/posts/show/1630
printf !:1 | sed -n -e 'l' 

# for an alternative to !:1 try [esc][ctrl][y]
printf !:1 | pbcopy    

# cf. man ascii
printf $(pbpaste) | od -A n -b
printf $(pbpaste) | od -A n -t xC
printf $(pbpaste) | od -A n -t dC

printf $(pbpaste) | od -A n -c
printf $(pbpaste) | od -A n -a

echo $'hel\rlo' 
echo $'hel\rlo' | sed s/[ctrl-v][ctrl-m]//
echo $'hel\rlo' | sed 's/[ctrl-v][ctrl-m]//'
echo $'hel\rlo' | sed "s/[ctrl-v][ctrl-m]//"
echo $'hel\rlo' | sed s/$'[ctrl-v][ctrl-m]'//


man infocmp
infocmp --help

infocmp -1 | grep '\^'
infocmp -1 | grep '\<k'
infocmp -L1 | grep '\^'
infocmp -I1 | grep '\^'
infocmp -C1 | grep '\^'


tput cup 0 0
tput cup 0 0 | cat -vet -; echo
tput cup 0 0 | sed -n -e 'l'
tput cup 0 0 | od -A n -c | sed '$,$d'
tput cup 0 0 | od -A n -a | sed '$,$d'
tput cup 0 0 | od -A n -b | sed '$,$d'
tput cup 0 0 | od -A n -t xC | sed '$,$d'

tput sgr0 | cat -vet -; echo
tput cuu | cat -vet -; echo
tput up | cat -vet -; echo
tput dl1 | cat -vet -; echo

tput kbs | wc -c
tput kbs | ruby -n -e 'p $_.to_s'


printf "%s\000\n" "str" | cat -vet -
printf "%s\000\n" "str" | od -A n -b | sed '$,$d'
printf "%s\000\n" "str" | sed -n -e 'l'
printf "%s\000\n" "str" | ruby -n -e 'p $_.to_s'

Handling file names with initial dash character

export PATH=/usr/bin:/bin:/usr/sbin:/sbin
export IFS=$' \t\n'

mkdir -p ~/Desktop/TestDir
touch ~/Desktop/TestDir/file{1,2,3,4,5}.txt
open ~/Desktop/TestDir

cd ~/Desktop/TestDir

touch -i

touch ~/Desktop/TestDir/-i

touch -- -i
chmod 000 -i
touch -- --i.txt
chmod 000 --i.txt

rm -f -i
rm -f -i -R *

rm -f -- -i
rm -f -- --i.txt

#rm -f ./-i
#rm -f ./--i.txt

rm -f -i -R *

flip - newline conversion tool

export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
export IFS=$' \t\n'

# download & install flip
cd ~/Desktop
curl -L -O http://ccrma-www.stanford.edu/~craig/utility/flip/flip.osx
mv ~/Desktop/flip.osx ~/Desktop/flip
/usr/bin/sudo /bin/cp -ip ~/Desktop/flip /usr/bin
/usr/bin/sudo /usr/sbin/chown root:wheel /usr/bin/flip
/usr/bin/sudo /bin/chmod +x /usr/bin/flip
ls -l /usr/bin/flip

flip
flip -t /path/to/file     # display current file type
flip -u /path/to/file     # convert to Unix: "\n"
flip -d /path/to/file     # convert to MS-DOS/Windows: "\r\n"
flip -m /path/to/file     # convert to Macintosh (OS 9): "\r"


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


# create a test file

testfile="${HOME}/Desktop/testfile.txt"

function createfile() {
   testfile="${HOME}/Desktop/testfile.txt"
   /usr/bin/jot -b 'sample text' 10 | /bin/cat -n > "$testfile"
   # append a line with a '\r\n' line separator
   printf "%s\r\n\r\n\r\r\n" "sample text" >> "$testfile"      
   # append a last line without a terminating '\n'
   printf "%s" "sample text" >> "$testfile"          
   return 0
}


function odcfile() {

/usr/bin/od -A n -c < "$@" | /usr/bin/sed -E -e 's/^[[:space:]]{11}//' \
       -e s/[[:space:]]{4}/$'\001'/g \
       -e 's/[[:space:]]+//g' | \
       /usr/bin/tr -d '\n' | /usr/bin/tr '\001' ' ' | \
       /usr/bin/sed -e s/\\\\n/$'\\\\\\n\\\n'/g 

return 0
}


createfile
odcfile "$testfile"
flip -t "$testfile"     # display current file type

flip -d "$testfile"     # convert to MS-DOS/Windows: "\r\n"
flip -t "$testfile"
odcfile "$testfile"

flip -m "$testfile"     # convert to Macintosh (OS 9): "\r"
flip -t "$testfile"
odcfile "$testfile"

flip -u "$testfile"     # convert to Unix: "\n"
flip -t "$testfile"
odcfile "$testfile"

Delete carriage returns & newlines with sed

# delete newlines

printf "a\nb\nc\nd\ne\nf" | sed -n -e 'l'
printf "a\nb\nc\nd\ne\nf" | sed -E -e :a -e '$!N; s/\n//g; ta'

printf "a\n\nb" | sed -n -e 'l'
printf "a\n\nb" | sed -E -e :a -e '$!N; s/\n//g; ta' | sed -n -e 'l'
printf "a\n\nb" | sed -E -e :a -e '$!N; s/\n$//g; ta' | sed -n -e 'l'
printf "a\n\n\n\n\n\nb" | sed -E -e :a -e '$!N; s/\n$//g; ta' | sed -n -e 'l'


# delete carriage returns

printf "he\r\rllo\n" | sed -n -e 'l'
printf "he\r\rllo\n" | sed -e s/$'\r'//g | sed -n -e 'l'
printf "he\r\rllo\n" | sed -e s/$'\r\r'/$'\r'/g | sed -n -e 'l'

CR=$'\r'
printf "hello\r\r\n" | sed "s/$CR$CR$/$CR/g" | sed -n -e 'l'


# alternatives

printf "a\nb\nc\nd\ne\nf" | tr -d '\n'

# cf. http://linux.dsplabs.com.au/rmnl-remove-new-line-characters-tr-awk-perl-sed-c-cpp-bash-python-xargs-ghc-ghci-haskell-sam-ssam-p65/
while read -d $'\n'; do echo -n "${REPLY} "; done < <((printf "a\nb\nc\nd\ne\nf"))
while read -d $'\n'; do printf "%s   " "${REPLY}"; done < <((printf "a\nb\nc\nd\ne\nf"))

xargs echo < <((printf "a\nb\nc\nd\ne\nf"))
xargs printf "%s " < <((printf "a\nb\nc\nd\ne\nf"))

printf "a\nb\nc\nd\ne\nf" | /usr/bin/paste -s -d ' ' -     # cf. man paste

# delete newlines in-place (also see below)
vim -e -s +':%j' +'w!' +'qa!' /path/to/file

# insert newlines again
printf "%s\n" "hello" | sed -e s/l/$'\\\n'/g | sed -n -e 'l'
printf "%s\n" "hello" | sed -E s/\(l\)/\\1$'\\\n'/g | sed -n -e 'l'


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


# converting between \n and \n\r

# cf. also flip, http://ccrma.stanford.edu/~craig/utility/flip/ and 
# http://codesnippets.joyent.com/posts/show/1660


# convert \n into \r\n
printf "a\nb\nc\nd\ne\nf\n"
printf "a\nb\nc\nd\ne\nf\n" | sed -n -e 'l'

printf "a\nb\nc\nd\ne\nf\n" | sed s/$/$'\r'/
printf "a\nb\nc\nd\ne\nf\n" | sed s/$/$'\r'/ | sed -n -e 'l'

printf "a\nb\n\n\n\n\n\n\n\n\n\n\nc\nd\ne\nf\n" | sed -E -e :a -e '$!N; s/\n$//g; ta' | sed -E s/$/$'\r'/ | sed -n -e 'l'

while read -d $'\n'; do printf "%s\r\n" "${REPLY}"; done < <((printf "a\nb\nc\nd\ne\nf\n"))
while read -d $'\n'; do printf "%s\r\n" "${REPLY}"; done < <((printf "a\nb\nc\nd\ne\nf\n")) | sed -n -e 'l'


# convert \r\n into \n
printf "a\r\nb\r\nc\r\nd\r\ne\r\nf\r\n"
printf "a\r\nb\r\nc\r\nd\r\ne\r\nf\r\n" | sed -n -e 'l'

printf "a\r\nb\r\nc\r\nd\r\ne\r\nf\r\n" | sed s/$'\r'$//
printf "a\r\nb\r\nc\r\nd\r\ne\r\nf\r\n" | sed s/$'\r'$// | sed -n -e 'l'

printf "a\r\nb\r\r\r\r\r\r\r\r\r\r\nc\r\nd\r\ne\r\nf\r\n" | sed s/$'\r'*$// | sed -n -e 'l'
printf "a\r\nb\r\r\r\r\r\r\r\r\r\r\nc\r\nd\r\ne\r\nf\r\n" | sed -E s/$'\r'+$// | sed -n -e 'l'

# lacks a terminating \n though
while read -d $'\r'; do printf "%s" "${REPLY}"; done < <((printf "a\r\nb\r\nc\r\nd\r\ne\r\nf\r\n"))
while read -d $'\r'; do printf "%s" "${REPLY}"; done < <((printf "a\r\nb\r\nc\r\nd\r\ne\r\nf\r\n")) | sed -n -e 'l'


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


# create test files

testfile="${HOME}/Desktop/testfile.txt"
output="${HOME}/Desktop/output.txt"

function createfiles() {

testfile="${HOME}/Desktop/testfile.txt"
output="${HOME}/Desktop/output.txt"

/usr/bin/touch "$output"

#/usr/bin/jot -b 'sample text' 10 | /bin/cat > "$testfile"
/usr/bin/jot -b 'sample text' 10 | /bin/cat -n > "$testfile"
printf "%s\r\n\r\n\r\r\n" "sample text" >> "$testfile"      # append a line with a '\r\n' line separator
printf "%s" "sample text" >> "$testfile"          # append a last line without a terminating '\n'

return 0
}

createfiles


# inspect test file

cat -vet "$testfile"
ed -s "$testfile" <<< $',l'
sed -n -e 'l'  "$testfile"
ruby -n -e 'p $_.to_s' < "$testfile"


function odcfile() {

/usr/bin/od -A n -c < "$@" | /usr/bin/sed -E -e 's/^[[:space:]]{11}//' \
       -e s/[[:space:]]{4}/$'\001'/g \
       -e 's/[[:space:]]+//g' | \
       /usr/bin/tr -d '\n' | /usr/bin/tr '\001' ' ' | \
       /usr/bin/sed -e s/\\\\n/$'\\\\\\n\\\n'/g 

return 0
}


odcfile "$testfile"


# remove newlines "\n"

# create a new file with newline characters replaced with a space
sed -e :a -e '$!N; s/\n/ /g; ta' "$testfile" > "$output"
odcfile "$output"

# same, but in-place
sed -i "" -e :a -e '$!N; s/\n/ /g; ta' "$testfile"
odcfile "$testfile"

createfiles

# delete newlines in-place
sed -i "" -e :a -e '$!N; s/\n//g; ta' "$testfile"
odcfile "$testfile"

createfiles

# alternative for creating a new file without newlines
tr -d '\n' < "$testfile" > "$output"
odcfile "$output"



# delete carriage returns "\r"

# create a new file with carriage returns deleted
sed -e s/$'\r'//g "$testfile" > "$output"
sed -e s/$'\r'$//g "$testfile" > "$output"  # only at line end
odcfile "$output"

# alternative
tr -d '\r' < "$testfile" > "$output"
odcfile "$output"


# delete carriage returns in-place

sed -i "" -e s/$'\r'//g "$testfile" > "$output"
odcfile "$testfile"

createfiles

# cf. http://bash-hackers.org/wiki/doku.php?id=howto:edit-ed (Pitfalls)
ed -s "$testfile" <<< $'H\n,g/\r/s/\r//\n,w'      # delete one carriage return in a line
ed -s "$testfile" <<< $'H\n,g/\r/s/\r//g\n,w'     # delete all carriage returns in a line
ed -s "$testfile" <<< $'H\n,g/\r$/s/\r$//g\n,w'   # delete a carriage return at line end

odcfile "$testfile"



printf "\n" | od -A n -c
printf "\012" | od -A n -c
printf "\x0a" | od -A n -c

printf '[ctrl-v][ctrl-j]' | od -A n -c      #  \n or ^J


printf "\r" | od -A n -c
printf "\015" | od -A n -c
printf "\x0d" | od -A n -c

printf '[ctrl-v][ctrl-j]' | od -A n -c      #  \r or ^M


# vim
createfiles

vim "$testfile"
#...
:set number
:set list
:%s/^M//g
:set fileformat=unix
#:set fileformat=dos
:x

odcfile "$testfile"


# edit files in-place with vim

createfiles

# delete newlines in-place
vim -e -s +':%j' +'w!' +'qa!' "$testfile"
odcfile "$output"

createfiles

# replace every \r with \n
# cf. http://vim.wikia.com/wiki/Change_end-of-line_format_for_dos-mac-unix
vim -e -s +':%s/\r/\r/g' +':set fileformat=unix' +'w!' +'qa!' "$testfile"
odcfile "$output"

createfiles

# delete every \r
vim -e -s +':%s/\r//g' +':set fileformat=unix' +'w!' +'qa!' "$testfile"
odcfile "$output"

createfiles

# delete \r only when it occurs at the end of a line
vim -e -s +':%s/\r$//g' +':set fileformat=unix' +'w!' +'qa!' "$testfile"
odcfile "$output"

createfiles

# replace carriage return line endings with \n
# cf. Getting rid of ^M - mixing dos and unix, http://www.vim.org/tips/tip.php?tip_id=26
printf "\r" >> "$testfile"
vim -e -s +':g/\r$/s///g' +':set fileformat=unix' +'w!' +'qa!' "$testfile"
odcfile "$testfile"

createfiles

# delete last \n of file
vim -e -s +':set noeol bin' +':set fileformat=unix' +'w!' +'qa!' "$testfile"
odcfile "$output"

createfiles

# change mixed mode files to DOS mode
#vim -e -s +':%s/\r\r/\r/g' +'w!' +'qa!' "$testfile"
sed -i '' -e s/$'\r\r'/$'\r'/g  "$testfile"
vim -e -s +':e ++ff=dos' +'w!' +'qa!' "$testfile"
vim -e -s +':e ++ff=dos' +':set ff=dos' +'w!' +'qa!' "$testfile"
odcfile "$output"

vim "$testfile"
:set ff?
:x

open or cd to the directory of a file path

# open directory of a file path
# can be used together with the [esc-.] key sequence
function odf() { /usr/bin/open "$(/usr/bin/dirname "$@")"; return 0; }

# cd to the directory of a file path
function cdf() { cd "$(/usr/bin/dirname "$@")"; return 0; }


ls -l /Library/Desktop\ Pictures/Nature/Tranquil\ Surface.jpg
ls -l /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ExecutableBinaryIcon.icns 

odf [esc-.]
cdf [esc-.]


odf /System/Library/SystemProfiler/SPFirewallReporter.spreporter/Contents/Resources/
cdf /System/Library/SystemProfiler/SPFirewallReporter.spreporter/Contents/Resources/English.lproj/Localizable.strings

Two shortcuts for displaying system_profiler data types

man system_profiler

function sptypes() {
   /usr/sbin/system_profiler -listDataTypes | /usr/bin/sed '1d' | /usr/bin/sort | /usr/bin/nl
   return 0
}

unset -f sptype
function sptype() {
   declare -i num
   declare sptypename
   num=$1
   #sptypename="$(/usr/sbin/system_profiler -listDataTypes | /usr/bin/sed '1d'  | /usr/bin/sort | /usr/bin/nl | /usr/bin/egrep "^[[:space:]]+${num}[[:space:]]" | /usr/bin/awk '{print $NF}')"
   sptypename="$(/usr/sbin/system_profiler -listDataTypes | /usr/bin/sed '1d' | /usr/bin/sort | /usr/bin/nl | /usr/bin/grep -w "${num}" | /usr/bin/awk '{print $NF}')"
   printf "\n\e[1m%s\e[m\n\n" "${sptypename}:"
   system_profiler "${sptypename}"
   return 0
}


sptypes
sptype 5

sptypes | grep -i pref
sptype 26 | less -R


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


# interactive version
unset -f sptype
function sptype() {
   declare -a array
   declare -i num
   array=($(/usr/sbin/system_profiler -listDataTypes | /usr/bin/sed '1d' | /usr/bin/tr '\n' ' '))
   echo
   printf "%s\n" "${array[@]}" | /usr/bin/nl
   echo
   printf "\n\e[1m%s\e[m" "Please select a number:  "
   read num
   num=$num-1
   echo
   printf "\n\e[1m%s\e[m\n\n" "${array[${num}]}: "
   system_profiler $(echo "${array[${num}]}")
   #system_profiler $(echo "${array[${num}]}") | less -R
   return 0
}

sptype

Automatically resize Terminal window

For more information see Terminal window commands, Discover tput and Apple Shell Scripting Primer -> ANSI Escape Sequence Tables.

tput lines
tput cols

echo $LINES
echo $COLUMNS

stty size
stty size | awk '{print $1}'    # lines
stty size | awk '{print $NF}'   # columns

stty size | cut -d" " -f1   # lines
stty size | cut -d" " -f2   # columns 

stty -a | awk '/rows/ {print $4}'      # lines
stty -a | awk '/columns/ {print $6}'   # columns

stty -a | sed -E -n -e 's/^.*[^[:digit:]]([[:digit:]]+)[[:space:]]+rows;.*$/\1/p;q;'
stty -a | sed -E -n -e 's/^.*[^[:digit:]]([[:digit:]]+)[[:space:]]+columns;.*$/\1/p;q;'



# automatically resize the Terminal window if it gets smaller than the default size

# positive integer test (including zero)
function positive_int() { return $(test "$@" -eq "$@" > /dev/null 2>&1 && test "$@" -ge 0 > /dev/null 2>&1); }


# resize the Terminal window
function sizetw() { 
   if [[ $# -eq 2 ]] && $(positive_int "$1") && $(positive_int "$2"); then 
      printf "\e[8;${1};${2};t"
      return 0
   fi
   return 1
}


# the default Terminal window size: 26 lines and 107 columns
sizetw 26 107


# automatically adjust Terminal window size
function defaultwindow() {

   DEFAULTLINES=26
   DEFAULTCOLUMNS=107

   if [[ $(/usr/bin/tput lines) -lt $DEFAULTLINES ]] && [[ $(/usr/bin/tput cols) -lt $DEFAULTCOLUMNS ]]; then
      sizetw $DEFAULTLINES $DEFAULTCOLUMNS
   elif [[ $(/usr/bin/tput lines) -lt $DEFAULTLINES ]]; then
      sizetw $DEFAULTLINES $(/usr/bin/tput cols)
   elif [[ $(/usr/bin/tput cols) -lt $DEFAULTCOLUMNS ]]; then
      sizetw $(/usr/bin/tput lines) $DEFAULTCOLUMNS
   fi

   return 0
}


# SIGWINCH is the window change signal
trap defaultwindow SIGWINCH    


sizetw 26 70
sizetw 10 107
sizetw 4 15

Clear the entire current line in Bash

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'

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

EOF

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

echo [esc-d] string

Changing the Finder "Open With" contextual menu from the command line

The command line tool duti (http://duti.sourceforge.net) can be used to change default file-application launching associations as shown by the Finder "Open With" contextual menu. Changing the Finder "Open With" contextual menu means changing the default way we "open" documents on the command line as well.

The "lsregister" command can be used to dump or rebuild the Launch Services database.

export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
export IFS=$' \t\n'


# download & install duti, MacVim and TextMate

# duti - set default UTI handlers
# cf. http://duti.sourceforge.net/documentation.php and
# http://developer.apple.com/macosx/uniformtypeidentifiers.html

cd ~/Desktop
curl -L -O http://downloads.sourceforge.net/duti/duti-1.3.0.pkg.tar.gz
tar -xzf duti-1.3.0.pkg.tar.gz
open -a Installer duti-1.3.0.pkg
stat -x /usr/local/bin/duti
duti -h
man duti


# MacVim, http://code.google.com/p/macvim/

cd ~/Desktop
curl -L -O http://macvim.googlecode.com/files/MacVim-7_2-stable-1_2.tbz
tar -xjf MacVim-7_2-stable-1_2.tbz
#open MacVim-7_2-stable-1_2.tbz    # uses BOMArchiveHelper (10.4) or Archive Utility (10.5) respectively
mv -i MacVim-7_2-stable-1_2 MacVim
mv -i ~/Desktop/MacVim /Applications


# TextMate, http://macromates.com

cd ~/Desktop
curl -L -O http://macromates.com/textmate/files/TextMate_1.5.7.dmg
hdiutil mount ~/Desktop/TextMate_1.5.7.dmg
cp -Ri '/Volumes/TextMate 1.5.7/TextMate.app' /Applications
hdiutil unmount '/Volumes/TextMate 1.5.7'
#/usr/bin/sudo /bin/ln -s /Applications/TextMate.app/Contents/Resources/mate /usr/local/bin/mate


# get the path to the "lsregister" command
locate lsregister | head -n 1
find /System/Library/Frameworks -type f -name lsregister -ls
find /System/Library/Frameworks/CoreServices.framework -type f -name lsregister -ls

alias lsregister="/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister"

#/usr/bin/sudo /bin/ln -s `locate lsregister | head -n 1` /bin/lsregister   # alternative

# rebuild Launch Services database
#alias rlsdb='lsregister -kill -r -f -all system,local,user'    # Mac OS X 10.5; cf. http://9stmaryrd.com/2008/05/16/158
alias rlsdb='lsregister -kill -r -f -domain local -domain system -domain user'

rlsdb

lsregister -h

lsregister -dump | grep -i duti
lsregister -dump | grep -i MacVim
lsregister -dump | grep -i TextMate
lsregister -dump | grep -i TextEdit
lsregister -dump | grep -i Safari
lsregister -dump | grep -i helpviewer


lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(.*)$/  \1/p' | sort | uniq | nl
lsregister -dump | grep '[[:space:]]uti:' | awk '{ print $2 }' | sort | uniq | nl
lsregister -dump | sed -E -n -e 's/^.*identifier:[[:space:]]+([^[:space:]].*)$/  \1/p' | sort | uniq | nl
lsregister -dump | sed -E -n -e 's/^.*path:[[:space:]]+([^[:space:]].*\.app)$/  \1/p' | sort | uniq | nl


lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(.*)$/  \1/p' | nl | egrep -i 'duti|MacVim|TextMate|TextEdit|Safari|helpviewer'
lsregister -dump | sed -E -n -e 's/^.*identifier:[[:space:]]+([^[:space:]].*)$/  \1/p' | nl | egrep -i 'duti|MacVim|TextMate|TextEdit|Safari|helpviewer'
lsregister -dump | sed -E -n -e 's/^.*path:[[:space:]]+([^[:space:]].*\.app)$/  \1/p' | nl | egrep -i 'duti|MacVim|TextMate|TextEdit|Safari|helpviewer'


lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(public\..*)$/  \1/p' | sort | uniq | nl
lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(.*text.*)$/  \1/p' | sort | uniq | nl


alias utis="lsregister -dump | grep '[[:space:]]uti:' | awk '{ print \$2 }' | sort | uniq"
alias utis="lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(.*)$/\1/p' | sort | uniq"
utis

utis | grep -i 'public\.' | nl

defaults read com.apple.LaunchServices


# move & copy the same file
unset -f mc
function mc() {
   if [[ $# -gt 1 ]] || [[ ! -e "$@" ]]; then return 1; fi
   tmpfile="${@}.tmp-${RANDOM}"
   printf "%s\n" "$tmpfile"
   /bin/mv "$@" "${tmpfile}"
   /bin/sleep 0.2
   /bin/cp -p "${tmpfile}" "$@"
   /bin/rm "${tmpfile}"
   return 0
}


unset -f mc
function mc() {
   if [[ $# -gt 1 ]] || [[ ! -e "$@" ]]; then return 1; fi
   tmpfile="/tmp/tmpfile.tmp-${RANDOM}"
   printf "%s\n" "$tmpfile"
   /bin/mv "$@" "${tmpfile}"
   /bin/sleep 0.2
   /bin/cp -Rp "${tmpfile}" "$@"
   /bin/rm -Rf "${tmpfile}"
   return 0
}



echo hello world > ~/Desktop/test.txt
echo hello world > ~/Desktop/test.html


# switch between Safari & Help Viewer as the default launching app for html files
open ~/Desktop/test.html


# cf. http://duti.sourceforge.net/documentation.php

printf "%s\n" 'com.apple.helpviewer   public.html   all' | duti     
mc ~/Desktop/test.html
open ~/Desktop/test.html

printf "%s\n" 'com.apple.Safari   public.html   all' | duti
mc ~/Desktop/test.html
open ~/Desktop/test.html

rlsdb


# switch between TextEdit, MacVim & TextMate as the default launching app for plain text files

open ~/Desktop/test.txt

printf "%s\n" 'com.macromates.textmate   public.plain-text   all' | duti
open ~/Desktop/test.txt
mc ~/Desktop/test.txt

printf "%s\n" 'org.vim.MacVim   public.plain-text   all' | duti
open ~/Desktop/test.txt    # type: ":q" or ":x" to quit test.txt
mc ~/Desktop/test.txt

printf "%s\n" 'com.apple.TextEdit   public.plain-text   all' | duti
open ~/Desktop/test.txt
mc ~/Desktop/test.txt



# cf. http://duti.sourceforge.net/documentation.php

/bin/cat > ~/Desktop/test.duti <<-'EOF'
com.apple.helpviewer   public.html   all
com.macromates.textmate   public.plain-text   all
EOF

# remove white space at line beginning
ed -s ~/Desktop/test.duti <<< $',s/^[[:space:]]*//\nw'

open -e ~/Desktop/test.duti

duti -v ~/Desktop/test.duti

mc ~/Desktop/test.html
open ~/Desktop/test.html

mc ~/Desktop/test.txt
open ~/Desktop/test.txt


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


# GUI alternative to switch the default launching app for a single file
[right-click on a file] + [alt]  ->  "Always Open With"

# GUI alternative to change the default launching app for all files of the same kind
[right-click on a file] -> "Get Info" -> "Open with:" -> [select an app] -> "Change All ..."

kv - launch Keyboard Viewer

locate -i *keyboardviewerserver

alias kv='/usr/bin/open /System/Library/Components/KeyboardViewer.component/Contents/SharedSupport/KeyboardViewerServer.app'

kv


# cf. Accented letters and other symbols on the Mac,
# http://mac4translators.blogspot.com/2008/08/accented-letters-and-other-symbols-on.html

[cmd]     #  hold the command key
[alt]
[alt][shift]