Vim Commands every Programmer should know

Aside from using Rubymine and occasionally using a bunch of GUI editors, I enjoy the occasional geeking with Vim, a text editor written by Bram Moolenaar, as my command line editor of choice.

It is probably one of the most popular editors of our times and will continue to be, most probably because of the fact that it is cross platform and has been pre-installed in a lot of unix systems. Not only does it make you look cool, not using a mouse, it boasts having to help you become more productive.

Vim commands basically removes the need to hold a mouse, everything is at your fingertips, literally. Moving your cursor through a document requires the use of 4 basic keys, j for down, k for up, h for left and l for right.

How cool is that!?

Now here a few VIM commands that will make you a VIM expert in no time:

Basics
Command Description
:e Open a file
:w Save a file
:q Close a file
:qa Close all files
:wqa Save and Close all files
Navigation
Command Description
Ctrl+v Block Selection
gg Go to first line
Shift+g Go to last line
o Go to next line and start editing
Shift+o Go to previous line and start editing
Shift+v Highlight current line
Shift+v Shift+g Highlight current line until end of file
Shift+^ Go to to the beginning of the line
Shift+$ Go to the end of the line
Search and Replace
Command Description
/keyword Search from top to bottom
?keyword Search from bottom to top
%s/old/new/g Search and replace old with new on current document
Working with multiple files
Command Description
:split filename Split window and open file in new split
:vsplit filename Split window vertically and open file in new split
:vs filename Split window vertically and open file in new split
ctrl-w ctrl-w Go to next window
Recipes
Command Description
gg Shift+v Shift+g Highlight the whole file
Ctrl+v Block Selection

Most of this commands I have learned working on web applications. It has made me faster for most tasks that needed text manipulation. I learned to love it. Trust me. After a few days or months on Vim, you’ll seem to hate all GUI editors.

 Generate Ordinal numbers, 1st, 2nd, 3rd in PHP

This is a neat function I have used to create ordinal numbers in PHP.

function ordinalize($num) {
        $suff = 'th';
        if ( ! in_array(($num % 100), array(11,12,13))){
            switch ($num % 10) {
                case 1:  $suff = 'st'; break;
                case 2:  $suff = 'nd'; break;
                case 3:  $suff = 'rd'; break;
            }
            return "{$num}{$suff}";
        }
        return "{$num}{$suff}";
    }

It produces something like 1st, 2nd, and 3rd. It works on all integers of course.

1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th
11th 12th 13th 14th 15th 16th 17th 18th 19th 20th
21th 22th 23th 24th 25th 26th 27th 28th 29th 30th
31th 32th 33th 34th 35th 36th 37th 38th 39th 40th
41th 42th 43th 44th 45th 46th 47th 48th 49th 50th
Let’s Try it Out

I wrote some PHP code to output ordinal numbers in tables like the one above but this one is until 100:

<?php function output (){
        echo '<tr>';
        foreach (range(1, 100) as $number) {
          echo '<td>';
          echo ordinalize($number);
          echo '</td>';
          if ($number % 10 == 0) {
            echo "</tr><tr>";
          }
        }
    }

    function ordinalize($num) {
        $suff = 'th';
        if ( ! in_array(($num % 100), array(11,12,13))){
            switch ($num % 10) {
                case 1:  $suff = 'st'; break;
                case 2:  $suff = 'nd'; break;
                case 3:  $suff = 'rd'; break;
            }
            return "{$num}{$suff}";
        }
        return "{$num}{$suff}";
    }

?>
<h3>Output:</h3>
<table class='table'>
  <?php output();?>
</table>
Live Example

Try it the sample for ordinal numbers in PHP with code included!

 Railscast's Deploying to a VPS

I have been using Ryan Bate’s Railscasts deployment recipes as long as it has been up. I’ve deployed projects for some startups that needed help, for my own startup projects and soon, another project that I have been cooking up for a while.

A few days ago, I have started tinkering with this script and have test deployed a few applications. I tried a few things and updated my deployment gist that I have been using to deploy.

A lot has changed for this deployment recipe.

  1. Ubuntu 10.04 is mostly not made available anymore by VPS providers
  2. A lot has changed since 10.04. Some commands may or may not work. You have to do some of your own research
  3. It still uses ruby version, 1.9.3

I have no problem using ruby version, 1.9.3. So, I left it at that. I am of course still using Unicorn and deploying with Capistrano.

Notable Changes

I urge anyone try it out!

 Textile

I have always looked at html as the best markup I can use for blogging or anything remotely connected to the web.

Through the years, I have had chances to work with lightweight markup languages like Markdown through Github and Stackoverflow and Textile through Trac.

The thing is, I like it now more than ever. Especially after tingling a bit more with TextPattern. I chose Textile instead of Markdown with no particular reason. However, here’s a comparison from Stackoverflow if you want see one.:

Textile
  • Used by Redmine and the Ruby community
  • 5 questions currently tagged on Stack Overflow
  • The most similar to HTML, but least readable source
  • Easiest nested lists of all three languages
  • Not understandable to non-programmers or the HTML-ignorant
  • Ideal for complex short documents (with links, nested lists, code, custom HTML); for example: short documentation, how-tos, blog or CMS content
Markdown

Doesn’t seem to have a home language “community” but…

  • … 47 questions tagged on Stack Overflow
  • Emphasizes source code readability, similar to email traditions
  • Straightforward HTML embedding (you just type the tags out)
  • No way to make tables besides embedding HTML
  • You know it already if you know Stack Overflow
  • Easy to learn if you already know reStructuredText
  • Automatic email address obfuscation for the format (with angle brackets)

I have modified my blog to use Textile. In fact, this is first post I am writing in Textile. It’s kind of weird. Fun in a way, like learning a new language. I can’t say that it’s the best way to blog, but I trust it. I trust that the formatting will be better than most WYSIWYG editors I have tried though. Something which we will get to later on.

Using Textile (even in other lightweight markup languages) can make you very expressive. Using tons of ways to present your writing. I can write tables without being bombarded by the need to put < and > signs everywhere just to create tags.

Full table with summary, caption, colgroups, thead, tfoot, 2x tbody

Title Starring Director Writer Notes
This is the tfoot, centred
The Usual Suspects Stephen Baldwin, Kevin Spacey Bryan Singer Chris McQuarrie One of the finest films ever made
Se7en Morgan Freeman, Brad Pitt David Fincher Andrew Kevin Walker Great psychological thriller

I can write Chemical formulas without thinking of matching tags.

Calcium borate: Ca3(BO3)2

Truly impressive!

WYSIWYG Editor Problems

One of problems with WYSIWYG editors, aside from the fact that they are heavy, is that they don’t produce clean markup. Yes, I am looking at you TinyMCE and CKEditor. You would see cluttered <br/>s, which I don’t favor, everywhere. You see excessive unneeded <p>s.

Lack of control on what you produce. I find myself switching to HTML view to modify the HTML produced by these editors. It can be cumbersome.

Thanks Dean Allen

I have to hand it to Dean Allen, the creator of both TextPattern and the Textile markup language. I have borrowed the classTextile.php class from TextPattern to convert my posts from Textile to HTML. And to tell you frankly, I am having fun with this.

Sidenote

On a side note, I just learned that Sublime Text 3 Beta has just been released. I’ve just downloaded it. There is a considerable slowness though, compared to the second version. It’s still Beta, so I must say I forgive them.