]> git.phdru.name Git - dotfiles.git/blob - .shellrc
Feat(recode-filenames-recursive): Allow to omit parameters
[dotfiles.git] / .shellrc
1 #
2 # $HOME/.shellrc
3 #
4
5 # If not running interactively, don't do anything
6 case $- in
7     *i*) ;;
8       *) return ;;
9 esac
10
11 # Stop if non-interactive shell
12 [ -z "$PS1" ] && return
13
14 #if [ -x /usr/bin/dircolors ]; then
15 #   # I don't like these aliases - they work only in command line
16 #   # but not in scripts I run from command line.
17 #   # Color parameters must be passed via environment.
18 #   # So instead I put them in shell scripts in ~/bin.
19 #   alias ls='ls --color=auto'
20 #   alias dir='dir --color=auto'
21 #   alias vdir='vdir --color=auto'
22 #
23 #   alias grep='grep --color=auto'
24 #   alias fgrep='fgrep --color=auto'
25 #   alias egrep='egrep --color=auto'
26 #
27 #   alias dmesg='dmesg --human'
28 #fi
29
30
31 case "$HOME" in
32     /home/*)
33         if [ -L /home ]; then
34             START_DIR="`pwd`" &&
35             cd "$HOME" &&
36             HOME="`pwd`" &&
37             cd "$START_DIR"
38         fi
39     ;;
40 esac
41
42 if test -n "$KSH_VERSION" -o -n "$FCEDIT"; then
43     back() { cd - "$@"; }
44     j() { jobs; }
45 fi
46
47 # clear screen and history, logout
48 chlo() { clear; cd; >$HISTFILE; unset HISTFILE; history -c; logout || exit; }
49
50 if [ -n "$STY" -a "$WINDOW" != 0 ]; then
51     #unset MAILCHECK everywhere except for the 1st GNU screen window
52     unset MAILCHECK
53 fi
54
55 if test -x /usr/bin/git >/dev/null 2>&1; then
56     # chdir to a directory
57     cdgitpath() {
58         if [ $# -ne 1 ]; then
59             echo "Usage: cdgitpath path_key" >&2
60             return 1
61         fi
62
63         path_key="$1"
64         if [ -z "$path_key" ]; then
65             echo "Usage: cdgitpath path_key" >&2
66             return 1
67         fi
68
69         path_url="`git config --get --path $path_key`"
70         if echo "$path_url" | grep -q '^file:/'; then
71             path_url="`echo \"$path_url\" | sed 's@^file:/\+@/@'`"
72         fi
73         cd "$path_url"
74     }
75
76     # See https://stackoverflow.com/a/77266926/7976758
77
78     cd_worktree() {
79         if [ $# -ne 1 ]; then
80             echo "Usage: cd_worktree <branch>" >&2
81             return 1
82         fi
83         path="$(git worktree list | grep -F "$1" | awk '{print $1}')"
84         if [ -n "$path" ]; then
85             cd "$path"
86         else
87             echo "Cannot find path for branch '$1'" >&2
88             return 1
89         fi
90     }
91 fi
92
93 include() {
94     cfg="$1"
95     shift
96     if [ -f "./$cfg" -a -r "./$cfg" ]; then
97         echo "Reading config file \`$cfg'" 1>&2
98         . "./$cfg" "$@"
99     elif [ -f "$HOME/lib/config/$cfg" -a -r "$HOME/lib/config/$cfg" ]; then
100         echo "Reading config file \`$HOME/lib/config/$cfg'" 1>&2
101         . "$HOME/lib/config/$cfg" "$@"
102     else
103         echo "Cannot find config file \`$cfg'" 1>&2
104     fi
105 }
106
107
108 [ "`type -t mc`" = alias ] && unalias mc
109 mc() {
110     if [ -n "$SLOWTERM" ]; then
111         MC_SLOW="--slow"
112     fi
113
114     case "$TERM" in
115         screen*) # screen, screen.rxvt
116             MC_XTERM="-x"
117         ;;
118     esac
119
120     #TMPDIR="$HOME"/tmp
121     #export TMPDIR
122
123     MC_FOUND=NO
124
125     for wrapper in \
126             /usr/local/share/mc/bin/mc-wrapper.sh \
127             /usr/local/libexec/mc/mc-wrapper.sh \
128             /usr/share/mc/bin/mc-wrapper.sh; do
129         if [ -r $wrapper ]; then
130             . $wrapper $MC_SLOW $MC_XTERM "$@"
131             rc=$?
132             MC_FOUND=YES
133             break
134         fi
135     done
136
137     if [ $MC_FOUND = NO ]; then
138         for mc in /usr/local/bin/mc /usr/bin/mc; do
139             if [ -x $mc ]; then
140                 MC_TMP_DIR="${TMPDIR-/tmp}/mc-$USER"
141                 if [ ! -d MC_TMP_DIR="${TMPDIR-/tmp}/mc-$USER" ]; then
142                     MC_TMP_DIR="${TMPDIR-/tmp}"
143                 fi
144                 MC_PWD_FILE="$MC_TMP_DIR/mc.pwd.$$"
145                 mc_type="`mc_type.py $mc`"
146                 if [ "$mc_type" = old ]; then
147                     $mc -P $MC_SLOW $MC_XTERM "$@" > "$MC_PWD_FILE"
148                 elif [ "$mc_type" = new ]; then
149                     $mc -P "$MC_PWD_FILE" $MC_SLOW $MC_XTERM "$@"
150                 else
151                     continue
152                 fi
153                 rc=$?
154                 MC_FOUND=YES
155                 cd "`cat \"$MC_PWD_FILE\"`" && /bin/rm "$MC_PWD_FILE"
156                 break
157             fi
158         done
159     fi
160
161     if [ $MC_FOUND = NO ]; then
162         echo "Cannot find mc-wrapper.sh or mc" >&2
163         rc=1
164     fi
165
166     unset wrapper MC_FOUND MC_PWD_FILE MC_SLOW MC_TMP_DIR MC_XTERM
167
168     return $rc
169 }
170
171
172 mkcd() {
173     if [ $# -ne 1 ]; then
174         echo "Usage: mkcd directory_name" >&2
175     elif [ -d "$1" ]; then
176         cd "$1"
177     elif [ -e "$1" ]; then
178         echo "Error: $1 is a file" >&2
179     else
180         mkdir -p "$1" && cd "$1"
181     fi
182 }
183
184
185 if which tmux >/dev/null 2>&1; then
186     tmux() {
187         case "$TERM" in
188             rxvt)
189                 TERM=rxvt-unicode command tmux "$@"
190             ;;
191
192             *)
193                 command tmux "$@"
194             ;;
195         esac
196     }
197 fi
198
199
200 vim() {
201     command vim "$@"
202     rc=$?
203     cd "`cat \"$HOME/tmp/vim/cwd\"`" && /bin/rm "$HOME/tmp/vim/cwd" &&
204     return $rc
205 }
206
207
208 if [ "$SHLVL" -eq 1 ] && which startx >/dev/null 2>&1; then
209     X() { startx >> .Xserver.log 2>&1; cyr; /bin/rm -f .Xauthority; }
210 fi
211
212 x() { exit; }