User:Mjb/tcsh configuration files

From Offset
< User:Mjb
Revision as of 10:10, 16 October 2015 by Mjb (talk | contribs)
Jump to navigationJump to search

Here are the tcsh shell configuration files I use. I try to keep them compatible with csh but have not fully tested them there.

Any questions/comments/suggestions, email me directly at root (at) skew.org.

.login

# csh and tcsh .login
#
# This is for things that only need to be run once, at login.
#
# See .cshrc for both login and non-login shell settings, including
# things to do if you are changing shells after login.

# (tcsh only) warn if current terminal type isn't in termcap or terminfo
#
if ($?tcsh) then
	if ( ! { (termname >& /dev/null) } ) then
		echo "Terminal type '${term}' is unrecognized on this system."
		echo "  If you want to set a different type, use 'setenv <foo>'."
		echo "  If you want to keep this type but use another's capabilities, use 'tset <foo>'."
		echo "  Use 'termname <foo>' to see if foo is recognized on this system."
		echo
	endif
endif

# backspace and delete need to work the way I expect them to
#
stty sane

# see who's logged in
#
echo 'Users online:'
users

# In xterm-based terminals, set the window title before each prompt is displayed.
# This is here instead of in .cshrc because this particular title uses the initial
# login date & time for a particular host. If it were in .cshrc, it would update
# the login time each time an interactive csh or tcsh subshell is started. 
#
if ( $?prompt && $TERM:s/-/./:r == "xterm" ) then
	alias settitle printf '"\033]2;\!*\a\033]1;\!*\a\033]0;\!*\a"'
	alias precmd settitle $HOST · logged in since `sh -c 'date -j "+%Y-%m-%d %T" 2> /dev/null || date "+%Y-%m-%d %T"'`
endif

# Some apps, like 'less' without the -X flag, like to switch to and from a 
# separate screen buffer (actually cursor-addressing mode rather than 
# memory-addressing mode). This capability is not on all terminals, and some 
# terminals wipe the original buffer when switching back to it. These 
# behaviors and their effects on terminal emulator scrollback buffers can be
# confusing for users.
#
# The switching is achieved by the ANSI code sequences assigned to terminal 
# capabilities 'ti' & 'te' (termcap) or 'smcup' & 'rmcup' (terminfo). It can 
# be disabled via a custom termcap or terminfo entry. Both work the same, 
# amending an existing entry to disable the capabilities via '@' suffixes.
#
# Here we do it for xterm-256color:
#
if (-f /etc/termcap) then
	# TERMCAP is the custom termcap entry itself
	setenv TERMCAP ${TERM}:ti@:te@:tc=${TERM}
else if (-d /etc/terminfo && -X tic) then
	# TERMINFO is a directory of tic(1)-compiled terminfo sources
	setenv TERMINFO ~/.terminfo
	setenv _TERMINFO_SOURCE ~/.terminfo-xterm-256color.src
	echo "xterm-256color, smcup@, rmcup@, use=xterm-256color," >! ${_TERMINFO_SOURCE}
	tic ${_TERMINFO_SOURCE}
	unsetenv _TERMINFO_SOURCE
else
	echo "Could not disable separate screen buffer."
endif

# warn if terminal size is undefined
#
if ( $?prompt && { ( stty -a | grep columns |  egrep -q '0 rows; 0 columns|rows 0; columns 0' ) } ) then
	echo "Terminal size is undefined; please 'stty rows # cols #' appropriately."
endif

if ( -f ~/.login.local ) source ~/.login.local

.cshrc

# mjb's .cshrc
# 
# Contains shell settings for csh and tcsh.
# On FreeBSD 4.1 and up, csh doesn't exist; it's a hard link to tcsh.
#
# Order of processing (login shells):
#   1. /etc/csh.cshrc
#   2. /etc/csh.login
#   3. ~/.tcshrc (if exists), else ~/.cshrc (this file)
#   4. file in 'histfile' shell variable (if exists), else ~/.history
#   5. ~/.login
#   6. file in 'dirsfile' shell variable (if exists), else ~/.cshdirs
#
# Order of processing (non-login shells):
#   1. /etc/csh.cshrc
#   2. ~/.tcshrc (if exists), else ~/.cshrc (this file)
#
# I use this .cshrc on multiple hosts, so it only contains settings
# common to all of them. At the end, it runs ~/.cshrc.local, which
# contains host-specific settings.
#

