# Inspired by: # - Re-size and Move with Escape Sequences, http://www.osxfaq.com/tips/unix-tricks/week99/monday.ws # - Define Aliases and Functions, http://www.osxfaq.com/tips/unix-tricks/week99/tuesday.ws # - Focus and Dock with Escape Sequences, http://www.osxfaq.com/Tips/unix-tricks/week99/wednesday.ws # Author: Adrian Mayo, http://101.1dot1.com and http://www.peachpit.com/title/0321374118 # also see: http://en.wikipedia.org/wiki/ANSI_escape_code # man bash | less -p PROMPTING # navigate by pressing n or N function title() { if [[ $# -eq 1 && -n "$@" ]]; then printf "\e]0;${@}\a"; fi; return 0; } # xterm control sequence: \e]0; ... \a function title() { if [[ -n "$@" ]]; then printf "\033]0;${1}\007"; fi; return 0; } function docktw() { printf "\e[2t"; return 0; } function docktw() { printf "\e[2t"; sleep 5; printf "\e[5t"; return 0; } function bgtw() { printf "\e[6t"; return 0; } function bgtw() { printf "\e[6t"; sleep 5; printf "\e[5t"; return 0; } # background the Terminal window title 'Hello, world!' docktw bgtw # positive integer test (including zero) function positive_int() { return $(test "$@" -eq "$@" > /dev/null 2>&1 && test "$@" -ge 0 > /dev/null 2>&1); } # move the Terminal window function mvtw() { if [[ $# -eq 2 ]] && $(positive_int "$1") && $(positive_int "$2"); then printf "\e[3;${1};${2};t" return 0 fi return 1 } # resize the Terminal window function sizetw() { if [[ $# -eq 2 ]] && $(positive_int "$1") && $(positive_int "$2"); then printf "\e[8;${1};${2};t" /usr/bin/clear return 0 fi return 1 } # full screen function fscreen() { printf "\e[3;0;0;t\e[8;0;0t"; /usr/bin/clear; return 0; } # default screen function dscreen() { printf "\e[8;35;150;t"; printf "\e[3;300;240;t"; /usr/bin/clear; return 0; } # max columns function maxc() { printf "\e[3;0;0;t\e[8;50;0t"; /usr/bin/clear; return 0; } # max rows function maxr() { printf "\e[3;0;0;t\e[8;0;100t"; /usr/bin/clear; return 0; } # show number of lines & columns function lc() { printf "lines: $(/usr/bin/tput lines)\ncolums: $(/usr/bin/tput cols)\n"; return 0; } # move cursor function mvc() { if [[ $# -eq 2 ]] && $(positive_int "$1") && $(positive_int "$2"); then /usr/bin/tput cup "$1" "$2" /usr/bin/tput el # clear to end of line #/usr/bin/tput ed return 0 fi return 1 } # wrap lines according to the number of columns of the Terminal window function wraptw() { declare str var=$(/usr/bin/tput cols) if [[ -s /dev/stdin ]]; then str="$(</dev/stdin)"; else str="$@"; fi printf "%s\n" "$str" | /usr/bin/fmt -w $var #printf "%s\n" "$str" | /usr/bin/fold -w $(/usr/bin/tput cols) return 0 } long_line="$(/usr/bin/jot -b 'This is a test sentence!' 100 | /usr/bin/tr '\n' ' ')" printf "%s\n" "$long_line" | wraptw lc mvtw 0 0 mvtw 0 300 mvtw 300 300 mvtw 80 140 fscreen dscreen maxc maxr lc sizetw 0 0 sizetw 0 50 sizetw 1 50 sizetw 50 50 sizetw 50 500 sizetw 50 150 mvc 2 45 mvc 100 23 mvc 99 3 dscreen #----------------------------------------------------- # customize the Terminal window's title bar text # show the current date as title bar text declare -x PROMPT_COMMAND='printf "\e]0; $(/bin/date)\a"' declare -x PROMPT_COMMAND='printf "\e]0;$(/bin/date "+%a %b %e %H:%M:%S ")\a"' declare -x PROMPT_COMMAND='printf "\e]0;$(/bin/date "+%Y-%m-%d %H:%M:%S ")\a"' # show current working directory declare -x PROMPT_COMMAND='printf "\e]0;$(pwd -P) \a"' declare -x PROMPT_COMMAND='printf "\e]0;$(pwd -P | /usr/bin/tr -d "[[:cntrl:]]") \a"' # show OLDPWD declare -x PROMPT_COMMAND='printf "\e]0;$(printf %s "$OLDPWD") \a"' declare -x PROMPT_COMMAND='printf "\e]0;$(printf %s "$OLDPWD" | /usr/bin/cut -c 1-100) \a"' export -p | grep -i PROMPT_COMMAND env | grep -i PROMPT_COMMAND unset -v PROMPT_COMMAND # first create environment variable TITLE # cf. Prompt Commands, http://www.osxfaq.com/Tips/unix-tricks/week71/wednesday.ws # Author: Adrian Mayo, http://101.1dot1.com and http://www.peachpit.com/title/0321374118 unset -v TITLE PROMPT_COMMAND declare -x TITLE="Terminal" declare -x PROMPT_COMMAND='printf "\e]0;${TITLE}\a"' # message as title bar text function message_title() { if [[ $# -eq 1 && -n "$@" ]]; then #TITLE="$@" TITLE="$(printf %s "$@" | /usr/bin/cut -c 1-100)" fi return 0 } export -f message_title export -p | grep -i message_title env | grep -i message_title #unset -f message_title message_title 'Hello, world!' # show OS information in title bar function osinfo() { x1="$(/usr/bin/sw_vers -productName)" x2="$(/usr/bin/sw_vers -productVersion)" x3="$(/usr/bin/sw_vers -buildVersion)" x4="$(/usr/bin/arch)" TITLE="${x1} - ${x2} - ${x3} - ${x4}" return 0 } export -f osinfo osinfo function eval_title() { if [[ -n "$(printf %s "$@") | /usr/bin/egrep '^date|whoami|logname|uname|machine|pwd|hostname$'" ]]; then cmd_output="$(eval "$@")" TITLE="$cmd_output" fi return 0 } export -f eval_title eval_title "date" eval_title "whoami" eval_title "logname" eval_title "uname" eval_title "machine" eval_title "pwd" eval_title "hostname" function date_title() { TITLE="$(/bin/date "+%Y-%m-%d")"; return 0; } export -f date_title date_title function logname_title() { TITLE="$(/usr/bin/logname)"; return 0; } export -f logname_title logname_title # show last command in title bar help fc unset -v TITLE PROMPT_COMMAND declare -x PROMPT_COMMAND='printf %b "\e]0;$(fc -ln | tail -n 1 | cut -c 2-150 | tr -d "\\")\a"' # show current command in title bar help history unset -v TITLE PROMPT_COMMAND declare -x PROMPT_COMMAND='printf %b "\e]0;$(history 1 | /usr/bin/cut -c 8-150 | tr -d "\\")\a"' declare -x PROMPT_COMMAND='printf %b "\e]0;$(history 1 | sed -E "s/^[[:space:]]*[[:digit:]]+[[:space:]]+(.*)$/\\1/" | cut -c 1-150 | tr -d "\\")\a"' #----------------------------------------------------- # increase font size function ifont() { /usr/bin/open -a Terminal /usr/bin/osascript <<__END__ repeat ${1:-1} times tell application "System Events" to tell process "Terminal" to keystroke "+" using command down end repeat __END__ return 0 } ifont ifont 10 # decrease font size function dfont() { /usr/bin/open -a Terminal /usr/bin/osascript <<__END__ repeat ${1:-1} times tell application "System Events" to tell process "Terminal" to keystroke "-" using command down end repeat __END__ return 0 } dfont dfont 10 # fullscreen window of Terminal.app # requires: # - http://www.culater.net/software/SIMBL/SIMBL.php # - http://ianhenderson.org/megazoomer.html function fullscreen() { /usr/bin/open -a Terminal /usr/bin/osascript <<__END__ tell application "System Events" to tell process "Terminal" to keystroke return using command down __END__ ifont 15 return 0 } # undo full-screen window of Terminal.app # requires: # - http://www.culater.net/software/SIMBL/SIMBL.php # - http://ianhenderson.org/megazoomer.html function nofullscreen() { /usr/bin/open -a Terminal dfont 15 /usr/bin/osascript <<__END__ tell application "System Events" to tell process "Terminal" to keystroke return using command down __END__ return 0 } fullscreen [F9] nofullscreen # hide all Terminal windows function hideall() { /usr/bin/osascript -e 'tell application "System Events" to set visible of some item of ( get processes whose name = "Terminal" ) to false' return 0 } hideall # create a new Terminal window and cd to the same current working directory # cf. http://justinfrench.com/index.php?id=231 unset -f newin function newin() { pwd -P | /usr/bin/pbcopy /usr/bin/open -a Terminal /usr/bin/osascript -e 'tell application "Terminal" to do script with command "cd \"$(/usr/bin/pbpaste)\"; echo \" \" | /usr/bin/pbcopy; /usr/bin/clear"' return 0 } cd ~/Desktop newin # create a new Terminal window and execute given command function newincmd() { declare args # escape single & double quotes args="${@//\'/\'}" args="${args//\"/\\\"}" printf "%s" "${args}" | /usr/bin/pbcopy #printf "%q" "${args}" | /usr/bin/pbcopy /usr/bin/open -a Terminal /usr/bin/osascript -e 'tell application "Terminal" to do script with command "/usr/bin/clear; eval \"$(/usr/bin/pbpaste)\""' return 0 } newincmd ls newincmd echo "hello \" world" newincmd echo $'hello \' world' # cd to the directory of the front Finder window # http://www.macosxhints.com/article.php?story=20060719155640762 function ff { osascript -e 'tell application "Finder"'\ -e "if (${1-1} <= (count Finder windows)) then"\ -e "get POSIX path of (target of window ${1-1} as alias)"\ -e 'else' -e 'get POSIX path of (desktop as alias)'\ -e 'end if' -e 'end tell'; }; function cdff { cd "`ff $@`"; } cdff
Further reading:
- Mac OS X Unix 101 Byte-Sized Projects
- preexec.bash -- Bash support for ZSH-like 'preexec' and 'precmd' functions
- "This bash shell is now fully operational!" (preexec.bash, screen, xterm)
- xterm titles with bash
- setting screen's title to the currently running process with bash
- Use Growl to monitor long-running shell commands (preexec.bash example)
- Set the title of the Terminal window with cd (using xterm escape sequences)
- Extending GNU Screen - Adding a taskbar
- Setting window and tab names in the Leopard Terminal
- How to change the title of an xterm: 4.3 bash
- Xterm Control Sequences
- list of xterm escape sequences
- programmatically changing the bash title
- Bash: How to change tab and window title of console
- MacVim without Distraction (full-screen mode)
- Hacking Without Distractions (MacVim in full-screen mode)
- Vi Input Manager
- Megazoomer