summaryrefslogtreecommitdiffstats
path: root/.zsh/rc/30functions
blob: 3250bf96f11cf6fc3e1598b6b77f24ad8874658e (plain)
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
#--------------------
# Functions 
#--------------------

# always /etc/rc.d/blub is a bit too much work ;)
rc() {
	sudo /etc/rc.d/$1 $2
}

# usefull if you have more machines and want to easily update the config here
update_zshrc() {
  old_pwd="$PWD"
  builtin cd "$HOME/.zsh" 
  wget http://flo.server-speed.net/stuff/dotfiles/zshrc -O $HOME/.zshrc
  wget http://flo.server-speed.net/stuff/dotfiles/zsh/filelist -O /tmp/zsh_filelist
  wget -B "http://flo.server-speed.net/stuff/dotfiles/zsh/" -i /tmp/zsh_filelist -x --cut-dirs=3 -nH
  rm /tmp/zsh_fileslist
  rm **/index.html
  builtin cd "$old_pwd"
  exec zsh
}

# add current time to a file
mbk() {
	mv -b "${1}" "$(echo $1 | sed "s/\./_`date +%Y-%m-%d_%T`./")"
}

# easily make backups 
bk() {
	cp -b "${1}" "$(echo $1 | sed "s/\./_`date +%Y-%m-%d_%T`./")"
}

# chpwd () => a function which is executed whenever the directory is changed
chpwd() {
  ls
}

# idea by Gigamo http://bbs.archlinux.org/viewtopic.php?pid=478094#p478094
ls () {
  /bin/ls -rlhbtF --color=auto $@ &&
  echo "${PURPLE}Files: ${BLUE}$(/bin/ls -l $@ | grep -v "^[l|d|total]" | wc -l) ${GREEN}--- ${PURPLE}Directories: ${BLUE}$(/bin/ls -l $@ | grep "^d" | wc -l)${NC}"
}

password() {
  if [ -z "$1" ]; then 
    count=8
  else
    count="$1"
  fi
  < /dev/urandom tr -dc A-Za-z0-9_ | head -c$count
}

# jump between directories
# Copyright 2005 Nikolai Weibull <nikolai@bitwi.se>
# notice: option AUTO_PUSHD has to be set
d(){
 emulate -L zsh
  autoload -U colors
  local color=$fg_bold[blue]
  integer i=0
  dirs -p | while read dir; do
    local num="${$(printf "%-4d " $i)/ /.}"
    printf " %s  $color%s$reset_color\n" $num $dir
    (( i++ ))
  done
  integer dir=-1
  read -r 'dir?Jump to directory: ' || return
  (( dir == -1 )) && return
  if (( dir < 0 || dir >= i )); then
    echo d: no such directory stack entry: $dir
    return 1
  fi
  cd ~$dir
}