##############################################################################
# Settings for all csh/cshrc instances running under my userid
#

### umask sets a mask for the default file permissions
umask 022	# unset group & others write, i.e. rw-r--r-- or rwxr-xr-x

### character encoding and localization preference
setenv	LANG en_US.UTF-8	# another option is en_US.ISO8859-1

### misc program options
setenv	BLOCKSIZE 1k	# for /bin/ls
setenv	GZIP '-9'		# for /usr/bin/gzip
setenv	BZIP2 '-9'		# for /usr/bin/bzip2


##############################################################################
# Settings for all csh/cshrc interactive shells, which includes:
#   * login shell (the shell instance you get at login)
#   * non-login shells (any shell instances you invoke after login)
#
# This does not include shells referenced by script shebang lines unless they
# use -i, which would be very unusual. We could also use ($?loginsh) to test
# for -l or login shell, but there's not really anything that we would want
# to be exclusive to a login shell that we don't already do in ~/.login.
#
if ($?prompt) then

	### improved key bindings
	bindkey "^W" backward-delete-word		# ctrl + W
	bindkey -k up history-search-backward	# up arrow
	bindkey -k down history-search-forward	# down arrow
	bindkey '^[[1;5D' backward-word			# ctrl + left arrow
	bindkey '^[[1;5C' forward-word			# ctrl + right arrow
	bindkey "\e[1~" beginning-of-line		# home
	bindkey "\e[3~" delete-char				# delete
	bindkey "\e[4~" end-of-line				# end

	### ctrl-D at prompt won't logout
	set ignoreeof

	### tab completion
	set autolist

	### complete filenames in csh (automatic for tcsh)
	set filec

	### beep only if no matches in filename completion attempt
	set matchbeep = nomatch

	### default typing mode (insert or overwrite)
	set inputmode = insert

	### save 1000 lines of history
	set history = 1000

	### restrict output redirection to not destroy existing files
	set noclobber

	### aliases could be set in .login, but here is better (esp. for 'su')
	if (-f ~/.alias) source ~/.alias

	### ask for confirmation before doing 'rm *'
	set rmstar

	### CVS related
	setenv	CVS_RSH ssh
	setenv	CVSEDITOR nano

	### IRC related
	setenv	IRCNICK mjb

	### my favorite editors
	if (-x /usr/local/bin/nano) then
		setenv	EDITOR	/usr/local/bin/nano
		setenv 	VISUAL  /usr/local/bin/nano
	else if (-x /usr/local/bin/pico) then
		setenv	EDITOR	/usr/local/bin/pico
		setenv 	VISUAL	/usr/local/bin/pico
	endif

	### my favorite pagers
	if (-X less) then
		setenv	PAGER	less
		setenv	LESS	'--quit-at-eof --quit-if-one-screen --hilite-search --ignore-case --LONG-PROMPT --quiet --tabs=4 --tilde'
		setenv	LESSHISTFILE	/dev/null
	else
		setenv	PAGER	more
	endif

	### plain prompts; usually overridden below
	if ($?tcsh) then
		set prompt = "[%m] %t %c %# "
		set symlinks = chase	# in prompt, use real directory name, not symlink
		set noding				# in prompt, don't say "DING!" at the top of the hour
	else
		set mch = `hostname -s`
		set prompt = "${mch:q}: {\!} % "
	endif

	### in tcsh, allow 'echo' builtin to recognize SysV-style escape sequences
	set echo_style = both

	### if terminal supports ANSI text formatting reset (of bold, underline, etc.)
	# echotc is tcsh-only, so we use tput instead
	if ({ ( tput me >& /dev/null || tput sgr0 >& /dev/null ) }) then
		# make ${E} be escape plus '['
		if ($?tcsh) then
			# assumes echo_style is 'both'
			set E = `echo -n "\e["`
		else if ({ filetest -X printf }) then
			set E = `printf '\033['`
		else
			set E = `echo -n 'E[' | tr E \\033`
		endif

		### if terminal supports setting ANSI foreground color (thus, ANSI color codes in general)
		if ({ ( tput AF 37 >& /dev/null || tput setaf 37 >& /dev/null ) }) then
			if ($?tcsh) then
				### use fancy tcsh prompt with bold, underline, and color
				set prompt = "%{\033[0m\033[1;30m%}%U[%m] %t%u %B%{\033[33m%}%c%b %# "

				### enable color output for NLS messages
				set colorcat

				### enable color output for ls-F (shell builtin)
				### without args, ls-F calls /bin/ls -F and colorizes as per LS_COLORS, below
				### with args, ls-F calls /bin/ls -F, plus /bin/ls's color flag:
				###   on most systems, it's --color=auto (as used by GNU ls)
				###   on BSD, it's -G (colors as per LSCOLORS, below)
				### on BSD, CLICOLOR being set forces -G behavior even if 'color' is unset!
				set color

				### ANSI color codes for ls-F when called without arguments
				### (with arguments, regular /bin/ls -G handling applies!)
				if (-f ~/.colors) setenv LS_COLORS `awk '! /^#|^$|\t/ {printf $1"="$2":"}' ~/.colors`
			endif

			### enable color output for BSD /bin/ls (has same effect as /bin/ls -G)
			### CLICOLOR_FORCE is needed to keep color when piping ls output to 'less'
			### CLICOLOR_FORCE may interfere with programs that parse ls output!
			setenv	CLICOLOR
			setenv	CLICOLOR_FORCE
			
			### color codes for BSD /bin/ls with CLICOLOR set or with -G
			setenv	LSCOLORS GxDxBeCbCxExhxfxgxfxgx
						
			### enable color in GNU grep
			setenv	GREP_OPTIONS --color=auto

			### set temporary variables for ANSI text formatting with color
			### ';5;nnn' assumes extended color is available.
			set ANSI_black			= "${E}0;30m"
			set ANSI_red			= "${E}0;31m"
			set ANSI_green			= "${E}0;32m"
			set ANSI_yellow			= "${E}0;33m"
			set ANSI_blue			= "${E}0;34m"
			set ANSI_magenta		= "${E}0;35m"
			set ANSI_cyan			= "${E}0;36m"
			set ANSI_white			= "${E}0;37m"
			set ANSI_brightblack	= "${E}1;30m"
			set ANSI_brightred		= "${E}1;31m"
			set ANSI_brightgreen	= "${E}1;32m"
			set ANSI_brightyellow	= "${E}1;33m"
			set ANSI_brightblue		= "${E}1;34m"
			set ANSI_brightmagenta	= "${E}1;35m"
			set ANSI_brightcyan		= "${E}1;36m"
			set ANSI_brightwhite	= "${E}1;37m"
			set ANSI_defaultcolor	= "${E}39m"
			set ANSI_reset			= "${E}0m"
			if ({ (termname | fgrep -q 256color) }) then
				set ANSI_bold			= "${E}1;38;5;74m"
				set ANSI_underline_on	= "${E}4m${E}38;5;146m"
				set ANSI_standout_on	= "${E}48;5;52m${E}38;5;197m"
			else
				set ANSI_bold			= "${ANSI_brightcyan}"
				set ANSI_underline_on	= "${E}4m${ANSI_magenta}"
				set ANSI_standout_on	= "${E}41m${ANSI_brightyellow}"
			endif
			set ANSI_underline_off	= "${E}24m${ANSI_defaultcolor}"
			set ANSI_standout_off	= "${E}49m${ANSI_defaultcolor}"
			set ANSI_dim			= "${E}2m"
			set ANSI_blink			= "${E}5m"
			set ANSI_reverse		= "${E}7m"
			set DIFF_plain_text	= "${ANSI_brightblack}"
			set DIFF_file_old	= "${ANSI_cyan}"
			set DIFF_file_new	= "${ANSI_yellow}"
			set DIFF_added		= "${ANSI_brightblue}"
			set DIFF_removed	= "${ANSI_brightred}"
			set DIFF_changed	= "${ANSI_blue}"
			set DIFF_diffstuff	= "${ANSI_magenta}"

		else
			### use fancy tcsh prompt with bold & underline but without color
			if ($?tcsh) set prompt = "%U[%m] %t%u %B%c%b %# "

			### set temporary variables for ANSI text formatting without color
			set ANSI_reset			= "${E}0m"
			set ANSI_bold			= "${E}1m"
			set ANSI_dim			= "${E}2m"
			set ANSI_blink			= "${E}5m"
			set ANSI_reverse		= "${E}7m"
			set ANSI_underline_on	= "${E}4m"
			set ANSI_underline_off	= "${E}24m"
			set ANSI_standout_on	= "${E}48m"
			set ANSI_standout_off	= "${E}49m"
			# untested
			set DIFF_plain_text	= "${ANSI_reset}"
			set DIFF_file_old	= "${ANSI_dim}"
			set DIFF_file_new	= "${ANSI_bold}"
			set DIFF_added		= ""
			set DIFF_removed	= ""
			set DIFF_changed	= ""
			set DIFF_diffstuff	= "${ANSI_standout_on}"

		endif				

		### customizing 'less' makes man pages pretty
		if ( $?LESS ) then
			setenv LESS "${LESS} --RAW-CONTROL-CHARS"
			setenv LESS_TERMCAP_me "${ANSI_reset}"
			if ({ ( tput mb >& /dev/null || tput blink >& /dev/null ) }) setenv LESS_TERMCAP_mb "${ANSI_blink}"
			if ({ ( tput md >& /dev/null || tput bold >& /dev/null ) }) setenv LESS_TERMCAP_md "${ANSI_bold}"
			if ({ ( tput mh >& /dev/null || tput dim >& /dev/null ) }) setenv LESS_TERMCAP_mh "${ANSI_dim}"
			if ({ ( tput mr >& /dev/null || tput rev >& /dev/null ) }) setenv LESS_TERMCAP_mr "${ANSI_reverse}"
			if ({ ( tput us >& /dev/null || tput smul >& /dev/null ) }) setenv LESS_TERMCAP_us "${ANSI_underline_on}" && setenv LESS_TERMCAP_ue "${ANSI_underline_off}"
			if ({ ( tput so >& /dev/null || tput smso >& /dev/null ) }) setenv LESS_TERMCAP_so "${ANSI_standout_on}" && setenv LESS_TERMCAP_se "${ANSI_standout_off}"
		else
			setenv LESS "--RAW-CONTROL-CHARS"
		endif

		### create aliases for colored diffs
		### diff-n, diff-u, diff-c for normal, unified, context
		set diffn_exprs = '-E -e "s/^> /'"${DIFF_file_new}"'> /" -e "s/^\< /'"${DIFF_file_old}"'< /" -e "s/^[0-9]+/'"${DIFF_diffstuff}"'&/"'
		set diffu_exprs = '-e "s/^\+/'"${DIFF_file_new}"'+/" -e "s/^-/'"${DIFF_file_old}"'-/" -e "s/^@@ /'"${DIFF_diffstuff}"'@@/" -e "s/^ /'"${DIFF_plain_text}"' /"'
		set diffc_exprs = '-e "s/^\*\*\* /'"${DIFF_file_new}"'*** /" -e "s/^--- /'"${DIFF_file_old}"'--- /" -e "s/^+ /'"${DIFF_added}"'+ /" -e "s/^- /'"${DIFF_removed}"'- /" -e "s/^! /'"${DIFF_changed}"'! /" -e "s/^\*\*\*\*/'"${DIFF_diffstuff}"'****/" -e "s/^ /'"${DIFF_plain_text}"' /"'
		alias diff-n 'diff --normal \!* | sed '"${diffn_exprs}"
		alias diff-u 'diff -u \!* | sed '"${diffu_exprs}"
		alias diff-c 'diff -c \!* | sed '"${diffc_exprs}"
				
		### unset our temporary variables
		unset ANSI_*
		unset DIFF_*
		unset E

	endif

