Archives for March, 2009:

Mar posted 20 Mar 2009 and tagged

Delete a local branch:

git branch -D name_of_branch

Pop out

$
git branch -D name_of_branch

Delete a remote branch:

git push origin :name_of_branch

Pop out

$
git push origin :name_of_branch

Remove deleted remote branches from your list:

git remote prune origin

Pop out

$
git remote prune origin

Mar posted 16 Mar 2009 and tagged

I wrote this bash function to print sequential lines starting from a random point in a text file. I use it to display a few lines of a song every time I open a new shell, but it’s general enough for many purposes.

lyrics() { S=$(( $(( RANDOM % $(( $(wc -l < $1)-1 )) )) + 1)) sed -n "$S,$(($S + $(($2-1)) ))"p $1 | awk '{print "+ "$0}' }

Pop out

1
2
3
4
lyrics() {
  S=$(( $(( RANDOM % $(( $(wc -l < $1)-1 )) )) + 1))
  sed -n "$S,$(($S + $(($2-1)) ))"p $1 | awk '{print "+ "$0}'
}

Add the function above to ~/.bash_login, ~/.bash_profile or ~/.bashrc, followed by this line calling it:

lyrics .my_lyrics 3

Pop out

1
lyrics .my_lyrics 3

Arguments: .my_lyrics is the name of the file containing the text, and 3 is the number of sequential lines to print. Here’s an example of what it produces in a shell:

Last login: Mon Mar 16 12:01:03 on ttys000
+ well maybe you should just drink a lot less coffee
+ and never ever watch the ten o'clock news
+ maybe you should kiss someone nice, or lick a rock, or both
heather@macbook:~ $

Mar posted 15 Mar 2009 and tagged

This is a lightly adapted version of Eric Meyer’s CSS Reset Reloaded, which is the best I’ve found so far. I’m posting it here in both expanded and minified form, mostly for my own convenience.

html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, font, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td { margin: 0; padding: 0; border: 0; outline: 0; font-weight: inherit; font-style: inherit; font-size: 100%; font-family: inherit; vertical-align: baseline; } :focus { outline: 0; } body { line-height: 1; color: #000; background: #fff; } ol, ul { list-style: none; } table { border-collapse: separate; border-spacing: 0; } caption, th, td { text-align: left; font-weight: normal; } blockquote:before, blockquote:after, q:before, q:after { content: ""; } blockquote, q { quotes: "" ""; } a { text-decoration: none; }

Pop out

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
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, font, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td {
  margin: 0;
  padding: 0;
  border: 0;
  outline: 0;
  font-weight: inherit;
  font-style: inherit;
  font-size: 100%;
  font-family: inherit;
  vertical-align: baseline;
}
:focus {
  outline: 0;
}
body {
  line-height: 1;
  color: #000;
  background: #fff;
}
ol, ul {
  list-style: none;
}
table {
  border-collapse: separate;
  border-spacing: 0;
}
caption, th, td {
  text-align: left;
  font-weight: normal;
}
blockquote:before, blockquote:after, q:before, q:after {
  content: "";
}
blockquote, q {
  quotes: "" "";
}
a {
  text-decoration: none;
}

And now minified:

html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td{margin:0;padding:0;border:0;outline:0;font-weight:inherit;font-style:inherit;font-size:100%;font-family:inherit;vertical-align:baseline;}:focus{outline:0;}body{line-height:1;color:#000;background:#fff;}ol,ul{list-style:none;}table{border-collapse:separate;border-spacing:0;}caption,th,td{text-align:left;font-weight:normal;}blockquote:before,blockquote:after,q:before,q:after{content:"";}blockquote,q{quotes:"" "";}a{text-decoration:none;}

Pop out

1
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td{margin:0;padding:0;border:0;outline:0;font-weight:inherit;font-style:inherit;font-size:100%;font-family:inherit;vertical-align:baseline;}:focus{outline:0;}body{line-height:1;color:#000;background:#fff;}ol,ul{list-style:none;}table{border-collapse:separate;border-spacing:0;}caption,th,td{text-align:left;font-weight:normal;}blockquote:before,blockquote:after,q:before,q:after{content:"";}blockquote,q{quotes:"" "";}a{text-decoration:none;}

Mar posted 14 Mar 2009 and tagged

Duplicate a table:

mysql> CREATE TABLE duplicate_name SELECT * FROM original_name;

Pop out

1
mysql> CREATE TABLE duplicate_name SELECT * FROM original_name;

Set root password:

mysql> UPDATE mysql.user SET Password = PASSWORD('password') WHERE User = 'root'; mysql> FLUSH PRIVILEGES;

Pop out

1
2
mysql> UPDATE mysql.user SET Password = PASSWORD('password') WHERE User = 'root';
mysql> FLUSH PRIVILEGES;

Dump MySQL database routines only:

mysqldump -u user_name -p --routines --no-data --no-create-db --no-create-info database_name > routines.sql

Pop out

$
mysqldump -u user_name -p --routines --no-data --no-create-db --no-create-info database_name > routines.sql

Limit rows in mysqldump:

mysqldump --where="true LIMIT 10000" -u user_name -p database_name table_name > table_name_10000.sql

Pop out

$
mysqldump --where="true LIMIT 10000" -u user_name -p database_name table_name > table_name_10000.sql

Change table’s character set to utf8:

mysql> ALTER TABLE table_name CONVERT TO CHARACTER SET utf8;

Pop out

1
mysql> ALTER TABLE table_name CONVERT TO CHARACTER SET utf8;

A simple find-and-replace:

mysql> UPDATE table_name SET column_name = REPLACE(column_name, 'find this', 'replace with this');

Pop out

1
mysql> UPDATE table_name SET column_name = REPLACE(column_name, 'find this', 'replace with this');

Mar posted 13 Mar 2009 and tagged

From Allan Odgaard’s excellent Working With History in Bash:

export HISTCONTROL=erasedups export HISTSIZE=10000 shopt -s histappend

Pop out

1
2
3
export HISTCONTROL=erasedups
export HISTSIZE=10000
shopt -s histappend

Mar posted 12 Mar 2009 and tagged , ,

Place in ~/.bash_login, ~/.bash_profile, or ~/.bashrc after changing lines 2 and 3:

my() { user="your_username"; pass="your_password"; if [ $# = 1 ] then mysql -u $user --password=$pass $1; else mysql -u $user --password=$pass --execute='show databases'; mysql -u $user --password=$pass; fi }

Pop out

1
2
3
4
5
6
7
8
9
10
11
my() {
  user="your_username";
  pass="your_password";
  if [ $# = 1 ]
  then
    mysql -u $user --password=$pass $1;
  else
    mysql -u $user --password=$pass --execute='show databases';
    mysql -u $user --password=$pass;
  fi
}

Before:

mysql -u [user] -p ([database])

Pop out

$
mysql -u [user] -p ([database])
Enter password:
mysql >

After:

my ([database])

Pop out

$
my ([database])
mysql >