Added the ua (unarchive) shortcut + an option in selector-history.
[scripts.git] / bashrc
1 # -*-Shell-script-*-
2
3 #########################################################################
4 # This program is free software: you can redistribute it and/or modify  #
5 # it under the terms of the version 3 of the GNU General Public License #
6 # as published by the Free Software Foundation.                         #
7 #                                                                       #
8 # This program is distributed in the hope that it will be useful, but   #
9 # WITHOUT ANY WARRANTY; without even the implied warranty of            #
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      #
11 # General Public License for more details.                              #
12 #                                                                       #
13 # You should have received a copy of the GNU General Public License     #
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.  #
15 #                                                                       #
16 # Written by and Copyright (C) Francois Fleuret                         #
17 # Contact <francois@fleuret.org> for comments & bug reports             #
18 #########################################################################
19
20 # The site-specific and confidential settings are in another file
21
22 PRIVATE_BASHRC="${HOME}/private/bashrc.perso"
23
24 # If the MANPATH is not set, set it
25
26 [ "${MANPATH}" ] || MANPATH=$(manpath)
27
28 # If the private bashrc exists, execute it
29
30 [ -f "${PRIVATE_BASHRC}" ] && source "${PRIVATE_BASHRC}"
31
32 # !!! THIS HAS TO BE HERE EVEN IN THE NON-INTERACTIVE PART OR YOU WILL
33 # LOSE YOU PREVIOUS HISTORY !!!
34
35 export HISTFILESIZE=20000
36 export HISTSIZE=${HISTFILESIZE}
37
38 export HISTIGNORE="${HISTIGNORE}:&:[ ]*"
39
40 # I want to save the command time, but I do not want to see it in
41 # history
42
43 export HISTTIMEFORMAT=""
44
45 shopt -s histappend
46
47 # I realized that most of my settings are meaningful only in
48 # interactive mode. This should maybe be done more properly through
49 # using different .bash_profile and .bash_login
50
51 [[ ${TERM} == "dumb" ]] || [ ! -t 0 ] && return
52
53 ######################################################################
54 ## The interactive part
55
56 export VT_RESET=$'\e[0m'
57 export VT_BOLD=$'\e[1m'
58 export VT_UNDERLINE=$'\e[4m'
59 export VT_BLINK=$'\e[5m'
60
61 export VT_SET_TITLE=$'\e]0;'
62 export VT_END_TITLE=$'\007'
63
64 export VT_BLACK_FG=$'\e[30m'
65 export VT_RED_FG=$'\e[31m'
66 export VT_GREEN_FG=$'\e[32m'
67 export VT_YELLOW_FG=$'\e[33m'
68 export VT_BLUE_FG=$'\e[34m'
69 export VT_MAGENTA_FG=$'\e[35m'
70 export VT_CYAN_FG=$'\e[36m'
71 export VT_WHITE_FG=$'\e[37m'
72
73 export VT_BLACK_BG=$'\e[40m'
74 export VT_RED_BG=$'\e[41m'
75 export VT_GREEN_BG=$'\e[42m'
76 export VT_YELLOW_BG=$'\e[43m'
77 export VT_BLUE_BG=$'\e[44m'
78 export VT_MAGENTA_BG=$'\e[45m'
79 export VT_CYAN_BG=$'\e[46m'
80 export VT_WHITE_BG=$'\e[47m'
81
82 # Colorize man pages!
83
84 export LESS_TERMCAP_us=${VT_GREEN_FG}
85 export LESS_TERMCAP_ue=${VT_RESET}
86 export LESS_TERMCAP_md=${VT_BLUE_FG}${VT_BOLD}
87 export LESS_TERMCAP_me=${VT_RESET}
88
89 # export LESS_TERMCAP_md=$'\e[1;34;40m'
90
91 # This prevents ^S from freezing the shell
92
93 stty -ixon
94
95 ulimit -c unlimited
96
97 alias ..='cd ..'
98 alias -- -='cd -'
99 alias rm='rm -i'
100 alias mv='mv -i'
101 # alias chmod='chmod -v'
102 alias chmod='chmod -c'
103 alias cp='cp -i'
104 alias rd=rmdir
105 alias md='mkdir -v'
106 alias ps='ps uxaf'
107 alias df='df -hT --sync'
108 alias grep='grep -E --mmap --color=auto'
109 alias find='ionice -c3 find'
110
111 alias s='screen -d -R -U && clear'
112
113 # alias kj="keyjnote -s -D 1000 -t Crossfade -T 100"
114 alias impressive="impressive -s -D 1000 -t Crossfade -T 100"
115
116 # alias fdupes='fdupes -r .'
117
118 # ls colors
119
120 if [ -e "${HOME}/.dircolors" ]; then
121     eval $(dircolors "${HOME}/.dircolors")
122     alias ls='ls --group-directories-first --color'
123     alias lt='ls --color -gohtr --time-style="+%Y %b %d %H:%M"'
124     alias ll='ls --color -goh --time-style="+%Y %b %d %H:%M"'
125     alias lll='ls --color -lth'
126     alias l='ls --color -I "*~" -I "*.o"'
127     alias less='less -R'
128 else
129     alias ls='ls --group-directories-first'
130     alias lt='ls -gohtr --time-style="+%Y %b %d %H:%M"'
131     alias ll='ls -goh --time-style="+%Y %b %d %H:%M"'
132     alias lll='ls -lth'
133     alias l='ls -I "*~" -I "*.o"'
134 fi
135
136 export EDITOR=emacsclient
137 export GIT_EDITOR=${EDITOR}
138
139 ######################################################################
140 # Ignored extensions when completing
141
142 # export FIGNORE="CVS"
143
144 ######################################################################
145 # Functions
146
147 # Find a file whose name contains a substring
148
149 function fn () {
150     name=$1
151     shift
152     find "$@" -name "*${name}*";
153 }
154
155 function bak () {
156     while [[ "$1" ]]; do
157         cp "$1" "$1".bak
158         shift
159     done
160 }
161
162 function ua () {
163     while [[ "$1" ]]; do
164
165         case "$1" in
166
167             *.tgz|*.tar.gz)
168                 tar zxvf "$1"
169                 ;;
170
171             *.rar)
172                 unrar x "$1"
173                 ;;
174
175             *.zip)
176                 unzip "$1"
177                 ;;
178
179             *)
180                 echo "Unknown file extension $1"
181                 ;;
182         esac
183
184         shift
185
186     done
187 }
188
189 # Create a dir and cd there
190
191 function mcd () {
192     mkdir -vp "$1"
193     cd "$1"
194 }
195
196 # Capture the screen in a dated png
197
198 function cap () {
199     if [[ $2 ]]; then
200         name=$2
201     else
202         name="capture-$(date +%s).png"
203     fi
204     echo "Waiting $1 s and saving to ${name}."
205     [[ "$1" ]] && sleep "$1"
206     echo "Please click on the window to capture."
207     xwd  | convert - ${name}
208     \ls -l ${name}
209 }
210
211 # Create and CD in a /tmp/tmp.XXXXXX directory. With the '-'
212 # arguments, do not create one and CD in the most recent instead
213
214 function cdt () {
215     if [[ "$1" ]]; then
216         if [[ "$1" == "-" ]]; then
217             cd $(\ls -td /tmp/tmp.?????? | head -1)
218         else
219             echo "USAGE: cdt [-]" >&2
220             return 1
221         fi
222     else
223         cd $(mktemp -d /tmp/tmp.XXXXXX)
224     fi
225 }
226
227 alias t='cd /tmp'
228
229 function trash () {
230     TRASH=$(date +/tmp/trash-%Y-%b-%d-%Hh)
231
232     if [[ -d ${TRASH} ]]; then
233         echo "Re-use ${TRASH}"
234     else
235         mkdir ${TRASH}
236         echo "Created ${TRASH}"
237     fi
238
239     mv "$@" ${TRASH}
240     echo "Trashed $@"
241 }
242
243 # alias trash=trash.sh
244
245 function mmsget () {
246     mplayer "$1" -dumpstream -dumpfile $(basename "$1")
247 }
248
249 function quicktex () {
250     if [[ $1 ]]; then
251         MAIN=$1
252     else
253         MAIN=$(\ls -t *.tex | head -1 | sed -r -e 's/\.tex//')
254     fi
255     pdflatex ${MAIN}
256     bibtex ${MAIN}
257     pdflatex ${MAIN}
258     pdflatex ${MAIN}
259     xpdf ${MAIN}.pdf
260 }
261
262 ######################################################################
263 # http://www.reddit.com/r/linux/comments/akt3j/a_functional_programming_style_map_function_for/
264
265 function map () {
266     local command i rep
267     if [ $# -lt 2 ] || [[ ! "$@" =~ :[[:space:]] ]];then
268         echo "Invalid syntax." >&2; return 1
269     fi
270     until [[ "$1" =~ : ]]; do
271         command="$command $1"; shift
272     done
273     command="$command ${1%:}"; shift
274     for i in "$@"; do
275         if [[ $command =~ \{\} ]];then
276             rep="${command//\{\}/\"$i\"}"
277             eval "${rep//\\/\\\\}"
278         else
279             eval "${command//\\/\\\\} \"${i//\\/\\\\}\""
280         fi
281     done
282 }
283
284 ######################################################################
285 ## A version of pho which stores the image numbers in environment
286 ## variables
287
288 function pho () {
289     PHO_BIN=/usr/bin/pho
290     TEMP=$(mktemp /tmp/pho.XXXXXXX)
291     ${PHO_BIN} "$@" | tee ${TEMP}
292     PHO_NOTE_1=$(grep ^"Note 1: " ${TEMP} | sed -e "s/^[^:]*: //")
293     PHO_NOTE_2=$(grep ^"Note 2: " ${TEMP} | sed -e "s/^[^:]*: //")
294     PHO_NOTE_3=$(grep ^"Note 3: " ${TEMP} | sed -e "s/^[^:]*: //")
295     PHO_NOTE_R90=$(grep ^"Rotate 90 \(CW\): " ${TEMP} | sed -e "s/^[^:]*: //")
296     PHO_NOTE_R180=$(grep ^"Rotate 180: " ${TEMP} | sed -e "s/^[^:]*: //")
297     PHO_NOTE_R270=$(grep ^"Rotate -90 \(CCW\): " ${TEMP} | sed -e "s/^[^:]*: //")
298     \rm ${TEMP}
299 }
300
301 # function rotjpeg () {
302     # if [ "$1" == "90" ] || [ "$1" == "180" ] || [ "$1" == "270" ]; then
303         # TEMP=$(mktemp /tmp/rotjpeg.XXXXXX)
304         # echo jpegtran -rotate "$1" -copy all $2 > ${TEMP}
305         # echo cp $2 ${2/jpg/}original.jpg
306         # echo cp ${TEMP} $2
307         # rm ${TEMP}
308     # else
309         # echo "Can not rotate with an angle of "$1" degrees."
310     # fi
311 # }
312
313 ######################################################################
314 ## A version of date that shows the time at home if TZ is set
315
316 function dt () {
317     echo "Local: $(date)"
318     if [[ ${TZ} ]]; then
319         unset TZ
320         echo "Home:  $(date)"
321     fi
322 }
323
324 ######################################################################
325 ## ifup / ifdown with sudo and memorization of the network
326
327 ## When invoked without an argument netup uses the same argument as
328 ## the previous time
329
330 ## When invoked without an argument netdown removes the last interface
331 ## which was netuped
332
333 [[ ${NETUP_HISTORY} ]] || NETUP_HISTORY="${HOME}/.netup_history"
334
335 function netup () {
336     if [[ ! "$@" ]] && [[ -s ${NETUP_HISTORY} ]]; then
337         # If we have no argument and there is a .netup_history, use it
338         ARGS=$(cat ${NETUP_HISTORY})
339     else
340         # Otherwise uses the given arguments, and store them
341         ARGS="$@"
342         echo ${ARGS} > ${NETUP_HISTORY}
343     fi
344
345     echo "${VT_GREEN_FG}Running [sudo ifup ${ARGS}]${VT_RESET}"
346     sudo ifup ${ARGS}
347
348     # Ugly hack to remove the dsl modem dns server when we add
349     # explicitely a dns in the /etc/network/interfaces
350
351     REMOVE_LOCAL_DNS=/usr/local/bin/remove-local-dns.sh
352
353     if [[ -x ${REMOVE_LOCAL_DNS} ]]; then
354         echo "${VT_GREEN_FG}Running [sudo ${REMOVE_LOCAL_DNS} 192.168]${VT_RESET}"
355         sudo ${REMOVE_LOCAL_DNS} 192.168
356     fi
357 }
358
359 function netdown () {
360     if [[ ! "$@" ]] && [[ -s ${NETUP_HISTORY} ]]; then
361         # If there are no arguments and there is a .netup_history, get the
362         # interface from it
363         ARGS=$(tail -1 ${NETUP_HISTORY} | sed -e "s/=.*$//")
364     else
365         # Otherwise, use the standard ifdown
366         ARGS="$@"
367     fi
368     echo "${VT_GREEN_FG}Running sudo [ifdown ${ARGS}]${VT_RESET}"
369     sudo ifdown ${ARGS}
370 }
371
372 function checkgw () {
373     GW=$(route -n | grep ^0.0.0.0 | awk '{print $2}')
374     if [[ -n "${GW}" ]]; then
375         ping ${GW}
376     else
377         echo "Can not find a getaway." >&2
378         return 1
379     fi
380 }
381
382 ######################################################################
383 # Show the most recent files, no scroll
384
385 function lr () {
386     HEIGHT=$(stty size | awk '{print $1}')
387     WIDTH=$(stty size | awk '{print $2}')
388     \ls -goth --time-style="+%Y %b %d %H:%M" "$@" | \
389         head -$((HEIGHT-2)) | \
390         cut -b1-${WIDTH}
391 }
392
393 ######################################################################
394 # cd and ls into a directory
395 # [from http://www.oreillynet.com/onlamp/blog/2007/01/whats_in_your_bash_history.html]
396
397 # function c () { cd "$@" && lr; }
398
399 ######################################################################
400 # You can change the xterm background color on the fly!
401
402 function setxtermbg () {
403     echo -n $'\e]11;'$1$'\007'
404 }
405
406 ######################################################################
407 # Shuffle the lines from the stdin
408
409 function shuffle () {
410     SEED=$1
411     [[ $SEED ]] || SEED=0
412     awk 'BEGIN{srand('${SEED}')} { print rand()" "$0 }' | sort -g | sed -e "s/^[0-9\.e\-]* //"
413 }
414
415 ######################################################################
416 # Stores the last entered command into a file
417
418 KEPT_COMMANDS=${HOME}/.kept_bash_commands
419
420 function keep () {
421     if [[ ${KEPT_COMMANDS} ]]; then
422         LINE=$(history | tail -2 | head -1 | sed -e "s/^[0-9 ]*//")
423         echo $LINE
424         echo $(date)": "${LINE} >> ${KEPT_COMMANDS}
425     else
426         echo "You have to set \$KEPT_COMMANDS"
427     fi
428 }
429
430 ######################################################################
431 # I sometime burn CDs and DVDs
432
433 function burn () {
434     DEVICE="/dev/cdrw"
435     if [[ ! "$1" ]]; then
436         echo "burn <iso name | dirname>" >&2
437     elif [[ -f "$1" ]]; then
438         if [[ $(file "$1" | grep "ISO 9660") ]]; then
439             wodim -eject -v dev=${DEVICE} "$1"
440         else
441             echo "Unknown type of $1" >&2
442         fi
443     elif [[ -d "$1" ]]; then
444         TMP=$(mktemp /tmp/cdimage.XXXXXX) && \
445             genisoimage -input-charset iso8859-1 -r -o ${TMP} "$1" && \
446             wodim -eject -v dev=${DEVICE} ${TMP}
447         rm -f ${TMP}
448     else
449         echo "Can not find $1" >&2
450     fi
451 }
452
453 ######################################################################
454 # And watch DVDs too!
455
456 function dvd () {
457
458     echo
459     echo " ! @   Seek to the beginning of the previous/next chapter"
460     echo " j     Cycle through the available subtitles"
461     echo " o     Show/hide the timing"
462     echo " x z   Subtitle delay"
463     echo " / *   Volume"
464     echo
465
466     if [[ "$1" ]]; then
467         dvd_device="$1"
468         shift
469     else
470         dvd_device="/dev/cdrom"
471     fi
472
473     title="1"
474
475     if [[ "$1" ]]; then
476         title=$1
477         shift
478     fi
479
480     mplayer > /dev/null \
481         -stop-xscreensaver \
482         -vc ffmpeg12 -quiet \
483         -vf yadif \
484         -alang en \
485         -softvol -softvol-max 1000 \
486         -dvd-device ${dvd_device} dvd://${title}
487
488 # -slang en
489
490 }
491
492 function ripdvd () {
493     mkdir -p ${HOME}/dvds
494     cd ${HOME}/dvds
495     time dvdbackup -v -M && eject
496 }
497
498 alias ripcd=abcde
499
500 ######################################################################
501 # Upload the sources from the current directory to work
502
503 function ulsrc () {
504     if [[ ! "${MY_WORK_MACHINE}" ]]; then
505         echo "\$MY_WORK_MACHINE undefined" 1>&2
506         return 1
507     fi
508
509     DIR=${PWD/$HOME\//}
510
511     scp {Makefile,*.{cc,h,sh}} ${MY_WORK_MACHINE}:${DIR}
512
513     echo "Uploaded to ${MY_WORK_MACHINE}:${DIR}/"
514 }
515
516 ######################################################################
517 # Create small images from images
518
519 function mksmall () {
520
521     PARAMS="-geometry 800x600"
522
523     # Auto-orient does not seem to work at all, hence the ugly hack
524     # with exif below
525
526     # PARAMS="-auto-orient -geometry 800x600"
527
528     echo "Using ${PARAMS}"
529
530     DEST_DIR=$1
531
532     [[ ${DEST_DIR} ]] || DEST_DIR=./small
533
534     mkdir -p ${DEST_DIR}
535
536     if [[ ! -d ${DEST_DIR} ]]; then
537         echo "Can not create ${DEST_DIR}" >&2
538         return
539     fi
540
541     NB_TOTAL=$(find -maxdepth 1 -type f | wc -l)
542     NB=0
543
544     for i in $(find -maxdepth 1 -type f); do
545         if [[ $(file $i | grep image) ]]; then
546             if [[ -e ${DEST_DIR}/$i ]]; then
547                 echo "The file ${DEST_DIR}/$i already exists."
548             else
549
550                 orientation=$(exif $i \
551                     | grep ^Orientation \
552                     | head -1 \
553                     | sed -e "s/^[^|]*|//" \
554                     | sed -e "s/ *$//")
555
556                 case ${orientation} in
557                     ""|"top - left")
558                         rotation_cmd=""
559                         ;;
560
561                     "right - top")
562                         rotation_cmd="-rotate 90"
563                         ;;
564
565                     "left - bottom")
566                         rotation_cmd="-rotate 270"
567                         ;;
568
569                     *)
570                         rotation_cmd=""
571                         echo "Unknown orientation \"${orientation}\" !"
572                         ;;
573                 esac
574
575                 if [[ $(file ${i/%.*/}.* | grep -E movie) ]] ; then
576                     CAPTION_PARAMS="-font FreeSans-Bold -pointsize 32 -fill green -annotate +10+32 Video"
577                 else
578                     CAPTION_PARAMS=""
579                 fi
580
581                 convert ${rotation_cmd} $i ${PARAMS} ${CAPTION_PARAMS} ${DEST_DIR}/$i
582             fi
583
584             \ls -lt ${DEST_DIR}/$i
585         fi
586
587         NB=$((NB+1))
588
589         echo "$((NB*100/NB_TOTAL))% (${NB}/${NB_TOTAL})"
590     done
591 }
592
593 ######################################################################
594 # Move a file to the ~/sources/config directory and replace it where
595 # it was by a symbolic link
596
597 function mvtoconfig () {
598     CONFIGDIR=${HOME}/sources/config
599     if [[ -d ${CONFIGDIR} ]]; then
600         NEWNAME=${CONFIGDIR}/$(basename "$1" | sed -e "s/^\.//")
601         mv "$1" $NEWNAME
602         ln -s $NEWNAME $1
603     else
604         echo "Can not find ${CONFIGDIR}"
605     fi
606 }
607
608 ######################################################################
609 # Track uncommited files (I presume this is very ugly from a real git
610 # user perspective)
611
612 function git-fm () {
613     CURRENT_DIR=$(pwd)
614     NB_SUBDIR=0
615
616     for i in $(find -name ".git"); do
617         NB_SUBDIR=$((NB_SUBDIR+1))
618         cd ${CURRENT_DIR}/$(dirname $i)
619         NB_MODIFIED=$(git status | grep modified | wc -l)
620         if [[ ${NB_MODIFIED} -gt 0 ]]; then
621             echo "$(dirname $i) (${NB_MODIFIED})"
622             git status | grep modified \
623                 | sed -e "s/^#\t/    /" | sed -e "s/modified: *//"
624         fi
625     done
626
627     cd ${CURRENT_DIR}
628
629     echo "Visited ${NB_SUBDIR} directories."
630 }
631
632 ######################################################################
633 # Commits all directories under git
634
635 alias git-ca="echo Are you sure?"
636
637 function git-ca () {
638     ORIGINAL_PWD=${PWD}
639     UNCOMMITTED=""
640     for d in $(find ${PWD} -name ".git"  | sed -e "s/\.git$//"); do
641         cd $d
642         NB_MODIFIED=$(git status | grep modified | wc -l)
643         if [[ ${NB_MODIFIED} -gt 0 ]]; then
644             if [[ $(pwd) =~ ${NO_AUTOMATIC_GIT_COMMIT} ]]; then
645                 UNCOMMITTED="${UNCOMMITTED} $(pwd)"
646             else
647                 echo $(pwd)" (${NB_MODIFIED} modified file(s))"
648                 git commit -a -m "Automatic commit" | grep -v ^#
649             fi
650             # git gc
651         fi
652     done
653
654     cd ${ORIGINAL_PWD}
655
656     if [[ ${UNCOMMITTED} ]]; then
657         echo "** WARNING: Did not automatically commit${UNCOMMITTED}"
658     fi
659 }
660
661 ######################################################################
662 # Backups all git directories into an encrypted backup file located
663 # either on the usb key or the SD card (in that order) if they can be
664 # mounted.
665
666 function git-backup () {
667
668     BACKUPDIR=/mnt/key
669
670     mount ${BACKUPDIR} 2> /dev/null
671
672     if [[ ! $(mount | grep ${BACKUPDIR}) ]]; then
673         BACKUPDIR=/mnt/sd
674         mount ${BACKUPDIR}
675     fi
676
677     if [[ $(mount | grep ${BACKUPDIR}) ]]; then
678         echo "Mounted ${BACKUPDIR}"
679     else
680         echo "Could not mount the backup directory"
681         return 1
682     fi
683
684     RESULT=${BACKUPDIR}/gitbackup-$(date +%F-%H%M%S).tgz.mc
685
686     tar zcvf - $(find ${HOME}/ -name .git) \
687         | mcrypt -f ${HOME}/private/mcrypt.key > ${RESULT}
688
689     if [[ -f ${RESULT} ]]; then
690         ls -lh ${RESULT}
691     else
692         echo "Could not create the backup!"
693         return 1
694     fi
695
696     sync
697
698     umount ${BACKUPDIR} && echo "Umounted ${BACKUPDIR}"
699 }
700
701 ######################################################################
702 # Downloads torrents located in ${BT_DIR}/torrents/ and puts the
703 # result in the ${BT_DIR}
704
705 function bt () {
706     if [[ ${BT_DIR} ]]; then
707         if [[ -d "${BT_DIR}/torrents" ]]; then
708             if [[ "$1" ]]; then
709                 mv "$1" ${BT_DIR}/torrents
710             fi
711             if [[ "$(ps auxwww | grep btlaunchmanycurses | grep -v grep)" ]]; then
712                 echo "A client is already running."
713             else
714                 cd ${BT_DIR} && screen btlaunchmanycurses torrents --max_upload_rate 32
715             fi
716         else
717             echo "Directory ${BT_DIR}/torrents does not exist."
718         fi
719     else
720         echo "You have to set \$BT_DIR."
721     fi
722 }
723
724 ######################################################################
725 # The complex prompt policy
726
727 export PS1
728
729 if [ "${CONSOLE}" == "yes" ]; then
730     PS1=""
731 else
732
733 # If the login is a standard one (as specified in
734 # IGNORED_PROMPT_LOGIN, which is set in the private bash file), do not
735 # show it. I have IGNORED_PROMPT_LOGIN="^fleuret$".
736
737     if [ ! ${IGNORED_PROMPT_LOGIN} ] || [[ ! ${USER} =~ ${IGNORED_PROMPT_LOGIN} ]]; then
738         IDENT="${USER}"
739     fi
740
741 # If the display is not the main one, make the assumption that the
742 # shell is not running on the localhost, and show the hostname
743
744     [ "${DISPLAY}" != ":0.0" ] && IDENT="${IDENT}@\h"
745
746 # If there is the login or the hostname, add a ":" to the prompt
747
748     [ "${IDENT}" ] && IDENT="${IDENT}:"
749
750 # If we are root, show that in red
751
752     if [[ ${USER} == "root" ]]; then
753         PS1="\[${VT_RED_BG}${VT_WHITE_FG}\]${IDENT}\w\[${VT_RESET}\] "
754     else
755         PS1="\[${VT_WHITE_BG}${VT_BLACK_FG}\]${IDENT}\w\[${VT_RESET}\] "
756     fi
757
758 # In an xterm, show the hostname and path in the title bar, highlight
759 # the prompt
760
761     # [ "${TERMS_WITH_BAR}" ] || TERMS_WITH_BAR="^xterm|screen$"
762
763     # if [[ "${TERM}" =~ "${TERMS_WITH_BAR}" ]]; then
764         # PS1="\[${VT_SET_TITLE}shell@\h (\w)${VT_END_TITLE}${VT_WHITE_BG}\]${IDENT}\w\[${VT_RESET}\] "
765     # else
766         # PS1="\[${VT_WHITE_BG}\]${IDENT}\w\[${VT_RESET}\] "
767     # fi
768
769 fi
770
771 ######################################################################
772 # This implements a local history. If we are in a directory containing
773 # a writable local history file, we add the last line of the global
774 # history to it.
775
776 LOCAL_HISTORY_FILE=".local_bash_history"
777
778 function keep_local_history () {
779     if [[ -w "${LOCAL_HISTORY_FILE}" ]]; then
780         history 1 | sed -e 's/^ *[0-9]* *//' >> ${LOCAL_HISTORY_FILE}
781         TMP=$(mktemp /tmp/lh.XXXXXX)
782         \chmod 600 ${TMP}
783         uniq < ${LOCAL_HISTORY_FILE} | tail -${HISTSIZE} > ${TMP}
784         # mv would replace a symbolic link, while cp keeps it
785         \cp ${TMP} ${LOCAL_HISTORY_FILE}
786         \rm ${TMP}
787         LOCAL_HISTORY_HINT="* "
788     else
789         LOCAL_HISTORY_HINT=""
790     fi
791 }
792
793 PS1="\[${VT_WHITE_BG}\]\${LOCAL_HISTORY_HINT}\[${VT_RESET}\]${PS1}"
794
795 ######################################################################
796 # Switch off the history
797
798 function histfile_cue () {
799     if [[ ! "${HISTFILE}" == "${HOME}/.bash_history" ]]; then
800         HISTORY_CUE="[${HISTFILE}]"
801     else
802         HISTORY_CUE=""
803     fi
804 }
805
806 PS1="\[${VT_YELLOW_BG}\]\${HISTORY_CUE}\[${VT_RESET}\]${PS1}"
807
808 ######################################################################
809 # The dus command is available on my web site
810 #
811 # git clone http://fleuret.org/git/dus/
812
813 alias dus='dus -f -i'
814
815 ######################################################################
816 # The finddup command is available on my web site
817 #
818 # git clone http://fleuret.org/git/finddup/
819
820 # alias finddup='finddup -p0d'
821 alias finddup='finddup -p'
822
823 ######################################################################
824 # Selector based history
825 #
826 # The selector command is available on my web site
827 #
828 # git clone http://fleuret.org/git/selector/
829
830 function selector-history () {
831     selector --bash -u -c 7,4,0,3 -q <(history)
832 }
833
834 # M-r puts the selected history line in place of the current one
835
836 bind '"\C-[r":"\C-a\C-kselector-history\C-m"'
837
838 # M-t appends the selected history line and the end of the current one
839
840 bind '"\C-[t":"\C-a\C-kselector-history\C-m\C-a\C-y\C-e"'
841
842 ######################################################################
843 # Maintain a list of visited directories and provide a selector-based
844 # command to go back to any of them.
845
846 export SELECTOR_CD_HISTORY
847
848 [[ "${SELECTOR_CD_HISTORY}" ]] || \
849     SELECTOR_CD_HISTORY=${HOME}/.selector-cd-history
850
851 function selector-cd-search () {
852     PATH_TEMP=$(mktemp /tmp/selector-cd-path.XXXXXX)
853     selector -t "cd" -l 10000 -d -i -o ${PATH_TEMP} -q ${SELECTOR_CD_HISTORY}
854     cd "$(cat ${PATH_TEMP} | sed -e 's!~!'${HOME}'!')"
855     \rm ${PATH_TEMP}
856 }
857
858 function selector-cd () {
859     if [[ -z "$1" ]]; then
860         cd
861     else
862         cd "$1"
863     fi
864     echo $PWD | sed -e "s!${HOME}!~!" >> ${SELECTOR_CD_HISTORY}
865 }
866
867 alias cd=selector-cd
868
869 # M-c provides a dynamic list of directories to cd into
870
871 bind '"\C-[c":"\C-a\C-kselector-cd-search\C-m"'
872
873 ######################################################################
874 # And we avoid to put in the history the use of the selector, which we
875 # do too often
876
877 HISTIGNORE="${HISTIGNORE}:selector-history"
878
879 ######################################################################
880
881 function selector-printer () {
882     TMP=$(mktemp /tmp/selector-printer.XXXXXX)
883     selector -o ${TMP} <(lpstat -a | awk '{print $1}')
884     export PRINTER=$(cat ${TMP})
885     echo "PRINTER=${PRINTER}"
886     rm -f ${TMP}
887     lpq
888 }
889
890 ######################################################################
891
892 function prompt_command () {
893 # save the history after every command to avoid loosing some when
894 # multiple shells are open
895     history -a
896 # and the local histories system defined above
897     keep_local_history
898 # and the history cue
899     histfile_cue
900 }
901
902 PROMPT_COMMAND="prompt_command"
903
904 ######################################################################
905
906 # Displaying the timezone if it is set
907
908 if [[ ${TZ} ]]; then
909     echo "${VT_BOLD}${VT_GREEN_FG}Time zone is ${TZ}.${VT_RESET}"
910 fi
911
912 ######################################################################