endif

if (-f ~/.cshrc.local) source ~/.cshrc.local

.colors

This is referenced by my .cshrc, above.

tcsh's builtin command ls-F, when invoked without other options while the shell variable color is set, colorizes the plain output of /bin/ls -F. tcsh uses the LS_COLORS environment variable to customize these colors. LS_COLORS uses the same format as expected by GNU ls. On a Linux system you can run dircolors to generate LS_COLORS from a config file, or run dircolors --print-database to see the config file. On FreeBSD, you can accomplish pretty much the same thing using the technique below.

However, if you invoke /bin/ls directly, or if you use ls-F with other options (like ls-F -A), then LS_COLORS is ignored. Instead, depending on whether the color shell variable is set, tcsh just passes the extra ls-F options to /bin/ls or /bin/ls -G (or /bin/ls --color=auto on non-BSD OSes). FreeBSD's /bin/ls uses an LSCOLORS environment variable (no underscore in its name) when colorizing the output. LSCOLORS has a special format and is much less capable than LS_COLORS; see the ls(1) man page. This colorization by /bin/ls will happen whenever it is invoked with -G or when the CLICOLOR environment variable is set. However, it will not happen if the output is being redirected and CLICOLOR_FORCE is not set. Apps which parse the output of /bin/ls tend to have difficulty when CLICOLOR_FORCE is set.

