Use Dropbox to keep your preferences across Computers
Sometimes it can be kind of painful to discover a cool new setting or mode for on of your favorite programs. For instance, I use TTYtter, a terminal based Twitter client. A few weeks into using it, I discovered it had ReadLine support for tab auto-completion of @usernames, in-line editing of posts and command history (up key). Awesome right? I know. Except, now I need to change my .ttytterrc file on my laptop, home computer, work computer, everywhere. Also, how do I get it there? USB drive, e-mail to myself, browse the network, etc. Being geeks, we don't want to go through all that.
Then I discovered Dropbox (that link is a referral link, here's the regular homepage). Not only is Dropbox a free 2GB of cloud storage for backing up your much needed files, but the really cool thing about Dropbox is that it automatically detects when files change and syncs them across all of your computers. Think about configuration files and read that again. Discover a new mode in any program with a config file and BAM! every computer you use has that mode enabled. The sync works in all directions so it doesn't matter which computer you make the change on, every computer gets it. Seems perfect, right?
Here's how it all works. When you install the Dropox client software on your computer, it will create a Dropbox folder with all your stuff in it. Add, delete or edit any file in this folder and Dropbox will sync it up to their servers, plus download the changes to any other computer running the Dropbox client with your account. It's kind of cool to have your desktop and laptop running next to each other and watch them download the changes instantly.
Back to business, I know what you are thinking, "All these files are in one Dropbox folder (and subfolders). My config file needs to be in my home directory. I still have to copy from my Dropbox to my home." Well, let's let the computer do the copying. Actually, let's not even waste time and space copying. Let's create a symlink to make the config files appear to be in your home folder but actually be stored in the Dropbox folder.
First, open a terminal, navigate to the Dropbox folder and create a new subfolder; I called mine 'configs.'
$ cd ~/Dropbox
$ mkdir configs
$ cd configs
Now, let's move the config file from the home folder into the Dropbox/configs folder and navigate to the home folder.
$ mv ~/.ttytterrc ~/Dropbox/configs/.ttytterrc
$ cd ~/
Here's the magic. We'll create a symlink (like a shortcut in Windows) which will make it appear that the file is in your home directory, but Dropbox will still keep track of it.
$ ln -s ~/Dropbox/configs/.ttytterrc ~/.ttytterrc
Rough Spot: If you get a file exists error, the link will not overwrite an existing file. Remove the file from where you are creating the link into (the home dir in our case).
Do this on all your computers, then you can edit the file directly from my home directory on any computer and the changes will be automatically propagated to all my other computers instantaneously.
Here are some other ideas for this technique which I haven't tried, but might be cool.
- Thunderbird Profile
- Media Library
- Flat data files
If you have any more or try one of these, let me know in the comments!
Trick out your Vim
Today, I'm going to add some plugins and scripts to make better use of Vim. I have not started this project and I will be updating this post throughout the day with my finds and experiences.
I generally use Vim to code web applications. My site at work run on PHP and at home I'm working with Ruby on Rails. So, I'll be looking for something to specifically help with that, but who knows what I'll find.
.vimrc
.vimrc is a file in your home directory. It contains a script which runs each time you start Vim. You can setup your configurations, preferences and plugins in here. The .vimrc is the starting point for fine tuning your Vim to exactly how you want it. If you are a beginner, I would recommend finding some more advanced users' .vimrc's and, at least, check them out, if not start using them for yourself. I started with Todd Werth's .vimrc file. He keeps his in a separate folder and uses a symlink to put them in his home folder. I strongly recommend this as well. You can automatically sync your .vimrc (or any other files) across computers with Dropbox. See how in my post on how to Use Dropbox to keep your preferences across Computers.
Here are some useful lines to start you out:
" Set tabs to 2 sapces
set softtabstop=2
set shiftwidth=2
set tabstop=2
set expandtab
" Move your backup and .swp files out of the directory of the file (Helpful to keep from adding them to a repo)
set backup
set backupdir=~/backup
set dir=~/swap
" Highlight the current line
set cursorline
" Keyboard shortcuts (Mappings)
imap kj
imap hh => " Hit hh to insert a "rocket" in insert mode
" Wildmenu - show a tab completion matches list
set wildmenu
set wildmode=list:longest,full
Again, search the google machine and checkout github for more samples and if you find anything cool, let me know in the comments.
Plugins
Autoclose
This first plugin automatically closes quotes, parenthesis, brackets, etc. for you. If you type a single quote, another single quote is added after the cursor. You can type an end paren to move the cursor after the autoclosed characters. This is helpful after nested autocloses. For instance, in PHP, I often find myself passing array values to a function:
run_trail($locmotives['lionel|'])
This pipe in the above example is the cursor position after the last l in lionel. I can simply hit ) and the cursor will jump out of the single quote, square bracket AND end paren. Simple.
Autoclose is developed by Karl Guertin and can be found at http://www.vim.org/scripts/script.php?script_id=1849
See ya Soon!
Keep ssh sessions from timing out
The Ubuntu Blog has a nice lil' article about keeping SSH sessions alive.
It basically boils down to editing your /etc/ssh/ssh_config file and adding the following:
/etc/ssh/ssh_config
ServerAliveInterval 5
The number is the number of seconds to send the small keep alive which keeps the connection open. Ubuntu Blog suggests changing it from 5 to 240 or 300 (4 or 5 minutes).
Breaking Bad Season 3 Firefox Persona
Update: HURRAY!!! It was approved!! Get it now at
http://www.getpersonas.com/en-US/persona/140288
I just submitted this to the persona site. If you love Breaking Bad as much as I do and can't wait for Mozilla to approve it (I don't even know how long it takes), there are instructions about adding your own custom Persona from Mozilla.
In Brief:
First download these images:
BB_top_header.jpg
BB_footer.png
Then, if you don't have the Personas add-on, go here to get it. After restarting Firefox, right click on the lil' fox in the lower left of Firefox and from Preferences, check Show Custom Persona in Menu. Then click the fox again and choose Custom > Edit. Select these Images: Header: BB_top_header.jpg; Footer: BB_footer.png. I used white for the text color and a dark green for the accent color. Click Ok.
That's it! Enjoy the Breaking Bad awesomeness!!
UPDATE: I found out that it usually takes about a week for a Persona to get approved which should be around Friday March 26, 2010. I will update this post when I hear word. If you see if before me, let me know in the comments!
Notepad++ Color Configurator and styler.xml
Before I start talking about colors, let's talk font. Consolas That's all you need to know. By far the best programming font. Get it. Use it. Love it.
Recently I started learning Ruby on Rails and I found RailsCasts with a ton of Rails focused screencasts. I believe he (they?) use TextMate for mac. I hadn't really thought much about color in my code before watching some of these screencasts. But, it actually helps, a lot! I use Notepad++ in Windows and they have a great styling system. You can use the in program Style Confirurator or you can directly modify the XML style sheet.
Style Configurator
The Style Configurator is a dialog within Notepad++ accessible through the Settings menu. The really great thing about the Configurator is that the styles you choose are updated live in your code. So, open up a file with a lot of code. Then open the Configurator and move it over to another screen. If you don't have more than one monitor, check the Transparency box in the lower right and you will be able to see through the Configurator. The slider there sets the Transparency level. Now you can see all of your code and pick styles for them.
Start with the very first 'language,' Global Styles. These not only include default styles of text but many of the stuff around your code, such as line number margin, inactive and active tab color, and the edge. The edge is great to keep your lines of code to a certain length for printing or displaying in a terminal. I also set the global font to Consolas. Also, be sure to set the current line color and selected text background color.
Now you are ready to style for you language. I usu PHP mostly at work so I'll go though that one. Hopefully, I'll be familiar with Ruby soon enough to write a post about Ruby styles.
- QUESTION MARK - This is the style of the opening and closing php tags ('')
- DEFAULT - Text that doesn't match any of the other styles will be this.
- STRING - Any string enclosed with double quotes
- STRING VARIABLE - variables enclosed withing literal strings: "The Count is $count"
- SIMPLESTRING - These are string enclosed with single quotes. (I used a very similar green to STRING)
- WORD - These are the keywords of PHP. There are a bunch defined. You can also define some of your own in the blank box.
- NUMBER - Number literals, including array indices
- VARIABLE - Any word started with a $
- COMMENT - Text between matching multi-line comments ('/*' and '*/')
- COMMENTLINE - Text on a line after and including two forward slashes (//)
- OPERATOR - Operators (+,=,-,etc.), matched parenthesis, brackets, curly brackets and logical operators
Stylers.xml
The stylers.xml file contains all the settings from the Configurator used by Notepad++. In addition to using the Configurator, you can manually edit this file. Or, download one from the web. The Notepad++ site has some under Theme Files. Joy Boner (read the About page) has 60 themes. I didn't check any of these out, but it might be worth a shot if you really don't want to set your own styles.
I also put my personal stylers.xml as a gist on github. Feel free to fork it.
If you have any other Notepad++ styling tips, comment below!
--Chris
Getting and Parsing E-mail with PHP
Here's my Problem:
My website sends text files to a partner's site via FTP. Our partner site sends us the results of their processing the file in an e-mail. This e-mail is a the direct output from their processing script. Or, the relevant details are buried in a bunch of garbled text.
My Solution:
I first looked at message piping, but my hosting provider doesn't provide an easy way to do this.
Then I found this PHP class from PHP classes, which handles the interactions with the server. You do have to create an account with PHP classes to download the files. If you e-mail me directly, I could send them to you.
It's easy enough to get working quickly.
$pop3->hostname="localhost"; // POP 3 server host name $user="username"; // Authentication user name $password="password"; // Authentication password $pop3->debug=1; // Output debug information $pop3->html_debug=1; // Debug information is in HTML
Edit the test_pop3.php file. All you need to do is change the sever name, user name and password to get rolling. After you see the full output and it's getting the mail correctly, I recommend setting the debug and html_debug values to 0. This will cut out the unnecessary text of the interaction with the server and just show you the messages and what's going on.
One hangup of this class, the body of the message is an array and the message was repeated twice in the array.
array(14) { [0]=> string(30) "--0016e6d99d7ed848e5047b02e012" [1]=> string(44) "Content-Type: text/plain; charset=ISO-8859-1" [2]=> string(0) "" [3]=> string(71) "This is a test. If this were an actual message, important text would be" [4]=> string(5) "here." [5]=> string(0) "" [6]=> string(30) "--0016e6d99d7ed848e5047b02e012" [7]=> string(43) "Content-Type: text/html; charset=ISO-8859-1" [8]=> string(0) "" [9]=> string(77) "This is a test. If this were an actual message, important text would be here." [10]=> string(4) "" [11]=> string(0) "" [12]=> string(32) "--0016e6d99d7ed848e5047b02e012--" [13]=> string(0) "" }
Luckily, the part boundaries are included in the array which makes it fairly easy to split the array and just get the message once.
$oneBody = array(); $delimiter = $body[0]; $oneBody_i = 0; for($body_i=3; $body_i<count($body); $body_i++) // starts on 3 to skip the top of the body stuff { if($body[$body_i] == $delimiter) break; $oneBody[$oneBody_i] = $body[$body_i]; $oneBody_i++; }
I needed to run some regular expressions on the body and imploded it into a single string.
$bodyString = implode('', $oneBody);
Now I have the body of my e-mail message as a string variable ready for processing. Alternatively, you can change the first parameter in implode( to a line break if you need to display the message not as one long string.
Lastly, once you process your messages, you don't need them anymore.
if(($error=$pop3->DeleteMessage($index))=="") echo "<PRE>Marked message $index for deletion.</PRE>\n";
The DeleteMessage( method will mark messages for deletion when you close the connection to your mail server. Please note that this method does not immediately delete the messages and messages can still be 'un-deleted' until the connection is closed. The ResetDeletedMessages( method is used to un-mark all messages for deletion.
Happy Parsing!!
--Chris