.cshrc

From Linux Bash Shell Scripting Tutorial Wiki
Jump to navigation Jump to search

The .cshrc file is the csh startup file. In addition, users can edit or create a particular file in their home directory called .cshrc. It is read every time you begin a new csh ( C Shell ) session.

Purpose

The .cshrc file in your home directory contains commands and other environment variables, including aliases. For instance, you can set $PATH, which tells the shell where to look for typed commands.

Viewing .cshrc file

Use the cat command or more command or less command to display your current .cshrc file in the current directory:

cat ~/.cshrc
more "$HOME/.cshrc"
.cshrc file is executed every time you execute a new csh session on Unix or Linux
.cshrc file is executed every time you execute a new csh session on Unix or Linux

Sample .cshrc file

Default .cshrc file on OpenBSD:

# $OpenBSD: dot.cshrc,v 1.10 2020/01/24 02:09:51 okan Exp $
#
# csh initialization

alias df	df -k
alias du	du -k
alias f		finger
alias h		'history -r | more'
alias j		jobs -l
alias la	ls -a
alias lf	ls -FA
alias ll	ls -lsA
alias tset	'set noglob histchars=""; eval `\tset -s \!*`; unset noglob histchars'
alias z		suspend

set path = (~/bin /bin /sbin /usr/{bin,sbin,X11R6/bin,local/bin,local/sbin,games})

if ($?prompt) then
	# An interactive shell -- set some stuff up
	set filec
	set history = 1000
	set ignoreeof
	set mail = (/var/mail/$USER)
	set mch = `hostname -s`
	alias prompt 'set prompt = "$mch:q"":$cwd:t {\!} "'
	alias cd 'cd \!*; prompt'
	alias chdir 'cd \!*; prompt'
	alias popd 'popd \!*; prompt'
	alias pushd 'pushd \!*; prompt'
	cd .
	umask 22
endif

Examples

Append the following in your ~/.cshrc file.

Setting prompt with csh

set prompt = "%m %C2 %h%# "

Where,

  1. m Show the short hostname
  2. C2 See the parent/current directory
  3. h Display the numbers in the history
  4. # View a % sign for normal users, # sign for root user.

Turning on auto-correction

When spelling correction is set as follows, and the shell thinks that any part of the command line is misspelled, it prompts with the corrected line:

set correct = cmd

For example,

lzs /etc/h*

One can answer 'y' or space to execute the corrected line, 'e' to leave the uncorrected command in the input buffer, 'a' to abort the command as if '^C' had been hit, and anything else to execute the original line unchanged.

CORRECT>ls /etc/h* (y|n|e|a)?
Spelling correction recognizes user-defined completions and set via ~/.cshrc file
Spelling correction recognizes user-defined completions and set via ~/.cshrc file

Setting autocomplete

If autolist variable set, possibilities are listed after an ambiguous completion. If it set to ambiguous, possibilities are listed only when no new characters are added by completion. For example:

set autolist=ambiguous

csh aliases

alias ls         'ls -F --color=auto'
alias ll         'ls -l'
alias ltr        'ls -ltr'
alias c          'clear'
alias df	     'df -h'
alias du	     'du -h'

See also