-
Notifications
You must be signed in to change notification settings - Fork 1
/
.zsh-alias
212 lines (189 loc) · 6.76 KB
/
.zsh-alias
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#
# Configuration is very miniscule but it does the job both for faint tty sessions and in
# interactive colored terminal emulators. If Powerlevel10k is available it is sourced.
# If not fallback prompt is available.
#
# Author: Mikael Henriksson (www.github.com/miklhh)
#
# ------------------------------------------------------------------------------------ #
# -- Settings for ls -- #
# ------------------------------------------------------------------------------------ #
function ls_gnu_linux { # GNU/Linux
ls --classify --color --group-directories-first --human-readable "${@}"
}
function ls_gnu_macos { # MacOS Darwin with GNU utilities installed
gls --classify --color --group-directories-first --human-readable "${@}"
}
function ls_bsd { # BSD ls (boring, can't group directories first)
ls -G "${@}"
}
case "$(uname -s)" in
Linux)
alias ls=ls_gnu_linux
compdef ls_gnu_linux=ls
;;
Darwin)
if command -v gls 1>/dev/null 2>&1; then
alias ls=ls_gnu_macos
compdef ls_gnu_macos=ls
else
alias ls=ls_bsd
compdef ls_bsd=ls
fi
;;
*) # Unknown, default to GNU ls
echo "[ .zsh-alias:${LINENO} ]: Warning: defaulting ls to 'ls_gnu_linux'"
alias ls=ls_gnu_linux
compdef ls_gnu_linux=ls
;;
esac
alias l='ls -l'
alias ll='ls -l -a'
# ------------------------------------------------------------------------------------ #
# -- NeoVim+Tmux integration -- #
# ------------------------------------------------------------------------------------ #
if command -v "nvim" 1>/dev/null; then
if command -v "tmux" 1>/dev/null 2>&1; then
function nvim_in_tmux() {
if [ -z "${TMUX}" ]; then
tmux new-session -d
tmux send-keys "nvim $@" Enter
tmux attach
else
nvim "$@"
fi
}
alias vim=nvim_in_tmux
compdef nvim_in_tmux=nvim
else
alias vim=nvim
fi
else
echo "[ .zsh-alias:${LINENO} ]: Warning: 'nvim' not in \${PATH}"
fi
function tmux_cd_relative {
local target=${1}
cd $(tmux display-message -p -F "#{pane_current_path}" -t "{$target}" || echo ".")
}
alias cdl='tmux_cd_relative left-of'
alias cdr='tmux_cd_relative right-of'
alias cdu='tmux_cd_relative up-of'
alias cdd='tmux_cd_relative down-of'
# ------------------------------------------------------------------------------------ #
# -- Change dir FZF style -- #
# ------------------------------------------------------------------------------------ #
# * Command 'C' : Use FZF to change directory backwards, from the current directory
# * command 'c' : Use FZF to change directory forwards, from the current directory
function generate_backward_dir_list() {
local PARAM="$1"
local REM="$(pwd)"
local PART=""
if [ "$PARAM" = "--exclude" ]; then
while [ ! -z "$REM" ]; do
echo "$PART"
IFS="/" read CUR REM <<< "$REM"
local PART="${PART}${CUR}/"
done
else
while [ ! -z "$REM" ]; do
IFS="/" read CUR REM <<< "$REM"
local PART="${PART}${CUR}/"
echo "$PART"
done
fi
}
function fzf_cd_preview {
fzf --height=40% --reverse --border --info=inline ${1}
}
function fzf_cd_backward {
cd $(generate_backward_dir_list --exclude | fzf_cd_preview --tac || echo .)
}
function fzf_cd_forward {
cd $(fd --hidden --no-ignore-vcs --type d . | fzf_cd_preview || echo .)
}
alias C=fzf_cd_backward
alias c=fzf_cd_forward
# ------------------------------------------------------------------------------------ #
# -- Misc -- #
# ------------------------------------------------------------------------------------ #
# Quick-open files with GNU xdg-open or BSD open
function o {
if command -v xdg-open 1>/dev/null 2>&1; then
# Freedesktop (probably GNU/Linux) environment: GNU 'xargs' with 'xdg-open'
if [ "$#" -eq 0 ]; then
if command -v fzf 1>/dev/null 2>&1; then
fzf --height=15 --print0 |
xargs -r -0 -I"{}" sh -c 'xdg-open "{}" 1>/dev/null 2>&1 &'
else
echo "Error: 'fzf' not in \${PATH}"
fi
else
xdg-open "$1" 1>/dev/null 2>&1
fi
elif command -v open 1>/dev/null 2>&1; then
# MacOS/BSD environment: BSD 'xargs' with BSD 'open'
if [ "$#" -eq 0 ]; then
if command -v fzf 1>/dev/null 2>&1; then
fzf --height=15 --print0 |
xargs -r -0 -I"{}" sh -c 'open "{}" 1>/dev/null 2>&1 &'
else
echo "Error: 'fzf' not in \${PATH}"
fi
else
open "$1" 1>/dev/null 2>&1
fi
else
# No 'xdg-open' or 'open' in $PATH
echo "[ .zsh-alias:${LINENO} ]: Warning: 'xdg-open/open' not in \${PATH}"
fi
}
# Open current directory with xdg-open/open
if command -v xdg-open 1>/dev/null 2>&1; then
# Freedesktop environment: GNU 'xdg-open'
alias op='xdg-open .'
elif command -v open 1>/dev/null 2>&1; then
# MacOS environment: BSD 'open'
alias op='open .'
else
echo "[ .zsh-alias:${LINENO} ]: Warning: 'xdg-open/open' not in \${PATH}"
fi
# Python 3 alias. On certain systems, e.g. Ubuntu, python is not linked to either
# Python 2 nor Python 3
if ! command -v python 1>/dev/null 2>&1; then
alias python='python3'
fi
# On MacOS Darwin: make an alias python->python3
if [ "$(uname -s)" = "Darwin" ]; then
alias python='python3'
fi
# (i)Python calculator
PYTHON_CALC_INIT_CMD='import math; import matplotlib.pyplot as plt; import numpy as np;'
if command -v ipython 1>/dev/null 2>&1; then
# Ipython with Vim keybindings and _without_ QT backend (nice..)
alias calc="ipython -i -c '${PYTHON_CALC_INIT_CMD}'"
else
# Regular python calculator (lame..)
echo "[ .zsh-alias:${LINENO} ]: Warning: 'ipython' not in \${PATH}"
alias calc="python3 -i -c '${PYTHON_CALC_INIT_CMD}'"
fi
# Create a new directory and change to it
function mkdir_and_cd() {
local MKPATH="${1}"
mkdir -p "${MKPATH}"
cd "${MKPATH}"
}
# Command line quickies
alias ca=cargo
alias t=tmux
alias ta='tmux attach -t'
alias g=git
alias m=mkdir_and_cd
function git-fetch-and-prune {
git fetch -p
git branch -r | \
awk '{print $1}' | \
egrep -v -f /dev/fd/0 <(git branch -vv | \
grep origin) | \
awk '{print $1}' | \
xargs git branch -d
}