# from the grml zshrc iirc
hglob() {
  echo -e "
      /      directories
      .      plain files
      @      symbolic links
      =      sockets
      p      named pipes (FIFOs)
      *      executable plain files (0100)
      %      device files (character or block special)
      %b     block special files
      %c     character special files
      r      owner-readable files (0400)
      w      owner-writable files (0200)
      x      owner-executable files (0100)
      A      group-readable files (0040)
      I      group-writable files (0020)
      E      group-executable files (0010)
      R      world-readable files (0004)
      W      world-writable files (0002)
      X      world-executable files (0001)
      s      setuid files (04000)
      S      setgid files (02000)
      t      files with the sticky bit (01000)
   print *(m-1)          # Dateien, die vor bis zu einem Tag modifiziert wurden.
   print *(a1)           # Dateien, auf die vor einem Tag zugegriffen wurde.
   print *(@)            # Nur Links
   print *(Lk+50)        # Dateien die ueber 50 Kilobytes grosz sind
   print *(Lk-50)        # Dateien die kleiner als 50 Kilobytes sind
   print **/*.c          # Alle *.c - Dateien unterhalb von \$PWD
   print **/*.c~file.c   # Alle *.c - Dateien, aber nicht 'file.c'
   print (foo|bar).*     # Alle Dateien mit 'foo' und / oder 'bar' am Anfang
   print *~*.*           # Nur Dateien ohne '.' in Namen
   chmod 644 *(.^x)      # make all non-executable files publically readable
   print -l *(.c|.h)     # Nur Dateien mit dem Suffix '.c' und / oder '.h'
   print **/*(g:users:)  # Alle Dateien/Verzeichnisse der Gruppe >users<
   echo /proc/*/cwd(:h:t:s/self//) # Analog zu >ps ax | awk '{print $1}'<"
}

# let vim highlight any file for us and save it as html
2html() {
  vim -n -c ':syntax on' -c ':so $VIMRUNTIME/syntax/2html.vim' -c ':wqa' $1 > /dev/null 2> /dev/null
}

# download any package's PKGBUILD and go into that dir
yd(){
  pbget --aur $1 
  builtin cd $1 
}

# some script to make this damn agent work easier
if [ "x$DISPLAY" != "x" ]; then
  source ${HOME}/bin/gpg-agent.sh
fi

# yeah I know you shouldn't use it...
google(){
	firefox "http://www.google.com/search?&num=100&q=$*" &
	disown firefox
}

# swaps 2 files
swap() {
  if [ "$1" = "" ] || [ "$2" = "" ] || [ "$1" = "-h" ]; then 
    echo -e "${blue}Usage:$NC swap <file> <file>";
    echo -e "Swaps files";
    return 1
  fi
  if [ -f $1 ] && [ -f $2 ]; then
    local TMPFILE=tmp.$$
    mv "$1" $TMPFILE
    mv "$2" "$1"
    mv $TMPFILE "$2"
  else
    echo -e "${RED}Error:$NC One or more files don't exist"
    return 1  
  fi
}

# get your internal and external IPs
# also found somewhere on the net
my_ip() {
  MY_IP=$(/sbin/ifconfig eth0 | awk '/inet/ { print $2 } ' | \
    sed -e s/addr://)
  MY_ISP=$(curl www.wieistmeineip.de | awk '(/[0-9]?[0-9]?[0-9]\.[0-9]?[0-9]?[0-9]\.[0-9]?[0-9]?[0-9]\.[0-9]?[0-9]?[0-9]/) {print}' | awk 'gsub(/[>||<]/," ")' | awk '{print $3}')
}

ii() {
  echo -e "\nUser ${RED}"${USER:-"N/A"}"$NC on "${RED}${HOST:-"N/A"}"$NC" 
  echo -e "\n${RED}Additionnal information:$NC " ; uname -a
  echo -e "\n${RED}Users logged on:$NC " ; w -h
  echo -e "\n${RED}Current date:$NC " ; date
  echo -e "\n${RED}Machine stats:$NC " ; uptime
  echo -e "\n${RED}Memory stats:$NC " ; free
  my_ip 2>&- ;
  echo -e "\n${RED}Local IP Address:$NC" ; echo ${MY_IP:-"N/A"}
  echo -e "\n${RED}ISP Address:$NC" ; echo ${MY_ISP:-"N/A"}
  echo
}

# short info to display when opening a new shell
short_ii() {
  echo -e "\nUser ${RED}"${USER:-"N/A"}"$NC on "${RED}${HOST:-"N/A"}"$NC" 
  echo -e "\n${RED}Machine stats:$NC " ; uptime
  echo
}

# actually it converts any mp4 or flv to mp3...
youtube2mp3() {
	for file in "$@"
	do 
   	if [ -f "${file}" ] ; then
			title=$(echo ${file} | sed 's/\(.*\)\..*/\1/')
      case $(file -b --mime-type "${file}") in
				video/mp4)
					faad "${file}" &&
					lame "${title}.wav" "${title}.mp3" &&
					rm "${title}.wav" 
				;;
				video/x-flv)
					ffmpeg -i "${file}" -vn -acodec copy "${title}.mp3"
				;;
				*)
					echo -e "${RED}Error:$NC No rule how to convert \"${file}\""
				;;
			esac
		else
      echo -e "${RED}Error:$NC \"${file}\" doesn't exist"
    fi
	done
}