sbicknel

~/.bashrc

Mar 13th, 2015
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 11.80 KB | None | 0 0
  1. ~/.bashrc:
  2. ----------
  3.  
  4. ...
  5.  
  6. for i in ~/.etc/bash/*; do
  7.     source "$i"
  8. done
  9.  
  10. ~/.etc/bash/bashaliasesrc:
  11. --------------------
  12.  
  13.  
  14. #======================================================================
  15. # This file contains aliases for interactive use. These are for use
  16. # with bindkey's vi-insert mode. See keybindingsrc for bindkey
  17. # vi-command mode commands.
  18. #======================================================================
  19.  
  20. alias src='source ~/.bashrc'
  21. alias vi='TERM=xterm-256color-it gvim -v'
  22. alias vim='TERM=xterm-256color-it gvim -v'
  23. alias pdf='okular'
  24. alias cls='cd;reset'
  25. alias conf='$EDITOR ~/.bashrc ~/.etc/bash/*'
  26. alias doc='cd ~/Documents'
  27. alias x='exit'
  28. if [[ $TERM != dumb ]]; then
  29.     alias ls='ls --color=auto --group-directories-first'
  30. else
  31.     alias ls='ls --group-directories-first'
  32. fi
  33. alias info='pinfo'
  34. alias tmux='TERM=screen-256color tmux'
  35. alias t='todo.sh -t -n -a'
  36. alias tedit='vim ~/Documents/t/todo/{todo,done}.txt'
  37. alias learnperl='cd ~/projects/perl/learning-perl; ./pldev'
  38. alias -- -='cd -'
  39.  
  40. ~/.etc/bash/keybindingsrc:
  41. --------------------------
  42.  
  43.  
  44. #======================================================================
  45. # This has key bindings for vi-command mode for the most part.
  46. #======================================================================
  47.  
  48. # set bash for vi keybindings
  49. set -o vi
  50.  
  51. # clear the screen
  52. # bind -m vi-insert '"\C-l":clear-screen'
  53. bind -m vi-command '",l":clear-screen'
  54.  
  55. # set up z to quit bash
  56. bind -m vi-command -x '"z":exit'
  57.  
  58. # display help for vi-mode
  59. bind -m vi-command -x '",h":pg /home/scott/Documents/b/bash-vi-editing-mode-cheat-sheet.2014-11-01.txt'
  60. # bind -m vi-insert -x '"\C-h":pg /home/scott/Documents/b/bash-vi-editing-mode-cheat-sheet.2014-11-01.txt'
  61.  
  62. # display help for konsole
  63. bind -m vi-command -x '",k":pg /home/scott/Documents/n/notes/konsole-shortcuts.2014-05-04.v001.txt'
  64. #bind -m vi-insert -x '"\c-k":pg /home/scott/Documents/n/notes/konsole-shortcuts.2014-05-04.v001.txt'
  65.  
  66. # source .bashrc
  67. bind -m vi-command -x '",s":src'
  68.  
  69. # edit bash configuration
  70. bind -m vi-command -x '",v":$EDITOR ~/.bashrc ~/.etc/bash/*'
  71.  
  72. # start file manager
  73. bind -m vi-command -x '",f":krusader 2>/dev/null'
  74.  
  75.  
  76. ~/.etc/bash/functionsrc:
  77. ------------------------
  78.  
  79.  
  80. #======================================================================
  81. # This file contains shell functions that are more complex than simple
  82. # aliases. These are for interactive use.
  83. #======================================================================
  84.  
  85.  
  86. #======================================================================
  87. # make a list of files in subdirectories in a pager. Optionally use
  88. # find to locate specific files in all subdirectories.
  89. #======================================================================
  90. function lst () {
  91.     if [[ $# -eq 0 ]]; then
  92.     ls ./* | $PAGER
  93.     else
  94.     local files=( $(find . -name "*${*}*" -type f 2>/dev/null | sort) )
  95.     local promptLines=$(echo -e "$PS1" | wc -l)
  96.     if [[ ${#files[@]} -ge $(( LINES - promptLines )) ]]; then
  97.         for f in "${files[@]}"; do
  98.         echo $f
  99.         done | $PAGER
  100.     else
  101.         for f in "${files[@]}"; do
  102.         echo $f
  103.         done
  104.     fi
  105.     unset f
  106.     fi
  107. } 2>/dev/null
  108.  
  109.  
  110. #======================================================================
  111. # Display a hex dump of a file in the default pager
  112. #======================================================================
  113. function hex () {
  114.     if [[ $# -gt 1 ]]; then
  115.     printf '%q: too many parameters\n' hex >&2
  116.     return 1
  117.     fi
  118.     if [[ $# -eq 0 ]]; then
  119.     printf '%q: file name required\n' hex >&2
  120.     return 1
  121.     fi
  122.     if ! [[ -f $1 ]]; then
  123.     printf '%q: file not found: %q\n' hex "$1" >&2
  124.     return 1
  125.     fi
  126.     hexdump -C "$1" | $PAGER
  127. }
  128.  
  129.  
  130. #======================================================================
  131. # sizeof - show byte size of one or more files
  132. #======================================================================
  133. function sizeof () {
  134.     if [[ $# -eq 1 ]]; then
  135.     stat -c %s "$1"
  136.     else
  137.     for f in "$@"; do
  138.         [[ -f $f ]] && printf '%8d: %s\n' "$(stat -c %s "$f")" "$f"
  139.     done
  140.     fi
  141. }
  142.  
  143.  
  144.  
  145. #======================================================================
  146. # Format a file (or standard input) and display it or page it if
  147. # necessary
  148. #     pg [-n] [file]
  149. #        -n add line numbers to output
  150. #======================================================================
  151. pg () {
  152.  
  153.     local    ncols=0                    # columns used for line numbering
  154.     local -r promptLines=$(echo -e "$PS1" | wc -l)  # number of lines taken by PS1 prompt
  155.  
  156.     # capture standard input
  157.     # huge files can, of course, fill all available memory
  158.     while IFS='' read -r -t 0.1 line; do
  159.     if ! [[ -v pipeFile ]]; then
  160.         if [[ "$1" = '-n' ]]; then
  161.         ncols=8
  162.         fi
  163.         local -a pipeFile   # array of lines from piped file
  164.         local inc       # amount to increment lineCount each pass through loop
  165.         local lineCount # total number of formatted lines of output
  166.     fi
  167.  
  168.     # add current text line to list of lines, determine how many formatted lines
  169.     # will result from it, and increment the total number of lines by that many
  170.     pipeFile=( "${pipeFile[@]}" "$line" )
  171.     inc=$(fmt -s -w $(( COLUMNS - ncols )) <<< $line | wc -l)
  172.     lineCount=$(( lineCount + inc ))
  173.     done
  174.  
  175.     # set up line numbering
  176.     if [[ "$1" = '-n' ]]; then
  177.     ncols=8
  178.     local -A num
  179.     num[cat]='-n'   # parameter for turning on line numbering in cat
  180.     num[less]='-N'  # parameter for turning on line numbering in less
  181.     shift
  182.     fi
  183.  
  184.     # if taking input from a file
  185.     if [[ $# -gt 0 ]]; then # is there a file names to process?
  186.     if [[ -f "$1" ]]; then  # and is it a regular file?
  187.  
  188.         # format file and feed it to either less or cat
  189.         fmt -s -w $(( COLUMNS - ncols )) "$1" |
  190.         if [[ $(fmt -s -w $(( COLUMNS - ncols )) "$1" | wc -l) \
  191.         -gt $(( LINES - promptLines )) ]]; then
  192.         command less -R ${num[less]}
  193.         else
  194.         command cat ${num[cat]}
  195.         fi
  196.     elif [[ -d "$1" ]]; then
  197.         printf '%s: %s: Is a directory\n' 'pg' "$1" >&2
  198.     else
  199.         printf '%s: %s: No such file or directory\n' 'pg' "$1" >&2
  200.     fi
  201.     elif [[ -v pipeFile ]]; then # taking input from standard input
  202.     for line in "${pipeFile[@]}"; do
  203.         echo "$line"
  204.     done |
  205.     fmt -s -w $(( COLUMNS - ncols )) |
  206.     if [[ lineCount -gt $(( LINES - promptLines )) ]]; then
  207.         command less -R ${num[less]}
  208.     elif [[ lineCount -gt 0 ]]; then
  209.         command cat ${num[cat]}
  210.     else
  211.         :
  212.     fi
  213.     fi
  214. }
  215.  
  216.  
  217. #======================================================================
  218. # Customize how which operates
  219. #======================================================================
  220. function which () {
  221.     { alias; declare -f; } | /usr/bin/which --tty-only --read-alias \
  222.     --read-functions --show-tilde --show-dot "$@"
  223. }
  224. export -f which
  225.  
  226.  
  227. #======================================================================
  228. # Run perl interactively
  229. #======================================================================
  230. iperl () {
  231.     clear
  232.     command perl -de 42
  233.     clear
  234. }
  235.  
  236. up() {
  237.     #======================================================================
  238.     # move up the directory tree
  239.     # usage: up [N]
  240.     #   N is an integer (defaults to 1)
  241.     #
  242.     # silently exits if non-numeric input given
  243.     # translates signed integers to absolute values
  244.     # ignores all but the first parameter
  245.     #
  246.     # exit codes:
  247.     #    0: success
  248.     #    1: invalid input
  249.     #    2: unable to change directory
  250.     #  255: nothing to do
  251.     #======================================================================
  252.  
  253.     # ensure PWD points to the real working directory
  254.     # side-effect resets global PWD to working directory
  255.     if ! command cd -P . && [[ $PWD != $(command realpath .) ]]; then
  256.     printf '$PWD spoofed to: %s\nReal working directory is: %s\n' \
  257.         "$PWD" "$(command realpath .)" >&2
  258.     local PWD=$(command realpath .)
  259.     fi
  260.  
  261.     # convert parameter to an absolute value, if possible, or return error code
  262.     local levels="${1:-1}"  # set the number of levels up the directory tree to traverse
  263.     levels=${levels#*-}     # strip leading negative sign
  264.     levels=${levels#*+}     # strip leading positive sign
  265.     if [[ $levels =~ [^[:digit:]] ]]; then
  266.     return 1
  267.     fi
  268.  
  269.     local targetDir="$PWD"  # new working directory starts from the current directory
  270.  
  271.     # set targetDir to target directory
  272.     local l
  273.     for (( l=1; l<=levels; l++ )); do
  274.  
  275.     # %/* will produce an empty string in first-level directories.  This handles that case
  276.     if [[ -n "${targetDir%/*}" ]]; then
  277.         targetDir="${targetDir%/*}"
  278.     else # set targetDir to / and break out of loop
  279.         targetDir=/
  280.         break
  281.     fi
  282.     done
  283.  
  284.     # if new working directory is different from current directory, change directories
  285.     if [[ "$targetDir" != "$PWD" ]]; then
  286.     command cd -P "$targetDir" || return 2 # if cd fails
  287.     else
  288.     return -1 # nothing to do (exit code = 255)
  289.     fi
  290. }
  291.  
  292.  
  293. #======================================================================
  294. # customizes the PS1 prompt
  295. #======================================================================
  296. ps1 () {
  297.     export ps1
  298.  
  299.     # is $PWD somewhere within $HOME?
  300.     if [[ $PWD =~ ^$HOME ]]; then
  301.  
  302.     # is this $HOME?
  303.     if [[ "$PWD" = "$HOME" ]]; then
  304.         :
  305.  
  306.     # is $PWD top-level within $HOME?
  307.     elif [[ "$HOME/${PWD##*/}" = "$PWD" ]]; then
  308.         printf '\[\e[0;34m\]/\[\e[1;31m\]%s' "${PWD##*/}"
  309.  
  310.     # must be a lower-level directory within $HOME
  311.     else
  312.         local p=${PWD#*$USER/}
  313.         local dots=''
  314.  
  315.         # substitute single dots for each subdirectory level below $HOME
  316.         while grep / <<< "$p" >/dev/null; do
  317.         p=${p#*/}
  318.         dots="${dots}."
  319.         done
  320.         printf '\[\e[0;34m\]/\[\e[1;31m\]%s\[\e[0;34m\]/\[\e[1;31m\]%s' "$dots" "${PWD##*/}"
  321.     fi
  322.  
  323.     # $PWD is somewhere other than $HOME
  324.     else
  325.     printf '\[\e[0;34m\]/(\[\e[1;31m\]%s\[\e[0;34m\])' "$PWD"
  326.     fi
  327.  
  328.     # is this a regular user?
  329.     if [[ $UID -gt 0 ]]; then
  330.     printf '\[\e[0;30m\] \$\[\e[1;30m\] '
  331.  
  332.     # must be root
  333.     else
  334.     printf '\[\e[0;31m\] \$\[\e[1;30m\] '
  335.     fi
  336. }
  337.  
  338.  
  339. #======================================================================
  340. # list 10 most-used commands
  341. #======================================================================
  342. mostused () {
  343.     history |\
  344.     awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' |\
  345.     grep -v "./" |\
  346.     column -c3 -s " " -t |\
  347.     sort -nr |\
  348.     nl |\
  349.     head -n10
  350. }
  351.  
  352.  
  353. ~/.etc/bash/environmentrc:
  354. ---------------------
  355.  
  356.  
  357. #======================================================================
  358. # This file contains environment variables that apply to all programs
  359. # and to the shell itself. Program-specific environment variables
  360. # should be in program-specific files, instead.
  361. #======================================================================
  362.  
  363. export PAGER='/usr/bin/less'
  364. export LESS='-R'
  365. export VIEWER="$PAGER"
  366. export MAILER='/usr/bin/kmail'
  367. export BROWSER='/usr/bin/firefox'
  368. export EDITOR='/usr/bin/vim'
  369. export VISUAL='/usr/bin/vim'
  370. export FCEDIT='/usr/bin/vim'
  371. export HISTSIZE='1000'
  372. export HISTIGNORE='cd:ls:[bf]g:clear:exit'
  373. export HISTCONTROL='ignoreboth:erasedups'
  374. export PROMPT_COMMAND='history -a; history -n; history -w; export PS1="\n\[\e[0;36m\]\H\[\e[0;34m\]/\[\e[1;31m\]~\[\e[0;36m\]\u$(ps1)"'
  375. export CDPATH='.:~:~/Documents:~/Videos:~/Music:~/Downloads:~/Pictures:~/Desktop:~/Mail:~/Dropbox'
  376. export LINES
  377. export COLUMNS
  378. export COLORTERM=$TERM
  379. export MAN_POSIXLY_CORRECT=1
  380.  
  381.  
  382. ~/.etc/bash/bashoptionsrc:
  383. --------------------------
  384.  
  385. #======================================================================
  386. # This file contains shell options for interactive use.
  387. #======================================================================
  388.  
  389. shopt -s autocd
  390. shopt -s cdable_vars
  391. shopt -s cdspell
  392. shopt -s dirspell
  393. shopt -s checkhash
  394. shopt -s globstar
  395. shopt -s histappend
  396. shopt -s checkwinsize
Advertisement
Add Comment
Please, Sign In to add comment