Comfortable, Color CLI

Friday, January 13

Torn as I now am between my iMac and my laptop (Ubuntu linux), I invariably find myself using both: OS X at the desk, Ubuntu on the couch. My brief tenure in linuxland taught me to embrace the CLI (Command Line Interface), and upon returning to mactopia, I find I use the CLI almost exclusively. (That and VIM. I decided early on that rather than waste time learning yet another GUI editor, I’d take the opportunity to learn VIM and promptly fell in love. I’m typing this in VIM right now, even as my TextMate icon stares up at me from the dock.) For me, Linux and Darwin are sufficiently similar that I’m at home on either and I’ve come to respect their subtle differences.

That said, one thing I really liked about the default bash terminal in Ubuntu was its color, so I sought to bring this feature over to my OS X Terminal. Using ls with color makes things faster—colors can tell you about files in ways that ls -F never could. To enable this in OS X (bash), you’ll need to add some options to your .profile:

export CLICOLOR=1
export TERM=xterm-color

The first line turns on color, and the second ensures that your terminal is declared as color capable. While this will work as advertised, it won’t necessarily look very good if you use a dark background. By default, directories are listed in blue, and while this looks good in Ubuntu it’s just too dark on the Mac. For those of us who rock a black background, you might want to change the ls colors.

export LSCOLORS=gxfxcxdxbxegedabagacad  # cyan directories

In the above example, I’m using cyan for directories; this is a fair compromise and looks nice in both OS X and Ubuntu. If you want to know about each of the color codes above, they’re listed in the ls manual (man ls).

For the complete linux effect on OS X, a green prompt will also be necessary. Without getting in the sordid details of customizing your prompt (it’s a long and arduous tale that I’ll save for another article), I offer this:

export PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$ '

When we put it all together, we get a comfortable and portable CLI that looks quite nice, no matter where you’re logging in from.

# colors
export CLICOLOR=1
export TERM=xterm-color
export LSCOLORS=gxgxcxdxbxegedabagacad  # cyan directories
export PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$ '

Database-agnostic dumps from mysql

Friday, October 21

MySql ships with a program called mysqldump for dumping table definitions and data from a database.

Usage: mysqldump [OPTIONS] database [tables]

There are a several of [OPTIONS], but when I want a nice, compact dump that I can use to create a schema definition, I use the following:

$ mysqldump -dr schema.ddl some_db --compact --compatible=mysql323,no_table_options --add-drop-table

This command results in a fairly database-agnositic schema definition in schema.ddl. There are no comments, no proprietary table or field definitions, and no data. The first option, -d, tells mysqldump to skip the data; -r tells it to output to schema.ddl; the rest of the options are verbose.

Creating new databases from this schema is painless. First, use the mysqladmin program to create a new, empty database:

$ mysqladmin create another_db;

Next, use the mysql client to execute the sql from schema.ddl:

$ mysql another_db < schema.ddl

Notes: For all three programs (mysqldump, mysqladmin, mysql), you can specify a user/pass with -u someuser -p . The commands executed will run as this user. Oh, and the -p option needn’t take an argument—you’ll be prompted for your password if you omit it.)