Changes from original version: I made the defaults match my LSCOLORS; I added a few document, media, and source file types; and I changed the heinous purple document color to bright white.

# ~/.colors v0.99 for FreeBSD tcsh - [c]2012 Michael S. Sanders

# SYNOPSIS:  an enhancement  of  the tcsh  builtin  'LS_COLORS' providing  a
# method of  organizing/externalizing file  colors. The  strategy is  to use
# only native FreeBSD components (no 3rd party pkgs or ports).

# REQUIREMENTS: tcsh, awk, & your favorite editor

# UPDATES (if any): http://topcat.hypermart.net/index.html

# LEGALESE:  Permission is  hereby granted,  free of  charge, to  any person
# obtaining a copy of this  software and associated documentation files (the
# "Software"),  to  deal  in  the Software  without  restriction,  including
# without  limitation  the rights  to  use,  copy, modify,  merge,  publish,
# distribute, sublicense, and/or sell copies  of the Software, and to permit
# persons  to whom  the  Software is  furnished  to do  so,  subject to  the
# following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT  NOT LIMITED TO THE  WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR  A PARTICULAR PURPOSE  AND NONINFRINGEMENT. IN NO  EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER  IN AN ACTION  OF CONTRACT, TORT OR  OTHERWISE, ARISING
# FROM,  OUT OF  OR IN  CONNECTION WITH  THE SOFTWARE  OR THE  USE OR  OTHER
# DEALINGS IN THE SOFTWARE.

