Pages

7/05/2012

Vim plugins

Vim has countless plugins that extend the built-in functionality. One great place to download plugins is from the vim.org website ( http://www.vim.org/scripts/index.php ). Although, I must admit that it's not very easy to discover new plugins there (you almost have to know what plugin you are looking for in order to find it on the vim.org website). For that reason, I am publishing a list of some of my favorite plugins.

Taglist

Source code browser - shows an overview of the source code structure (by file) and allows you to quickly jump to specific items. (Note, this plugin relies on Exuberant Ctags)
http://www.vim.org/scripts/script.php?script_id=273

Gtags

I use Gnu Global as a source code tagging system (instead of Exuberant Ctags). It is really quite handy, allowing quick navigation to function/ class references to their definitions (and back).
Please see http://wadeberrier.blogspot.com/2012/04/source-code-navigation-with-vim.html for a refence on how to set it up.
Download Gnu Global at http://www.gnu.org/software/global/

Setcolor

Allows for quick/easy colorscheme switching. I used this a lot to finally settle on my favorite colorschemes. There are some languages for which I prefer different color schemes - in these cases, this plugin is very convenient. Currently, my favorite colorschemes are 256-jungle, jellybeans, and spiderhawk.
http://vim.wikia.com/wiki/Switch_color_schemes

A

This simple plugin allows you to quickly swtich between source files and header files (C / C++). The default keybinding to switch is :A, and :AT will open it in a new tab.
http://www.vim.org/scripts/script.php?script_id=31

Vim Tutorial

Vi (and Vim) are text editors that have been around for a very long time. One can find Vi on almost any Unix system. While I prefer to develop software with a graphical IDE (such as Eclipse and Visual Studio), it has proven very beneficial for me to learn how to use Vi.

For anyone interested in learning how to use Vi / Vim, the best tutorial I have found is at http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html . It has a series of screenshots highlighting what important keys do. (A pdf version can be found at http://www.glump.net/dokuwiki/howto/vim_graphical_cheat_sheet ). I like to print off the master cheat sheet and keep it close to my computer for quick reference.

Note that there are many features to Vi / Vim that are not covered in the tutorial - it is a really powerful/feature-packed editor that cannot be covered in a simple tutorial.

6/20/2012

PHP - POST an array

Those familiar with HTML know how to submit form entries to the server. Form values become accessible to the server based on the element's "name" attribute. Take the following form for example:

<form method="post" action="index.php">
        First Name: <input name="fname" type="text" />
        Last Name: <input name="lname" type="text" />
        <input type="submit" value="Submit" />
</form>

When the form is submitted, the form values can be access in PHP with the $_POST or $_REQUEST superglobal variables. To get the "First Name" that was submitted, simply use $_POST["fname"] or $_REQUEST["fname"]. Easy.

Now, if you want to use checkboxes, you don't have to use a different name for each checkbox (that could get messy in your PHP code). Instead, you can give all checkboxes in a group the same name. The trick is that you submit the checkbox values to an array (much easier to deal with in PHP - as long as the checkboxes are closely related). Example:

    <input name="os[]" type="checkbox" value="Linux" />
    <input name="os[]" type="checkbox" value="Windows" />
    <input name="os[]" type="checkbox" value="Mac" />
    <input name="os[]" type="checkbox" value="Other" />

Notice the brackets "[]" at the end of the name? This means it is an array. PHP will literally add each checked value as a new element at the end of the array. Then you can loop through the array in PHP:

$osSelections = $_POST["os"];
foreah($osSelections as $selection) {
        // Do stuff
}

This is still basic - nothing too exiting yet.

What I learned the other day is pretty neat. Since PHP has associative arrays (where strings are used as the array index instead of ordered numbers), you can specify a key for array elements. For example:

    <input name="submission[fname]" type="text" />

would be accessed in PHP with $_POST["submission"]["fname"]. That got you into a 2-dimension array. After years of dealing with HTML I never knew (or even thought) it was possible to specify the array key. You can even submit a multi-dimensional array like so:

    <input name="submission[os][]" type="checkbox" value="Linux" />

Pretty neat. You probably won't need to use it much, but it can come in handy in the right situation.

6/06/2012

Git Version Control

When I first set out to use git, I had a hard time understanding how to use it. Because of this, it took me quite some time to get it set up. This post is an attempt to simplify the process for others, and hopefully make it easier to understand.

First off, the best resource I have found for git is the book "Pro Git" by Scott Chacon. It can be purchased in print, or read online for free (http://git-scm.com/book). The book does an excellent job of explaining how git works. You only need to read the first 3 or 4 chapters to get started. Chapter 5 is helpful if you want to sync your repository to a server. The rest of the book has good info, but isn't required to get you started.

Here is a short summary of how I got started with git. The first thing you need to know is that git is different from traditional version control systems. For example, with subversion, there is a central repository on the server. When you want to edit a file, you checkout the file, make changes, then check the file back in. With git, you have a local repository that you commit your changes to. If you want, you can push/pull changes to/from a remote repository.

For Ubuntu users, install git like so:
apt-get install git-core

Windows users can install msysGit (http://code.google.com/p/msysgit), or you could try the new Github for Windows.

Assuming your project is already setup (whether it's a C# project, a Java project, a collection of documents, etc...), navigate to the project directory and type:
git init

There you have it - a local git repository. You will see that this created a folder named ".git" inside your project directory. Should you ever want to delete your git repository, you can just delete the .git folder (be careful though!).

Now, add your source files to the version control with "git add":
git add . (to add all files)
or
git add filename (to add an individual file).

Finally, you need to commit your changes:
git commit -m "initial commit"

Now, as you go about making changes to the source code, simply add the changed files with "git add" and do another commit.

Of course, there are many more features. It would be very worthwhile to learn about branching and merging in git as well as how to revert to past commits - all of this is described in the Pro Git book mentioned above.

Enjoy your new life with git!