SHARE
TWEET

Dot expansion (cd ...<enter|tab> ../../)

bac0n Jul 17th, 2018 (edited) 138 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. # Expand dots (~/.bash_aliases).
  3. #
  4. # example:
  5. # cd ... -> ../../
  6. # cd .... -> ../../../
  7. # cd /usr/local/bin/.../bin/.../bin -> /bin
  8. #
  9. if [ -n "$PS1" ]; then
  10. cd(){
  11.     local dir="${@:(-1)}" x y z
  12.     while [[ ! -d $dir ]] && [[ ${dir:${#x}} =~ ((^|/)([.]{3,}))(/.*)?$ ]]; do
  13.         x="${dir%%${BASH_REMATCH[0]}*}${BASH_REMATCH[1]}"
  14.         if [[ ! -d $x ]]; then
  15.             y='..'
  16.             for ((z = 3; z <= ${#BASH_REMATCH[3]}; z++)); do
  17.                 y+='/..'
  18.             done
  19.             dir="${dir%%${BASH_REMATCH[0]}*}${BASH_REMATCH[2]}${y}${BASH_REMATCH[4]}"
  20.         fi
  21.     done
  22.     builtin pushd "$dir" > /dev/null && /bin/ls --almost-all --color=auto
  23. }
  24. fi
  25.  
  26. ##
  27.  
  28. #
  29. # bash_completion (/etc/bash_completion.d/cd).
  30. #
  31. # example:
  32. # cd ...<TAB> -> cd ../../
  33. # cd ....<TAB> -> cd ../../../
  34. # cd /usr/local/bin/.../bin/.../bin<TAB> -> cd /usr/local/bin/../../bin/../../bin
  35. #
  36. # This meta-cd function observes the CDPATH variable, so that cd additionally
  37. # completes on directories under those specified in CDPATH.
  38. #
  39. _cd()
  40. {
  41.     local cur prev words cword
  42.     _init_completion || return
  43.  
  44.     local IFS=$'\n' i j k x y z
  45.  
  46.     compopt -o filenames
  47.  
  48.     while [[ ! -d $cur ]] && [[ ${cur:${#x}} =~ ((^|/)([.]{3,}))(/.*)?$ ]]; do
  49.  
  50.         if [[ -n $(compgen -d -- $cur) ]]; then
  51.             if [[ $cur == ..+(.) ]] || [[ $cur == */..+(.) ]]; then
  52.                 COMPREPLY[${#COMPREPLY[@]}]="$cur/"
  53.             fi
  54.             break
  55.         fi
  56.         x="${cur%%${BASH_REMATCH[0]}*}${BASH_REMATCH[1]}"
  57.         if [[ ! -d $x ]]; then
  58.             y='..'
  59.             for ((z = 3; z <= ${#BASH_REMATCH[3]}; z++)); do
  60.                 y+='/..'
  61.             done
  62.             cur="${cur%%${BASH_REMATCH[0]}*}${BASH_REMATCH[2]}${y}${BASH_REMATCH[4]}"
  63.         fi
  64.     done
  65.  
  66.     # Use standard dir completion if no CDPATH or parameter starts with /,
  67.     # ./ or ../
  68.     if [[ -z "${CDPATH:-}" || "$cur" == ?(.)?(.)/* ]]; then
  69.         _filedir -d
  70.         return
  71.     fi
  72.  
  73.     local -r mark_dirs=$(_rl_enabled mark-directories && echo y)
  74.     local -r mark_symdirs=$(_rl_enabled mark-symlinked-directories && echo y)
  75.  
  76.     # we have a CDPATH, so loop on its contents
  77.     for i in ${CDPATH//:/$'\n'}; do
  78.         # create an array of matched subdirs
  79.         k="${#COMPREPLY[@]}"
  80.         for j in $( compgen -d -- $i/$cur ); do
  81.             if [[ ( $mark_symdirs && -h $j || $mark_dirs && ! -h $j ) && ! -d ${j#$i/} ]]; then
  82.                 j+="/"
  83.             fi
  84.             COMPREPLY[k++]=${j#$i/}
  85.         done
  86.     done
  87.  
  88.     _filedir -d
  89.  
  90.     if [[ ${#COMPREPLY[@]} -eq 1 ]]; then
  91.         i=${COMPREPLY[0]}
  92.         if [[ "$i" == "$cur" && $i != "*/" ]]; then
  93.             COMPREPLY[0]="${i}/"
  94.         fi
  95.     fi
  96.     return
  97. }
  98.  
  99. if shopt -q cdable_vars; then
  100.     complete -v -F _cd -o nospace cd pushd
  101. else
  102.     complete -F _cd -o nospace cd pushd
  103. fi
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top