# SETUP (3 steps):
#
# 1. save this file in your home directory as '.colors'
#
# 2. add the following construct to ~/.tcshrc, or ~/.cshrc (excluding '#')
#
# if (-f ~/.colors) then
#    alias ls 'ls-F'
#    set listflags='ha'
#    set color
#    setenv LS_COLORS `awk '! /^#|^$|\t/ {printf $1"="$2":"}' ~/.colors`
# endif
#
# 3. (re)source your ~/.tcshrc by invoking 'source ~/.tcshrc'

# SYNTAX: <*.ext><space>[<intensity<;>]<foreground>[<;><background>]
#
# note: square brackets above indicate items are optional
#
# examples...
#
# *.mp3       31 # normal red
# *.png     1;32 # bold green
# *.doc     4;33 # underlined yellow
# *.man     5;34 # flashing blue
# *.c      30;46 # black foreground/cyan background
# *.xyz 05;31;43 # flashing red foreground/yellow background
#
# ISO 6429 color table
#
# color    intensity     foreground   background
# ----------------------------------------------
# black  | 0 normal    | 30         | 40
# red    | 1 bold      | 31         | 41
# green  | 4 underline | 32         | 42
# yellow | 5 flashing  | 33         | 43
# blue   |             | 34         | 44
# purple |             | 35         | 45
# cyan   |             | 36         | 46
# white  |             | 37         | 47
# ----------------------------------------------

