Customizing Your Unix Environment
Sooner or later, you’re going to decide that you don’t like some of the default behaviors of your favorite Unix programs. When that happens, you will be pleased to learn that there’s are great deal you can do to modify your Unix environment.
1 Startup Customizations
When you launch a new shell (e.g., whenever you log in, or spawn off a new xterm
), the shell starts by executing the commands in a set of files within your home directory. This gives you an opportunity to customize your Unix environment.
Prior to late summer of 2016, new student accounts were set up, by default, to use the command shell tcsh
. Since then, new student accounts have been set up with bash
as the default shell. To see which one your account is using, run the command
echo $SHELL
1.1 Customizing bash
When a new bash
shell is started by a command that logs the user in, it reads start-up instructions from a file ~/.bash_profile
before allowing the user to type.
If a new bash
shell is started by an already logged-in user, it reads start-up instructions from ~/.bashrc
instead.
Most people put instructions that they alweays want performed in ~/.bashrc
, and have their ~/.bash_profile
load that after it has done its job. Many people accumulate a large number of “alias” commands, and these are usually kept in a separate file, ~/.bash_aliases
. (Note that all of these files start with a ‘.’ in their name, so they are “hidden” from an ordinary ls
command unless you give the -a
option.)
1.1.1 .bash_profile
A typical .bash_profile
:
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
Actually, all that this does is to run my .bashrc
file. So, whether I am logging in for the first time or not, my .bashrc
file will be executed.
1.1.2 .bashrc
Here is a simplified version of my .bashrc
file:
# ~/.bashrc: executed by bash(1) for non-login shells.
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# don't put duplicate lines in the history.
HISTCONTROL=ignoredups:ignorespace
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# Display the last two directories of my current path in the command prompt.
export PROMPT_DIRTRIM=2
PS1='\u@\h:\w\$ '
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"'
;;
*)
;;
esac
# Set my default editor
EDITOR=emacs
# Default permissions when I, create a new directory are 700,
# when I create a new file, 600
umask 077
# Load alias definitions from ~/.bash_aliases
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# Add my ~/bin directory to my $PATH
if [ -d ~/bin ] ; then
PATH=~/bin:"${PATH}"
fi
There’s a lot going on here, but most of it is explained in the comments.
A few things of note:
-
The
EDITOR=
line indicates thatemacs
is my editor of choice. Some programs will use this information to load an editor when you have large amounts of text to enter/alter. -
The
umask
command sets my default file protections. See this discussion for details. -
At the end, I add my
~/bin
directory, where I store my personal collection of useful programs and scripts, to my $PATH variable so that I can run them without typing out their full path. -
Just before that, I loaded my
~/.bash_aliases
file.
1.1.3 .bash_aliases
The .bash_aliases
file contains a number of personal shortcuts that I have established. Here’s a sample of mine:
alias cp='cp -i'
alias mv='mv -i'
# enable color support of ls & grep
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
linux() {
ssh -f -Y -A $* linux.cs.odu.edu xterm -title "linux" -sl 500 -sb
}
-
The aliases for
cp
andmv
add a-i
option to those commands. This option causes the commands to warn me if I am about to overwrite an existing file. -
The alias for
ls
causes it to use different colors for directories and for executable files than for ordinary files (rather likels -F
). -
The aliases for
grep
cause it to use colors to highlight the matched portion of each line of output. -
Finally, I have set up
linux
as a shorthand command to launch a newxterm
remotely connected to one of of the CS Dept. Linux servers.
1.2 Customizing tcsh
When a C-shell or TC-shell is started up (e.g., whenever you log in, or spawn off a new xterm
), it executes the commands in a file called ~/.cshrc
You may or may not already have a .cshrc
file.. You can check by giving the command
ls -a ~
If you don’t have a .cshrc
file, you should make one. If you do, consider changing it as described here. Edit your .cshrc
file and insert the following:
setenv EDITOR emacs
umask 002
limit coredumpsize 0
#
# skip remaining setup if not an interactive shell
#
if ($?USER == 0 || $?prompt == 0) exit
set history=40
set ignoreeof
set ellipsis="..."
set prompt="%m:%c02> "
alias cp 'cp -i'
alias mv 'mv -i'
alias rm 'rm -i'
alias ls 'ls -F'
alias lsc 'ls --color=tty'
alias ff 'find . -name \!* -print'
The setenv
line indicates that emacs
is your editor of choice. Some programs, including many e-mail programs, will use this information to load an editor when you have large amounts of text to enter/alter.
The umask
command sets your default file protections. See this discussion for details.
Of the remaining lines, the most interesting are the alias
commands. These set up “abbreviations” for commands. In this case, we are mainly adding options to familiar commands. The first three aliases add a -i
option to the cp
, mv
, and rm
commands. This option causes each command to prompt for a confirmation whenever its action would result in a file being deleted. The fourth alias adds the -F
option to all ls
commands, making it easier to tell the difference between ordinary files, directories, and executable programs. The lsc
alias uses color to give cues as to which files are plain files and which are excutable, and which are directories. The final alias sets up a “find-file” command, ff
. This will search the current directory and all subdirectories within it for a file matching a given pattern. For example the command sequence
cd ~
ff '*.txt'
will list all of your files with the .txt
extension.
After you have checked this file and saved it, try invoking a new copy of the shell
tcsh
to test out the changes in behavior.
Another change worth considering in a .cshrc
file is adding additional directories to your PATH
. For example, if you want to be sure that xterm
and other X
client programs are readily available whenever you are logged in, you would want to make sure that /usr/local/X11R6/bin
is in your $PATH
. You can do this by adding the command
set path = (/usr/local/X11R6/bin $path)
to your .cshrc
file.
2 Customizing X Application Appearance
When an X application is started, it looks for instructions in a ~/.Xdefaults
file. These instructions can be used to control the appearance and, to a lesser degree, the behavior, of most X applications.
Each line in a .Xdefaults
file has the form
programName*resourceName: value
The programName is the name of the program to which this directive applies. Examples would be “xterm” or “emacs”. The resourceName is the name of some customizable property of that program. The value is the setting you want for that property. Exactly what resources can be customized and what the allowable values are likely to be will vary considerably from one program to another. You can usually find the list by issuing the man
command to read the manual page for that program. Almost all programs will support a geometry
resource to indicate how large a window to open and/or where to place it on the screen. Many support foreground
and background
resources to set text and background colors. Another common resource is font
, for designating the main text font.
For example, try putting the following into your own ~/.Xdefaults
file (on the Unix system):
!
! My xterm settings: I like a heavy font and always want a scrollbar
!
xterm*faceName: dejavu sans mono:medium:pixelsize=12
xterm*Geometry: 80x36
xterm*foreground: midnight blue
xterm*background: white
xterm*scrollBar: true
!
! A light-text-on-dark background version
dark*faceName: dejavu sans mono:medium:pixelsize=12
dark*Geometry: 60x24
dark*foreground: grey
dark*background: black
then launch a new xterm the “normal” way:
and also like this:
You can see the effects of changing the geometry and color resources.
By the way, emacs
offers a nice way to see the range of available color names. Running emacs under X, try giving the command M-x list-colors-display
. It’s better than the giant-size box of Crayolas!
The nastiest part of customizing X is dealing with fonts. As you can see from the above example, font names tend to be long and complicated. The command xlsfonts
will list all the fonts know to your remote Unix client machine. But that list is unlikely to be helpful, partly because it’s so long and partly because the X windows server program running on your local PC may not know how to display most of those fonts. A more helpful program is called xfontsel
. You can use it to sample all the available fonts, seeing how the entire character set will look on your machine.
3 Program Customizations
Many programs have their own mechanisms for customizing their appearance and behavior. Typically, these are stored in your home directory, but with file names beginning with “.”, making them invisible to your usual ls
command unless you add the -a
option. (Try doing a ls -a ~
right now - you might be surprised at how many things are already stored there.) The .login
and .cshrc
files discussed earlier are simply one example of this more general convention in Unix.
In many cases, these files represent information stored after you worked with a program and/or used menu settings to establish your preferences. For example, once you have used the insight
X window interface to the gdb
debugger, you will afterwards have a file ~/.gdbtkinit
that records the breakpoints you had set in your last debugging session and which of the optional “view” windows were open. Under normal circumstances, you would never alter this file directly.
On the other hand, some of these files are intended for you to edit directly. For example, we have already discussed altering your .cshrc
and .Xdefaults
files. Another worth mentioning is the file ~/.emacs
, which controls the behavior of the emacs
editor.
emacs
may be the most customizable program ever written, and the range of things you can put into a .emacs
file is well beyond the scope of this course. The best way for most people to get started in emacs customization is to look at other people’s .emacs
files, such as mine and to read the emacs documentation, particularly examples of some common things to put into a .emacs
file.