Monday, September 19, 2011

More Useful and Interesting Bash Prompts

morebashprompts-smallA while back, we listed 8 Useful and Interesting Bash Prompts. That turned out to be one of our most popular articles, and still generates user response and feedback. This time we’ve found a few more gems, as well as some handy tips and tricks for having the coolest shell prompt on the proverbial block.

Using These Prompts

Some of the following prompts are one-liners, and can be pasted directly into your terminal for (temporary) use. To make them permanent, you’ll have to paste the code into your .bashrc or.bash_profile.

The larger, more complex prompts such as Twtty should be saved to their own script file (such as myprompt.sh), and you place a line like

source myprompt.sh

in your .basrc file to “import” them.


Twtty Prompt


This is a two-line prompt that holds quite a bit of information including username, hostname, workingdirectory, time, and command history number. Because of the technical and visual complexity, it’s best to place this prompt script in its own file, then source it from your .bashrc or .bash_profile as described above.


morebashprompts-twtty



function prompt_command {
  TERMWIDTH=${COLUMNS}
  # Calculate the width of the prompt:
  hostnam=$(echo -n $HOSTNAME | sed -e "s/[\.].*//")
# "whoami" and "pwd" include a trailing newline
usernam=$(whoami)
cur_tty=$(tty | sed -e "s/.*tty\(.*\)/\1/")
newPWD="${PWD}"
# Add all the accessories below ...
let promptsize=$(echo -n "--(${usernam}@${hostnam}:${cur_tty})---(${PWD})--" \
| wc -c | tr -d " ")
let fillsize=${TERMWIDTH}-${promptsize}
fill=""
while [ "$fillsize" -gt "0" ]
do
fill="${fill}-"
let fillsize=${fillsize}-1
done
  if [ "$fillsize" -lt "0" ]
then
let cut=3-${fillsize}
newPWD="...$(echo -n $PWD | sed -e "s/\(^.\{$cut\}\)\(.*\)/\2/")"
fi
}   PROMPT_COMMAND=prompt_command   function twtty {   local GRAY="
\[\033[1;30m\]"
local LIGHT_GRAY="
\[\033[0;37m\]"
local WHITE="
\[\033[1;37m\]"
local NO_COLOUR="
\[\033[0m\]"   local LIGHT_BLUE="\[\033[1;34m\]"
local YELLOW="
\[\033[1;33m\]"   case $TERM in
xterm*)
TITLEBAR='\[\033]0;\u@\h:\w\007\]'
;;
*)
TITLEBAR="
"
;;
esac   PS1="
$TITLEBAR\
$YELLOW-$LIGHT_BLUE-(\
$YELLOW\$usernam$LIGHT_BLUE@$YELLOW\$hostnam$LIGHT_BLUE:$WHITE\$cur_tty\
${LIGHT_BLUE})-${YELLOW}-\${fill}${LIGHT_BLUE}-(\
$YELLOW\${newPWD}\
$LIGHT_BLUE)-$YELLOW-\
\n\
$YELLOW-$LIGHT_BLUE-(\
$YELLOW\$(date +%H%M)$LIGHT_BLUE:$YELLOW\$(date \"+%a,%d %b %y\")\
$LIGHT_BLUE:$WHITE\$$LIGHT_BLUE)-\
$YELLOW-\
$NO_COLOUR "   PS2="$LIGHT_BLUE-$YELLOW-$YELLOW-$NO_COLOUR "
}

One important thing to note about this prompt is that the horizontal bar scales to fit the width of your terminal, and that the working directory line will truncate itself when it gets too large.


If you have any problems with the above code snippet, the plain text version can be downloaded here.


High-Performance Minimalist Prompt


All the complexities of a fancy prompt like Twtty do take up extra CPU cycles, and while it’s certainly not going to grind your system to a halt, it can add up. If you want something that packs a lot of info without slowing you down, I suggest this:


morebashprompts-minimal



PS1="[\d | \T -> \w ...\$?]\n#"

One interesting thing to note here is the use of the variable $? at the end. This signifies the exit status of the last command entered. 0 means all is well, any other number usually indicates an error.


Rob’s Prompt


This is one of the more popular prompts floating around the Internet. The code is small and simple, yet it gives many of the features found in the larger, more complex examples.


morebashprompts-robs



PS1="\[\033[0;33m\][\!]\`if [[ \$? = "0" ]]; then echo "\\[\\033[32m\\]"; else echo "\\[\\033[31m\\]"; fi\`[\u.\h: \`if [[ `pwd|wc -c|tr -d " "` > 18 ]]; then echo "\\W"; else echo "\\w"; fi\`]\$\[\033[0m\] "; echo -ne "\033]0;`hostname -s`:`pwd`\007"

MegaFancyPrompt


The name is entirely deserved – this prompt is quite large and extremely fancy. It’s got color, titlebar control, user-awareness, proxy detection, screen detection, job count, and more. In short, just about every feature found in the other prompts we’ve mentioned can be found here, and quite a few others. If you want it ALL in your prompt, this is it.


morebashprompts-megafancy


Note: Not all features are visible at all times, so while it may not look like much in the screenshot, you can check out the full (documented, commented) script file here for details on how to get the visible parts you want.



COLOR_WHITE='\033[1;37m'
COLOR_LIGHTGRAY='033[0;37m'
COLOR_GRAY='\033[1;30m'
COLOR_BLACK='\033[0;30m'
COLOR_RED='\033[0;31m'
COLOR_LIGHTRED='\033[1;31m'
COLOR_GREEN='\033[0;32m'
COLOR_LIGHTGREEN='\033[1;32m'
COLOR_BROWN='\033[0;33m'
COLOR_YELLOW='\033[1;33m'
COLOR_BLUE='\033[0;34m'
COLOR_LIGHTBLUE='\033[1;34m'
COLOR_PURPLE='\033[0;35m'
COLOR_PINK='\033[1;35m'
COLOR_CYAN='\033[0;36m'
COLOR_LIGHTCYAN='\033[1;36m'
COLOR_DEFAULT='\033[0m'
  # Function to set prompt_command to.
function promptcmd () {
history -a
local SSH_FLAG=0
local TTY=$(tty | awk -F/dev/ '{print $2}')
if [[ ${TTY} ]]; then
local SESS_SRC=$(who | grep "$TTY " | awk '{print $6 }')
fi
  # Titlebar
case ${TERM} in
xterm
* )
local TITLEBAR='\[\033]0;\u@\h: { \w } \007\]'
;;
* )
local TITLEBAR=''
;;
esac
PS1="${TITLEBAR}"
  # Test for day change.
if [ -z $DAY ] ; then
export DAY=$(date +%A)
else
local today=$(date +%A)
if [ "${DAY}" != "${today}" ]; then
PS1="${PS1}\n\[${COLOR_GREEN}\]Day changed to $(date '+%A, %d %B %Y').\n"
export DAY=$today
fi
fi
  # User
if [ ${UID} -eq 0 ] ; then
if [ "${USER}" == "${LOGNAME}" ]; then
if [[ ${SUDO_USER} ]]; then
PS1="${PS1}\[${COLOR_RED}\]\u"
else
PS1="${PS1}\[${COLOR_LIGHTRED}\]\u"
fi
else
PS1="${PS1}\[${COLOR_YELLOW}\]\u"
fi
else
if [ ${USER} == ${LOGNAME} ]; then
PS1="${PS1}\[${COLOR_GREEN}\]\u"
else
PS1="${PS1}\[${COLOR_BROWN}\]\u"
fi
fi
  # HTTP Proxy var configured or not
if [ -n "$http_proxy" ] ; then
PS1="${PS1}\[${COLOR_GREEN}\]@"
else
PS1="${PS1}\[${COLOR_LIGHTRED}\]@"
fi
  # Host
  if [[ ${SSH_CLIENT} ]] || [[ ${SSH2_CLIENT} ]]; then
SSH_FLAG=1
fi
if [ ${SSH_FLAG} -eq 1 ]; then
PS1="${PS1}\[${COLOR_CYAN}\]\h "
elif [[ -n ${SESS_SRC} ]]; then
if [ "${SESS_SRC}" == "(:0.0)" ]; then
PS1="${PS1}\[${COLOR_GREEN}\]\h "
else
local parent_process=`cat /proc/${PPID}/cmdline`
if [[ "$parent_process" == "in.rlogind*" ]]; then
PS1="${PS1}\[${COLOR_BROWN}\]\h "
elif [[ "$parent_process" == "in.telnetd*" ]]; then
PS1="${PS1}\[${COLOR_YELLOW}\]\h "
else
PS1="${PS1}\[${COLOR_LIGHTRED}\]\h "
fi
fi
elif [[ "${SESS_SRC}" == "" ]]; then
PS1="${PS1}\[${COLOR_GREEN}\]\h "
else
PS1="${PS1}\[${COLOR_RED}\]\h "
fi
  # Detached Screen Sessions
local DTCHSCRN=$(screen -ls | grep -c Detach )
if [ ${DTCHSCRN} -gt 2 ]; then
PS1="${PS1}\[${COLOR_RED}\][scr:${DTCHSCRN}] "
elif [ ${DTCHSCRN} -gt 0 ]; then
PS1="${PS1}\[${COLOR_YELLOW}\][scr:${DTCHSCRN}] "
fi
  # Backgrounded running jobs
local BKGJBS=$(jobs -r | wc -l )
if [ ${BKGJBS} -gt 2 ]; then
PS1="${PS1}\[${COLOR_RED}\][bg:${BKGJBS}]"
elif [ ${BKGJBS} -gt 0 ]; then
PS1="${PS1}\[${COLOR_YELLOW}\][bg:${BKGJBS}] "
fi
  # Stopped Jobs
local STPJBS=$(jobs -s | wc -l )
if [ ${STPJBS} -gt 2 ]; then
PS1="${PS1}\[${COLOR_RED}\][stp:${STPJBS}]"
elif [ ${STPJBS} -gt 0 ]; then
PS1="${PS1}\[${COLOR_YELLOW}\][stp:${STPJBS}] "
fi
  # Bracket {
if [ ${UID} -eq 0 ]; then
if [ "${USER}" == "${LOGNAME}" ]; then
if [[ ${SUDO_USER} ]]; then
PS1="${PS1}\[${COLOR_RED}\]"
else
PS1="${PS1}\[${COLOR_LIGHTRED}\]"
fi
else
PS1="${PS1}\[${COLOR_YELLOW}\]"
fi
else
if [ "${USER}" == "${LOGNAME}" ]; then
PS1="${PS1}\[${COLOR_GREEN}\]"
else
PS1="${PS1}\[${COLOR_BROWN}\]"
fi
fi
PS1="${PS1}{ "
  # Working directory
if [ -w "${PWD}" ]; then
PS1="${PS1}\[${COLOR_GREEN}\]$(prompt_workingdir)"
else
PS1="${PS1}\[${COLOR_RED}\]$(prompt_workingdir)"
fi
  # Closing bracket } and $\#
if [ ${UID} -eq 0 ]; then
if [ "${USER}" == "${LOGNAME}" ]; then
if [[ ${SUDO_USER} ]]; then
PS1="${PS1}\[${COLOR_RED}\]"
else
PS1="${PS1}\[${COLOR_LIGHTRED}\]"
fi
else
PS1="${PS1}\[${COLOR_YELLOW}\]"
fi
else
if [ "${USER}" == "${LOGNAME}" ]; then
PS1="${PS1}\[${COLOR_GREEN}\]"
else
PS1="${PS1}\[${COLOR_BROWN}\]"
fi
fi
PS1="${PS1} }\$\[${COLOR_DEFAULT}\] "
}   # Trim working dir to 1/4 the screen width
function prompt_workingdir () {
local pwdmaxlen=$(($COLUMNS/4))
local trunc_symbol="..."
if [[ $PWD == $HOME* ]]; then
newPWD="~${PWD#$HOME}"
else
newPWD=${PWD}
fi
if [ ${#newPWD} -gt $pwdmaxlen ]; then
local pwdoffset=$(( ${#newPWD} - $pwdmaxlen + 3 ))
newPWD="${trunc_symbol}${newPWD:$pwdoffset:$pwdmaxlen}"
fi
echo $newPWD
}
  # Determine what prompt to display:
# 1. Display simple custom prompt for shell sessions started
# by script.
# 2. Display "bland" prompt for shell sessions within emacs or
# xemacs.
# 3 Display promptcmd for all other cases.
  function load_prompt () {
# Get PIDs
local parent_process=$(cat /proc/$PPID/cmdline | cut -d \. -f 1)
local my_process=$(cat /proc/$$/cmdline | cut -d \. -f 1)
  if [[ $parent_process == script* ]]; then
PROMPT_COMMAND=""
PS1="\t - \# - \u@\H { \w }\$ "
elif [[ $parent_process == emacs* || $parent_process == xemacs* ]]; then
PROMPT_COMMAND=""
PS1="\u@\h { \w }\$ "
else
export DAY=$(date +%A)
PROMPT_COMMAND=promptcmd
fi
export PS1 PROMPT_COMMAND
}
  load_prompt

Bonus – Finding the Right Colors


As you may have noticed, most of these prompts use color, and the color is often represented in a coded form like ‘\033[1;30m’, which is quite difficult to read or remember. An easy way to see all your available colors, and preview what they’ll look like, is to simply paste the following snippet into your browser:



for i in `seq 1 7 ; seq 30 48 ; seq 90 107 ` ; do 
echo -e "\e[${i}mtest\e[0m$i"
done

Which will output each color code with its number.


morebashprompts-colortest


Conclusion


There are, of course infinite variations that could be packed into your prompt. Nearly any kind of textual data can be added in, and bash’s developers have had decades to add in functionality. Some readers have even commented about running custom Python or Ruby scripts from within the prompt. Whatever you do, make it functional.


via maketecheasier.com

Monday, September 12, 2011

Google Docs Tips For Beginners

google-doc-tips-introGoogle Docs is Google’s online document creation and management application. If you haven’t used Google Docs and you have a Google account, it is really worth checking out. It could easily save you from purchasing MS Office or upgrading.

Like many Google applications, Google Docs has been doing some testing behind the scenes. Being able to use and manage your documents well can save lots of time. Here are a few tips to help you be more versed with Google Docs.

Starting from scratch

If you are just starting with Google Docs, here are a few things you will want to start with. Note there are features you can add via web apps and extensions if you are using Google Chrome browser. The newest is offline support.

Collections

These are folders where you can organize your documents. I cannot stress enough how much easier it is to organize from the start. If you need to go back and sort through all of your documents and files, it will be a daunting task.

To create a collection, click on the Create button in the top left of the Google Docs homepage. In the drop down, you should see Collection as an option. Name the collection and you are ready to go.

google-doc-tips-create

By default, you will be taken into the new collection. If you want a sub-folder, this is a good time to make one. If you need to add a sub-folder, you can always do it later.

google-doc-tips-sub-folder

Importing

To move your current documents to the Google cloud, there are a couple of methods. If you have a lot of them to import to the cloud, you may want to consider an application like Gladinet. What Gladinet can do for you is upload all of the files from a certain folder or set of folders. You will need to sort them after they are all uploaded.

The other option is to upload them individually or by the folder. If you choose this method, you can import the files into collections you have already created.

google-doc-tips-upload

Spreadsheets

If you are an Excel user, this may be slightly different for you. However, many of the same features and settings will be in Google Docs. If you are a hard-core Excel user, you may not have everything you need. Also look at used for Google Forms. These export to a spreadsheet and stored in Google Docs.

Keyboard shortcuts

These are a few of the many keyboard shortcuts when you are using spreadsheets.

  • Show all formulas  Ctrl + ~
  • Make a copy  Ctrl + Up arrow + S
  • Add new sheet  Up arrow + F11
  • Select column  Up arrow + Space
  • Select row  Ctrl + Up arrow + Space
  • Strikethrough  Alt + Up arrow + 5
  • Redo  Ctrl + Y
  • Move to next sheet  Ctrl + Up arrow + PageDown

Conclusion

Google Docs is a fantastic replacement for most desktop If you an everyday user, you will do great. It is an easy transition from Microsoft Office. Having the flexibility to access your documents from anywhere with an internet connection including your phone, outweighs the small features that are lost.

What did you find useful when you first switched to Google Docs?

via maketecheasier.com

Saturday, September 3, 2011

How to Install Gnome 3 In Ubuntu

gnome3-logoEven though Gnome 3 (aka Gnome shell) is still in beta, it has sure garnered a lot of attention from both Linux enthusiasts and power users. Most of us who are not happy with Ubuntu unity tend to install Gnome 3 (or try outFedora) and see how it fares against the Unity desktop. If you are one of those who wanted to try out Gnome 3 on your Ubuntu, but don’t want to deal with the compilation of code, here’s how you can install it easily via a PPA.

Installing Gnome Shell in Ubuntu Natty 11.04

Open a terminal and type the following command:(only applicable for Ubuntu Natty 11.04.)

sudo add-apt-repository ppa:gnome3-team/gnome3
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install gnome-session gnome-shell

Restart your computer. At the login screen, select Gnome as the desktop session.


What will change


1. If you are using the Unity desktop, some of the stuff might break.


2. The font will change.


Additional software to install


If you plan to use Gnome Shell as your main desktop manager, the following applications will be useful for you.


1. Gnome Tweak Tool


To install, you can either click here or use the following command:



sudo apt-get install gnome-tweak-tool

gnome3-tweak-tool


2. Gnome-shell-frippery


This application is a Gnome 3 extension and it allows you to disable automatic workspace and several other tweaks.


To install, download the Gnome-shell-frippery tar file and extract it to the “~/.local/share/gnome-shell/extensions” folder.


Restart Gnome-shell.


3. Gnome Shell extensions


There are several other Gnome-shell extensions that you might be interested. You can open yourUbuntu Software Center (or synaptic Package Manager) and search for “gnome-shell-extension


gnome3-extensions


Uninstalling Gnome-shell


If you decided that Gnome 3 (or Gnome shell) is not for you, here’s the way to remove it from your system.



sudo apt-get install ppa-purge
sudo apt-get autoremove gnome-shell
sudo ppa-purge ppa:gnome3-team/gnome3

Conclusion


Gnome 3 is radically different from Gnome 2 and Unity. If you are using it for the first time, be prepared to unlearn what you know about Gnome and pick up from scratch again. There is a high probability that you will hate it the first time you use it, because it doesn’t come with most of the stuff that you are familiar with.