# COLORS: add/remove/season to taste

# media (red)

*.avi        31
*.bmp        31
*.flc        31
*.fli        31
*.flv        31
*.gif        31
*.jpeg       31
*.jpg        31
*.m4a        31
*.mid        31
*.mov        31
*.mp2        31
*.mp3        31
*.mp4        31
*.mpeg       31
*.mpg        31
*.nes        31
*.pls        31
*.png        31
*.swf        31
*.wav        31
*.xbm        31
*.xpm        31

# archive (yellow/brown)

*.bz2        33
*.gz         33
*.img        33
*.iso        33
*.jar        33
*.tar        33
*.tar.gz     33
*.tar.xz     33
*.tbz        33
*.tgz        33
*.xz         33
*.zip        33

# docs (white)

*.1        1;37
*.asc      1;37
*.csv      1;37
*.doc      1;37
*.htm      1;37
*.html     1;37
*.info     1;37
*.log      1;37
*.man      1;37
*.nfo      1;37
*.pdf      1;37
*.pod      1;37
*.ps       1;37
*.sig      1;37
*.tex      1;37
*.txt      1;37

# rc/dot files (cyan)

*.cshrc      36
*.exrc       36
*.history    36
*.inputrc    36
*.login      36
*.logout     36
*.mailrc     36
*.muttrc     36
*.nano       36
*.netrc      36
*.newsrc     36
*.pid        36
*.profile    36
*.shrc       36
*.tcshrc     36
*.xinitrc    36

# source (gray)

*.4th      1;30
*.asm      1;30
*.awk      1;30
*.c        1;30
*.cpp      1;30
*.css      1;30
*.diff     1;30
*.git      1;30
*.h        1;30
*.js       1;30
*.lex      1;30
*.lisp     1;30
*.lua      1;30
*.patch    1;30
*.php      1;30
*.pl       1;30
*.py       1;30
*.rb       1;30
*.sed      1;30
*.sh       1;30
*.sql      1;30
*.tcl      1;30
*.xml      1;30
*.xsl      1;30
*.yak      1;30
Makefile   1;30

# file attributes (these *always* overide other colors)

no            0 # normal (non-filename) text
fi            0 # regular file
di         1;36 # directory
ln         1;33 # symbolic link
pi      1;32;41 # named pipe (fifo)
so      1;31;44 # socket
do         1;35 # door
bd         1;34 # block device
cd           37 # character device
ex         1;32 # executable file
mi         5;31 # missing file (defaults to fi)
or         5;31 # orphaned symbolic link (defaults to ln)

# eof

~/.alias

Here are some highlights from my .alias file, as referenced by my .cshrc, above.

# directory listing
#
alias ls	'ls-F -Ahs'
alias la	'ls-F -Ahl \!* | tr -d "\000" | less --QUIT-AT-EOF'
# tr -d "\000" is to delete vt100 padding characters; see
# http://lists.freebsd.org/pipermail/freebsd-questions/2008-June/177876.html

# terminal reset
#
alias cls	echotc cls
alias vtn	'echo "X[4iX[?4iX[?38lX\X(BX)0OX[?5lX[0mX[rX[HX[J" | tr "XO" "\033\017"'

# misc command shortcuts / default options
#
alias cp	'cp -p'
alias dump	'tcpdump -s 0 -w - port 80'
alias xit	exit
alias bzip	bzip2
alias bunzip	bunzip2
alias bz	bzip2
alias buz	bunzip2
alias guz	gunzip
alias gz	gzip
alias mv	'mv -i'

# emit an ISO 8601 date
#
alias isodate	date "+%Y-%m-%dT%H:%M:%S"

# iso-2022-jp to utf-8 filter
#
alias j "python -c ""'import sys;i=sys.stdin;print i.read().decode("'"iso-2022-jp"'","'"replace"'").encode("'"utf-8"'");'"

# show IPv4 addresses in use
alias whatsmyip ifconfig \| awk \'/inet/ { print \$2 \}\' \| egrep -v \'::\|^127.0.0.1\'

if (-f ~/.alias.local) source ~/.alias.local