summaryrefslogtreecommitdiffstats
path: root/.bashrc
blob: 75a05def2b75cb953b3b2b331ee1b1114e575ccf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

[[ -f /etc/bashrc ]] && source /etc/bashrc

# environment settings ********************************************************
export ANDROID_HOME="$HOME/Android/Sdk"
_BASHRC_SOURCED=1
export PYTHONSTARTUP="$HOME/.pythonrc"
export PYTHONPATH="${PYTHONPATH:-$HOME/wsp/python_sandbox}"
export PYTHONDOCS="/usr/share/doc/python2.7-doc/html/"
export RUBYOPT=rubygems
export BROWSER=chrome-launcher
export GOPATH="${GOPATH:-$HOME/wsp/go/binaries}"
export VAGRANT_DEFAULT_PROVIDER=virtualbox
if which nvim >/dev/null 2>&1; then
    export EDITOR=nvim
elif [[ -n "${DISPLAY:-}" ]] && which vimx >/dev/null 2>&1; then
    export EDITOR=vimx
elif [[ -z "${EDITOR:-}" ]]; then
    export EDITOR=vim
fi
[[ -z "${SYSTEMD_EDITOR:-}" ]] && SYSTEMD_EDITOR="${EDITOR}"

[[ "${_PROFILE_SOURCED:-0}" == 0 && -f "${HOME}/.profile" ]] && source "${HOME}/.profile"

# If not running interactively, don't do anything
[[ $- != *i* ]] && return     # non-interactive shell

# reset the shell level if the parent has a different controlling terminal
[[ "${SHLVL:-1}" -gt 1 && "$(ps -o tty= -p$$)" != "$(ps -o tty= -p${PPID:-1})" ]] && SHLVL=1

[[ -d "$PYTHONPATH" ]] && export PATH="$PATH:$PYTHONPATH"

# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace
export HISTCONTROL=erasedups:ignorespace
# single press of escape means cancel
export KEYBOARD_KEY_TIMEOUT_US=1000

# append to the history file, don't overwrite it
#shopt -s histappend # already in /etc/bashrc

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=10000
HISTFILESIZE=100000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS. (already in /etc/bashrc)
#shopt -s checkwinsize

# recognice ** patterns
shopt -s globstar

# make less more friendly for non-text input files, see lesspipe(1)
for lessbin in lesspipe{,.sh}; do
    if which "${lessbin}" >/dev/null 2>&1; then
        eval "$(SHELL=/bin/sh "${lessbin}")"
        break
    fi
done

# Set nice prompt
[[ -f ~/.bash_prompt ]] && source ~/.bash_prompt || :

# Alias definitions.
[[ -f ~/.bash_aliases ]] && source ~/.bash_aliases || :

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if [[ -f /etc/bash_completion ]] && ! shopt -oq posix; then
    source /etc/bash_completion
    complete -cf sudo
fi

# Local overrides:
[[ -f ~/.bash_overrides ]] && source ~/.bash_overrides || :

# Make Ctrl+S work in cli applications (mc).
stty start undef stop undef

function is_fn_defined() {
    [[ -n "$(type -t $1)" && "$(type -t $1)" = function ]]
}

# fzf settings ***********************************************************************************
if ! is_fn_defined __fzfcmd; then
    if command -v fzf-share >/dev/null; then
        source "$(fzf-share)/key-bindings.bash"
    elif [[ -f ~/.fzf.bash ]]; then
        source ~/.fzf.bash
    elif [[ -e /usr/share/fzf/shell/key-bindings.bash ]]; then
        source /usr/share/fzf/shell/key-bindings.bash
    fi
fi
if is_fn_defined __fzfcmd; then
    if which ag >/dev/null 2>&1; then
        export FZF_DEFAULT_COMMAND='ag -g ""'
    fi

    # ********************************************************************************************
    # Add following keybindings for git:
    #    CTRL-G+CTRL-F for files
    #    CTRL-G+CTRL-B for branches
    #    CTRL-G+CTRL-T for tags
    #    CTRL-G+CTRL-R for remotes
    #    CTRL-G+CTRL-H for commit hashes
    # copied from https://junegunn.kr/2016/07/fzf-git/

    # Will return non-zero status if the current directory is not managed by git
    function is_in_git_repo() {
         git rev-parse HEAD > /dev/null 2>&1
    }

    function fzf-down() {
        fzf --height 50% "$@" --border
    }

    function gt() {
         # "Nothing to see here, move along"
         is_in_git_repo || return

         # Pass the list of the tags to fzf-tmux
         # - "{}" in preview option is the placeholder for the highlighted entry
         # - Preview window can display ANSI colors, so we enable --color=always
         # - We can terminate `git show` once we have $LINES lines
        git tag --sort -version:refname | \
            fzf-tmux --multi --preview-window right:70% \
                --preview 'git show --color=always {} | head -'"${LINES}"
    }

    bind '"\er": redraw-current-line'
    bind '"\C-g\C-t": "$(gt)\e\C-e\er"'

    function gf() {
      is_in_git_repo || return
      git -c color.status=always status --short |
      fzf-down -m --ansi --nth 2..,.. \
        --preview '(git diff --color=always -- {-1} | sed 1,4d; cat {-1}) | head -500' |
      cut -c4- | sed 's/.* -> //'
    }

    bind '"\C-g\C-f": "$(gf)\e\C-e\er"'

    function gb() {
      is_in_git_repo || return
      git branch -a --color=always | grep -v '/HEAD\s' | sort |
      fzf-down --ansi --multi --tac --preview-window right:70% \
        --preview 'git log --oneline --graph --date=short --pretty="format:%C(auto)%cd %h%d %s" $(sed s/^..// <<< {} | cut -d" " -f1) | head -'$LINES |
      sed 's/^..//' | cut -d' ' -f1 |
      sed 's#^remotes/##'
    }

    bind '"\C-g\C-b": "$(gb)\e\C-e\er"'

    function gr() {
      is_in_git_repo || return
      git remote -v | awk '{print $1 "\t" $2}' | uniq |
      fzf-down --tac \
        --preview 'git log --oneline --graph --date=short --pretty="format:%C(auto)%cd %h%d %s" {1} | head -200' |
      cut -d$'\t' -f1
    }

    bind '"\C-g\C-r": "$(gr)\e\C-e\er"'

    function gh() {
      is_in_git_repo || return
      git log --date=short --format="%C(green)%C(bold)%cd %C(auto)%h%d %s (%an)" --graph --color=always |
      fzf-down --ansi --no-sort --reverse --multi --bind 'ctrl-s:toggle-sort' \
        --header 'Press CTRL-S to toggle sort' \
        --preview 'grep -o "[a-f0-9]\{7,\}" <<< {} | xargs git show --color=always | head -'$LINES |
      grep -o "[a-f0-9]\{7,\}"
    }

    bind '"\C-g\C-h": "$(gh)\e\C-e\er"'

fi

# vim: set et ts=4 sw=4 tw=98 ft=sh: