summaryrefslogtreecommitdiffstats
path: root/.bashrc
blob: 2212fee7be79a6fca372d0a78cae1a5169182e8f (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
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

# Shell control
export HISTCONTROL=ignorespace:erasedups
export HISTSIZE=100000
export HISTFILESIZE=100000
shopt -s histappend
shopt -s checkwinsize

# User specific aliases and functions
alias rm='rm -i'
alias l='less -N'
alias dotgit='/usr/bin/git --git-dir=$HOME/code/dotfiles.git --work-tree=$HOME'

# Add to path cleanly
local_pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}
local_pathmunge ~/usr/local/bin

# Change to the correct sibling of a parent directory
cd_parent_sibling ()
{
    local sibling_name="$2";
    local curname="";
    local curtop="$PWD";
    local finaltop=""
    while [ ! "$curtop" = "/" ]; do
        if [ -d $(dirname $curtop)/$sibling_name ]; then
            finaltop=$(dirname $curtop)/$sibling_name;
            break;
        fi
        curname=$(basename $curtop)/$curname;
        curtop=$(dirname $curtop);
    done
    if [ ! -z "$finaltop" ]; then
        if [ ! -z "$3" ]; then
            if [ ! -d $finaltop/$curname ]; then
                echo "$finaltop found but $finaltop/$curname not found";
            fi
            finaltop=$finaltop/$curname;
        fi
        if [ -d $finaltop ]; then
            echo "$1 $finaltop"
            $1 $finaltop
        fi
    else
        echo "parent directory with sibling $sibling_name not found"
    fi
}

# Change to build directory
cdb ()
{
    if [ -z "$1" ]; then
        cd_parent_sibling cd build t;
    else
        cd_parent_sibling cd "$1" t;
    fi
}

# Change to source directory
cds ()
{
    if [ -z "$1" ]; then
        cd_parent_sibling cd source t;
    else
        cd_parent_sibling cd "$1" t;
    fi
}

# Push to top of build directory
cdbt ()
{
    if [ -z "$1" ]; then
        cd_parent_sibling pushd build;
    else
        cd_parent_sibling pushd "$1";
    fi
}

# Push to top of source directory
cdst ()
{
    if [ -z "$1" ]; then
        cd_parent_sibling pushd source;
    else
        cd_parent_sibling pushd "$1";
    fi
}

# An emacs 'alias' with the ability to read from stdin; example:
#    echo "hello world" | ef -
ef()
{
    # If the argument is - then write stdin to a tempfile and open the
    # tempfile.
    if [[ "$1" == "-" ]]; then
        tempfile=$(mktemp emacs-stdin-$USER.XXXXXXX --tmpdir)
        cat - > $tempfile
        emacsclient -n -c -e "(progn (find-file \"$tempfile\")
                               (set-visited-file-name nil)
                               (rename-buffer \"*stdin*\" t))
                 " 2>&1 > /dev/null
    else
        emacsclient -n -c "$@"
    fi
}

# Source first available file in list
source_first()
{
    for x in $*; do
        [ -f $x ] && source $x && break
    done
}

# fasd: Jump to directory or use FZF
j() {
    [ $# -gt 0 ] && fasd_cd -d "$*" && return
    local dir
    dir=$(fasd -Rdl "$1" | fzf -1 -0 --no-sort +m) && cd "${dir}" || return 1
}

# Set up command-line key bindings
case "$-" in
    *i*)
        # Interactive shells
        # Set up fzf and fasd
        source_first /usr/share/fzf/shell/key-bindings.bash ~/.fzf.bash
        eval "$(~/usr/local/bin/fasd --init auto)"
        complete -d j

        # Emacs tramp wants a simple prompt that it can recognize
        if [[ "$TERM" = "dumb" ]]; then
            PS1="> "
        fi
        ;;
    *)
        # Non-interactive shells
        ;;
esac