VimBashPath

30th May 2017 at 7:27pm
Bash

Taken from a question I answered on StackOverflow https://stackoverflow.com/questions/44268582/bash-prompt-w-path-prefixed-with-single-chars-only/44269076#44269076.

nicolaw@sansa:~/c/e/s/b/archives $ source ~/vimpath.sh
nicolaw@sansa:~/c/e/s/b/archives $ ls -Al
total 1
drwxrwxr-x 3 nicolaw nicolaw 3 May 30 18:43 images
drwxrwxr-x 3 nicolaw nicolaw 3 May 30 19:56 .images
nicolaw@sansa:~/c/e/s/b/archives $ cd images/
nicolaw@sansa:~/c/e/s/b/a/images $ cd letsencrypt-ssl/
nicolaw@sansa:~/c/e/s/b/a/i/letsencrypt-ssl $ cd ../../.images/letsencrypt-ssl/
nicolaw@sansa:~/c/e/s/b/a/.i/letsencrypt-ssl $ pwd
/home/nicolaw/code/eduncan911.github.io/source/blog/archives/.images/letsencrypt-ssl
nicolaw@sansa:~/c/e/s/b/a/.i/letsencrypt-ssl $ cat ~/vimpath.sh
#!/bin/bash

function __prompt_get_dir () {
  local path="${PWD//~/\~}"
  if [[ "$path" = "~" ]] ; then
    __abbrev_path="~"
    return
  fi
  local out=""
  local i=0
  for (( i=0; i<${#path}; i++ )); do
    case "${path:i:2}" in
      \~*) out+="${path:i:1}" ;;
      /\.|/_) out+="${path:i:3}"; ((i+=2)); continue ;;
      /*) out+="${path:i:2}"; continue ;;
      *) continue ;;
    esac
  done
  __abbrev_path="${out%/*}/${path##*/}"
}

__tmp_re="\b__prompt_get_dir\b"
if [[ ! "$PROMPT_COMMAND" =~ $__tmp_re ]] ; then
  export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND; }__prompt_get_dir"
fi
unset __tmp_re
export PS1="\u@\h:\$__abbrev_path $ "

nicolaw@sansa:~/c/e/s/b/a/.i/letsencrypt-ssl $