edit function and some minor stuff
[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 --multi --preview 'vicat {1}' | 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
24
25 #top10 commands
26 top10() { history | awk '{a[$2]++ } END{for(i in a){print a[i] " " i}}' | sort -rn | head; }
27 # goes up by the number of directories
28 up() {
29   local d=""
30   limit=$1
31   for ((i=1 ; i <= limit ; i++)); do
32     d=$d/..
33   done
34   d=$(echo $d | sed 's/^\///')
35   if [[ -z "$d" ]]; then
36     d=..
37   fi
38   cd $d
39 }
40 # ARCHIVE EXTRACTOR {{{
41 extract() {
42   clrstart="\033[1;34m"  #color codes
43   clrend="\033[0m"
44
45   if [[ "$#" -lt 1 ]]; then
46     echo -e "${clrstart}Pass a filename. Optionally a destination folder. You can also append a v for verbose output.${clrend}"
47     exit 1 #not enough args
48   fi
49
50   if [[ ! -e "$1" ]]; then
51     echo -e "${clrstart}File does not exist!${clrend}"
52     exit 2 #file not found
53   fi
54
55   if [[ -z "$2" ]]; then
56     DESTDIR="." #set destdir to current dir
57   elif [[ ! -d "$2" ]]; then
58     echo -e -n "${clrstart}Destination folder doesn't exist or isnt a directory. Create? (y/n): ${clrend}"
59     read response
60     #echo -e "\n"
61     if [[ $response == y || $response == Y ]]; then
62       mkdir -p "$2"
63       if [ $? -eq 0 ]; then
64         DESTDIR="$2"
65       else
66         exit 6 #Write perms error
67       fi
68     else
69       echo -e "${clrstart}Closing.${clrend}"; exit 3 # n/wrong response
70     fi
71   else
72     DESTDIR="$2"
73   fi
74
75   if [[ ! -z "$3" ]]; then
76     if [[ "$3" != "v" ]]; then
77       echo -e "${clrstart}Wrong argument $3 !${clrend}"
78       exit 4 #wrong arg 3
79     fi
80   fi
81
82   filename=`basename "$1"`
83
84   #echo "${filename##*.}" debug
85
86   case "${filename##*.}" in
87     tar)
88       echo -e "${clrstart}Extracting $1 to $DESTDIR: (uncompressed tar)${clrend}"
89       tar x${3}f "$1" -C "$DESTDIR"
90       ;;
91     gz)
92       echo -e "${clrstart}Extracting $1 to $DESTDIR: (gip compressed tar)${clrend}"
93       tar x${3}fz "$1" -C "$DESTDIR"
94       ;;
95     tgz)
96       echo -e "${clrstart}Extracting $1 to $DESTDIR: (gip compressed tar)${clrend}"
97       tar x${3}fz "$1" -C "$DESTDIR"
98       ;;
99     xz)
100       echo -e "${clrstart}Extracting  $1 to $DESTDIR: (gip compressed tar)${clrend}"
101       tar x${3}f -J "$1" -C "$DESTDIR"
102       ;;
103     bz2)
104       echo -e "${clrstart}Extracting $1 to $DESTDIR: (bzip compressed tar)${clrend}"
105       tar x${3}fj "$1" -C "$DESTDIR"
106       ;;
107     zip)
108       echo -e "${clrstart}Extracting $1 to $DESTDIR: (zipp compressed file)${clrend}"
109       unzip "$1" -d "$DESTDIR"
110       ;;
111     rar)
112       echo -e "${clrstart}Extracting $1 to $DESTDIR: (rar compressed file)${clrend}"
113       unrar x "$1" "$DESTDIR"
114       ;;
115     7z)
116       echo -e  "${clrstart}Extracting $1 to $DESTDIR: (7zip compressed file)${clrend}"
117       7za e "$1" -o"$DESTDIR"
118       ;;
119     *)
120       echo -e "${clrstart}Unknown archieve format!"
121       exit 5
122       ;;
123   esac
124 }
125 #}}}
126
127
128
129 # REMIND ME, ITS IMPORTANT! {{{
130 # usage: remindme <time> <text>
131 # e.g.: remindme 10m "omg, the pizza"
132 remindme() {
133   if [[ "$#" -lt 2 ]]; then
134     echo -e "Usage: remindme [time] '[message]'"
135     echo -e "Example: remindme 50s 'check mail'"
136     echo -e "Example: remindme 10m 'go to class'"
137     #exit 0 #not enough args
138   fi
139   if [[ "$#" -gt 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 #more than enough args
144   fi
145   if  [[ "$#" == 2 ]]; then
146     sleep $1 && notify-send -t 15000 "$2" & echo 'Reminder set'
147   fi
148   }
149
150 #}}}
151
152
153
154 pacget () { pacman -Slq | fzf --multi --preview 'pacman -Si {1}' | xargs -ro sudo pacman -S ;}
155 pacdel () { pacman -Qq | fzf --multi --preview 'pacman -Qi {1}' | xargs -ro sudo pacman -Rns ;}