bashrc separated into files
[dotfiles/.git] / .config / bashrc / functions
1 #!/bin/bash
2
3 # transform md anotations into PDF
4 mdpdf () { pandoc "$1".md -t beamer -o "$1".pdf ;}
5 # Get all metadata
6 metadata () { echo "Salida del comando stat" ; stat "$1" ; echo "Salida del comando ls -lisan" ; ls -lisan "$1" ; echo "salida del comando exiftool" ; exiftool "$1" ;}
7 # activates the autocd mode
8 shopt -s autocd
9 edit () { du -a -L "$1" | awk '{print $2}' | fzf | xargs -r "$VISUAL" ;}
10
11 function cdls() {
12     DIR="$*";
13         # if no DIR given, go home
14         if [ $# -lt 1 ]; then
15                 DIR=$HOME;
16     fi;
17     builtin cd "${DIR}" && \
18     # use your preferred ls command
19         ls -F --color=auto
20 }
21
22
23 #vicat is a color and line number cat
24
25 function vicat() {
26   ccat $@ | nl;
27
28 }
29
30
31 #top10 commands
32 top10() { history | awk '{a[$2]++ } END{for(i in a){print a[i] " " i}}' | sort -rn | head; }
33 # goes up by the number of directories
34 up() {
35   local d=""
36   limit=$1
37   for ((i=1 ; i <= limit ; i++)); do
38     d=$d/..
39   done
40   d=$(echo $d | sed 's/^\///')
41   if [[ -z "$d" ]]; then
42     d=..
43   fi
44   cd $d
45 }
46 # ARCHIVE EXTRACTOR {{{
47 extract() {
48   clrstart="\033[1;34m"  #color codes
49   clrend="\033[0m"
50
51   if [[ "$#" -lt 1 ]]; then
52     echo -e "${clrstart}Pass a filename. Optionally a destination folder. You can also append a v for verbose output.${clrend}"
53     exit 1 #not enough args
54   fi
55
56   if [[ ! -e "$1" ]]; then
57     echo -e "${clrstart}File does not exist!${clrend}"
58     exit 2 #file not found
59   fi
60
61   if [[ -z "$2" ]]; then
62     DESTDIR="." #set destdir to current dir
63   elif [[ ! -d "$2" ]]; then
64     echo -e -n "${clrstart}Destination folder doesn't exist or isnt a directory. Create? (y/n): ${clrend}"
65     read response
66     #echo -e "\n"
67     if [[ $response == y || $response == Y ]]; then
68       mkdir -p "$2"
69       if [ $? -eq 0 ]; then
70         DESTDIR="$2"
71       else
72         exit 6 #Write perms error
73       fi
74     else
75       echo -e "${clrstart}Closing.${clrend}"; exit 3 # n/wrong response
76     fi
77   else
78     DESTDIR="$2"
79   fi
80
81   if [[ ! -z "$3" ]]; then
82     if [[ "$3" != "v" ]]; then
83       echo -e "${clrstart}Wrong argument $3 !${clrend}"
84       exit 4 #wrong arg 3
85     fi
86   fi
87
88   filename=`basename "$1"`
89
90   #echo "${filename##*.}" debug
91
92   case "${filename##*.}" in
93     tar)
94       echo -e "${clrstart}Extracting $1 to $DESTDIR: (uncompressed tar)${clrend}"
95       tar x${3}f "$1" -C "$DESTDIR"
96       ;;
97     gz)
98       echo -e "${clrstart}Extracting $1 to $DESTDIR: (gip compressed tar)${clrend}"
99       tar x${3}fz "$1" -C "$DESTDIR"
100       ;;
101     tgz)
102       echo -e "${clrstart}Extracting $1 to $DESTDIR: (gip compressed tar)${clrend}"
103       tar x${3}fz "$1" -C "$DESTDIR"
104       ;;
105     xz)
106       echo -e "${clrstart}Extracting  $1 to $DESTDIR: (gip compressed tar)${clrend}"
107       tar x${3}f -J "$1" -C "$DESTDIR"
108       ;;
109     bz2)
110       echo -e "${clrstart}Extracting $1 to $DESTDIR: (bzip compressed tar)${clrend}"
111       tar x${3}fj "$1" -C "$DESTDIR"
112       ;;
113     zip)
114       echo -e "${clrstart}Extracting $1 to $DESTDIR: (zipp compressed file)${clrend}"
115       unzip "$1" -d "$DESTDIR"
116       ;;
117     rar)
118       echo -e "${clrstart}Extracting $1 to $DESTDIR: (rar compressed file)${clrend}"
119       unrar x "$1" "$DESTDIR"
120       ;;
121     7z)
122       echo -e  "${clrstart}Extracting $1 to $DESTDIR: (7zip compressed file)${clrend}"
123       7za e "$1" -o"$DESTDIR"
124       ;;
125     *)
126       echo -e "${clrstart}Unknown archieve format!"
127       exit 5
128       ;;
129   esac
130 }
131 #}}}
132
133
134
135 # REMIND ME, ITS IMPORTANT! {{{
136 # usage: remindme <time> <text>
137 # e.g.: remindme 10m "omg, the pizza"
138 remindme() {
139   if [[ "$#" -lt 2 ]]; then
140     echo -e "Usage: remindme [time] '[message]'"
141     echo -e "Example: remindme 50s 'check mail'"
142     echo -e "Example: remindme 10m 'go to class'"
143     #exit 0 #not enough args
144   fi
145   if [[ "$#" -gt 2 ]]; then
146     echo -e "Usage: remindme [time] '[message]'"
147     echo -e "Example: remindme 50s 'check mail'"
148     echo -e "Example: remindme 10m 'go to class'"
149     #exit 0 #more than enough args
150   fi
151   if  [[ "$#" == 2 ]]; then
152     sleep $1 && notify-send -t 15000 "$2" & echo 'Reminder set'
153   fi
154   }
155
156 #}}}
157
158
159
160 pacget () { pacman -Slq | fzf --multi --preview 'pacman -Si {1}' | xargs -ro sudo pacman -S ;}
161 pacdel () { pacman -Qq | fzf --multi --preview 'pacman -Qi {1}' | xargs -ro sudo pacman -Rns ;}