-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.functions
158 lines (143 loc) · 3.54 KB
/
.functions
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
#!bin/bash
# This file constains some functions I've created using a bash script for my zsh
# terminal.
#
# More information at: github.com/pblcc/dotfiles
#
# Follow me on Twitter: @pablcorbcon
# This function acts as a simple calculator, so that I don't have to open my
# calculator to calculate basic operations.
#
# Usage:
# calc number1 operation number2
calc() {
local result=""
result="$(printf "scale=10;%s\\n" "$*" | bc --mathlib | tr -d '\\\n')"
if [[ "$result" == *.* ]]; then
printf "%s" "$result" |
sed -e 's/^\./0./' \
-e 's/^-\./-0./' \
-e 's/0*$//;s/\.$//'
else
printf "%s" "$result"
fi
printf "\\n"
}
# Source a configuration file (allways a dotfile) located at ~
#
# Usage:
# config file
#
# Example
# config zshrc == soure ~/.zshrc
config(){
source "$HOME/.$1"
}
# Get the colors of the manual pages
#
# Usage:
# man <command>
man() {
env \
LESS_TERMCAP_mb="$(printf '\e[1;31m')" \
LESS_TERMCAP_md="$(printf '\e[1;31m')" \
LESS_TERMCAP_me="$(printf '\e[0m')" \
LESS_TERMCAP_se="$(printf '\e[0m')" \
LESS_TERMCAP_so="$(printf '\e[1;44;33m')" \
LESS_TERMCAP_ue="$(printf '\e[0m')" \
LESS_TERMCAP_us="$(printf '\e[1;32m')" \
man "$@"
}
# Query the wikipedia from the terminal to see the texts in the screen.
#
# Usage:
# wiki <words>
#
# Example:
# wiki Python programming language
wiki() {
if ! [ -x "$(command -v links)" ]; then
echo "Links package is not installed"
sudo apt install links
fi
if ! [ -x "$(command -v wikipedia2text)" ]; then
echo "Wikipedia to text is not installed"
sudo apt-get install wikipedia2text
fi
wikipedia2text -p "$*"
}
# Create a new directory and move inside it
m() {
mkdir -p "$@"
cd "$@" || exit
}
# Determine tthe size of a file or the total size of a directory
# to avoid complex commands
#
# Usage:
# fs [directory][file]
fs() {
if du -b /dev/null > /dev/null 2>&1; then
local arg=-sbh
else
local arg=-sh
fi
if [[ -n "$@" ]]; then
du $arg -- "$@"
else
du $arg -- .[^.]* *
fi
}
# Start an HTTP server from a directory, optionally specifying
# the port of it
start-server(){
local port="${1:-8000}"
sleep 1
open "https://localhost:$port/"
# check if python is installed and open a socket on it.
if ! [[ -x "$(command -v python3)" ]]; then
echo "Python is not installed"
exit 1
fi
python -c $'\
import SimpleHTTPServer;\
map = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map;\
map[""] = "text/plain";\
for key, value in map.items():\
map[key] = value + ";charset=UTF-8";\
SimpleHTTPServer.test();' "$port"
}
# Run dig and display the most usefull information
#
# Usage:
# digga <words>
digga(){
dig +nocmd "$1" any +multiline +noall +answer
}
# `tre` is a shorthand for `tree` with hidden files and color enabled, ignoring
# the `.git` directory, listing directories first. The output gets piped into
# `less` with options to preserve color and line numbers, unless the output is
# small enough for one screen.
tre() {
tree -aC -I '.git' --dirsfirst "$@" | less -FRNX
}
# This function is used for adding an alias to the .zshrc file. Then it will restart
# ZSH to execute the alias.
#
# Usage: addalias name "command of the alias"
addalias() {
filename="$HOME/alias.zsh"
echo "alias $1='$2'" | tee -a $filename > /dev/null
source "$filename"
}
# Is used to format a given location
format() {
umount "$1"
mkfs.vfat "$1"
}
# Moves to an specific project (located at $1), all
# the projects are located in the ~/projects/ folder
projects() {
cd ~/projects/$1
ls -la
}