I have always believed that when using computers, one must constantly be looking for ways to let the computer do the repetitive work - this is after all what computers are best at! It is easy to lose sight of this, however, and hack away at the same old commands day in and day out. Well, I say enough!
The problem: Starting up a Rails server, console, and dedicated command prompt multiple times a day, and dedicating valuable screen and taskbar space to multiple command prompt windows just isn't good enough! (This is especially true when working on multiple projects.)
The solution: Console2 and a shortcut file (or one shortcut file per project). I assume here that you may be new to Console2 or have limited experience with it, so don't be afraid. Charge ahead.
First, download and install Console2 beta:
http://sourceforge.net/projects/console/. I installed mine at C:\Console2.
Console is a Windows console window enhancement. Console features include: multiple tabs, text editor-like text selection, different background types, ... [etc.]
It is important to keep in mind that Console is not a Windows shell (command prompt) replacement like
Windows Power Shell or
Cygwin. Instead, it is basically just a fancy wrapper around the Windows shell. And after playing with Windows Power Shell I have determined that I have no immediate need to use it... but you might. And if you do then that's just fine because you can pull up Windows Power Shell in a tab inside of Console just as easily as you can pull up the Windows command shell. You'll see how to later.
Let's jump right into the Console2 configuration options. The following are my settings. (At least the juicy ones.)
One thing in this screen that I was disappointed to learn was that it is not possible to set the foreground text color for each tab individually. There is only one global text color setting for all tabs and it is right here (above) on the Appearance tab. So I chose to use white as mine. Fortunately it
is possible to customize the background color for each tab, though, as we'll see in a minute.
I like to trim up my console a bit, removing the excess menu and toolbars, etc. One can always access these by right-clicking anywhere on the console window later.
The "Tabs" tab is where the magic starts happening. In here you can add predefined tab names which we'll be referring to (by name) later on. For the rails environment, then, we will want to create 3 tabs. 1) cmd, 2) Server, and 3) Console. Above I have pictured my Server tab. Notice the shell being used is "cmd.exe" - the standard Windows shell. The "Startup dir" setting will override the default startup directory set either back in the first page of settings or in a Windows shortcut. I set mine to my root projects folder, C:\code. This is just because I didn't want to limit myself to any one project yet. Besides we'll be overriding this yet again in a bit.
In the Background tab, you can set the background color for, in this case, your Server window. As you can see my Server background is green. I have my Console background set to red, and normal command prompt is set to black. Easy enough!
Ok now that Console is set up, let's get into the meat and potatoes of this post. Create a shortcut to Console.exe and place it on your desktop for now. Then right-click on the shortcut and choose properties. Now, enter the following crazy long command into the Target field (in the Shortcut tab):
C:\Console2\Console.exe -t Server -d APP_NAME -r "/k doskey /macrofile=C:\macros && ruby script/server -b 127.0.0.1 -p 80 --debugger" -t Console -d APP_NAME -r "/k doskey /macrofile=C:\macros" -t cmd -d APP_NAME -r "/k doskey /macrofile=C:\macros"
Note: The Target field in a windows shortcut can only hold up to 260 characters. The above is cutting it close to that threshold. Also, remember to replace all 3 instances of the token "APP_NAME" with the name of your rails app. Since we set the startup folder for your Server, Console and cmd tabs to your rails project root (in the Console 2 Tabs settings above) this is a valid path to your rails app.
What is this crazy long command doing? Let's break it down...
- C:\Console2\Console.exe = the Console2 program executable
- -t Server = use the Console2 tab named "Server" that we defined earlier.
- -d APP_NAME = startup directory for your Rails app. This way the shell will start up in the Rails app directory specific to this shortcut.
Now for the
-r "/k doskey /macrofile=C:\macros && ruby script/server -b 127.0.0.1 -p 80 --debugger"...
This is a bit of a special case (and also completely optional)... We could have just used
-r "/k doskey /macrofile=C:\macros" here, but I wanted to perform 2 commands for this tab after Console2 starts (the
-r flag means "run the following command when Console2 starts"). Here's the breakdown of the full command:
- /k is just a necessity for the windows cmd.exe shell.
- doskey /macrofile=C:\macros means run the doskey command, passing it the macrofile switch with a value of "C:\macros". This is an important file that we have not set up yet but will in a minute.
- && ruby script/server -b 127.0.0.1 -p 80 --debugger = after initializing the macros file, startup my Rails server. You may want to customize your rails server startup script here though, perhaps to use a different port.
From there, the rest of the text in the Target field is just repeating itself to set up the 3 different tabs, all defined similar to the above break down. Remember we're trying to save screen real-estate so we're starting up a Server tab, a Console tab and a cmd tab all in one Console2 window from just one shortcut file. Makes sense? I found the above Console command line switches in the help file, by the way, which is located at C:\Console\console.chm if you installed Console2 to C:\Console. Now on to the C:\macros file.
Note that I called this file "C:\macros" to save on space from e.g. "C:\macros.txt", but this is just a text file. I saved 12 characters on listing this file 3 times in the Target field above, and I needed to save that space to fit within the 260 character limit of the Target field in windows shortcuts!
Inside of C:\macros I have the following:
ls=dir/w $*
ll=dir $*
cat=type $*
..=cd..
grep=find "$1" $2
mv=ren $*
rm=del $*
m=type C:\macros
me=notepad C:\macros
mr=doskey /macrofile=C:\macros
gls=dir/w %GEMS%\$*
gll=dir %GEMS%\$*
gcd=cd %GEMS%\$*
ge=e %GEMS%\$1
s=ruby script/server -p 80 --debugger
s80=ruby script/server -p 80 --debugger
s3000=ruby script/server -p 3000 --debugger
s3001=ruby script/server -p 3001 --debugger
p=mongrel_rails start -e production -p 80
p3000=mongrel_rails start -e production -p 3000
c=ruby script/console
rr=rake routes
rre=rake routes | e
rrc=cls && rake routes
rdm=rake db:migrate $*
rdmr=rake db:migrate:redo $*
rlc=rake log:clear
UPDATE: I've added the contents of my C:\macros file as of 11/1/2009. I've added a section for viewing, editing and reloading the macros file (m, me, mr - repsectively) and a few other goodies. For more on what's going on with the g* macros, check out my post on
Viewing source for local gems in Windows.
You'll probably recognize what's going on here. I'm setting up basically the equivalent of aliases - a real repetitive task saver! Feel free to add any aliases that you'd like, of course! Take note of my two favorite ones:
s=ruby script/server -p 80 --debugger and
c=ruby script/console. With these I can start my Rails server simply by typing
s and hitting enter. Or I can start up my Rails console by typing
c and hitting enter.
Ok now we're all set. You should be able to just double click the shortcut you made on your desktop and you'll see your Server pop up and immediately start running, your Console ready and waiting in the 2nd tab, and a standard command prompt in the 3rd tab, also ready and waiting - for you to issue rake commands (like
rake routes)! Now that's about as warm a welcome as coming home to your dog after work! Oh and I don't like to start my Rails console automatically along with the server because sometimes it can interfere with the Server output buffer for some reason, so I just manually start it with my
c alias when I'm ready to.
One last suggestion: Add a shortcut key combo to your windows shortcut file on your desktop! I typically use Ctrl + Shift + FIRST_LETTER_OF_RAILS_APP_NAME. This way you can start your Server, Console, and cmd prompt up almost as fast as you can think about it.
A newbie hint: When working with Console, if you'd like to copy some text, the default behavior has you hold down Shift while dragging the mouse. At first I thought this was weird, but now I find it a welcome alternative to the Windows command prompt options... (either finding "Mark" in the Edit menu, or enabling the "Quick Edit" option). Quick edit made it too easy to get in trouble with the Rails server by accidentally single-clicking in the server window to give it focus. This, of course, would put a selection box on the server and thus would freeze the process until you escaped out of the selection (during which time your browser can't reach the server for no obviously apparent reason)! With Shift + Mouse Drag this complication is much less likely. But of course, if you'd like to change this behavior you can - in the Console2 Settings dialog.
Cheers!