Tag Archives: bash - Page 2

More Useful Bash/Terminal Settings

A few more tricks to make your bash environment better. As always, add them to your ~/.profile or ~/.bash_profile to enable.

Disable the pagination of long lists when ambiguously tab completing.

bind 'set page-completions off'

Increase max returned items before being prompted. (ie, “Display all 380 possibilities? (y or n”). You can set the number to whatever you’d like.

bind 'set completion-query-items 300'

Show the list of autocompletion options after the first tab. This prevents the beep + second tab behavior.

bind 'set show-all-if-ambiguous on'

When autocompleting for cd or rmdir, list only directories as choices.

complete -d cd rmdir

Autocompletion for ssh known_hosts. Add this to your ~/.ssh/config (if the file doesn’t exist, create it)

Host *
HashKnownHosts no

Make grep highlight the matching terms in its output.

export GREP_OPTIONS='--color=auto'

Ignore case for case preserving but insensitive filesystems (like HFS+). I don’t personally use this, but perhaps some people will like it.

bind 'set completion-ignore-case on'

Don’t show hidden files when listing. Another option I don’t personally use.

bind 'set match-hidden-files off'

Improved Bash History

If you use multiple shells simultaneously (in my case with Terminal.app on OS X) you’ve undoubtedly noticed that the history of the last closed shell clobbers any commands you might have executed in others. This makes it difficult to use reverse-i-search to find commands you recall using. However, with a few modifications to your bash history you can greatly increase its utility.

export HISTCONTROL=erasedups
export HISTSIZE=10000
export HISTTIMEFORMAT="%D %T "
export HISTIGNORE="&:ls:exit"
shopt -s histappend

Save the above lines to your home directory’s .profile (or .bash_profile) and your shell history will now prevent duplicates, have a maximum of 10,000 items, append a timestamp to all new commands, exclude a list of commands, and append history between shells.

RAM Disks in OS X

If you’ve ever wanted to set up a ramdisk in Mac OS X, here’s a quick script you can use. Simply invoke it with a size in MB and the name for your volume and it will create and automount it for you. It also won’t leave any dangling directories in your /Volumes/ when you eject it! Be sure to chmod the file you place the script in to make it executable (755 or likewise).

#!/bin/bash
if [ -n "$3" ]; then NUM_ARG=ERR; fi
if [ -z "$1" ]; then NUM_ARG=ERR; fi
 
if [ -n "$NUM_ARG" ];
then
echo 'Usage: ./ramdisk <size in MB> <name, optional>'
exit
fi
SIZE=$1
NAME='my_ramdisk'
let "SIZE *= 2048"
if [ -n "$2" ]; then NAME=$2; fi
diskutil erasevolume HFS+ "$NAME" `hdiutil attach -nomount ram://${SIZE}`
echo 'Done'

Color Terminal in OS X

Apple ships OS X without ls set to use colors (and sans the useful ll alias). For those of us who have grown to love the instant delineation between directories, symlinks, and files this is an inconvenience. Luckily, adding this is quite simple. Place the following into your ~/.profile and you’re all set. To change this setting for all users edit /etc/bashrc as a superuser, but be aware that future OS X updates could potentially erase your modifications.

export CLICOLOR=1
alias ll='ls -l'

If you’d like to alter the ls defaults for coloration you can add one more line:

export LSCOLORS=ExFxCxDxBxegedabagacad

The incomprehensible value is a custom color setting defined by these rules.

For those who are new to the command line, ~/.profile means a file named “.profile” in your home directory. You can create the text file in TextEdit, save as plain text, then rename it to .profile, but be sure to move it into your home directory first as the Finder hides files prefixed with “.” by default.