Create / Modify Text Files on the Command Line

While working on the command line, I hate opening up files in editors just to create files that would contain measly content like, .rvmrc, phpinfo files and README files.

Fortunately, in Linux and Unix systems, you can easily create and modify files with just the command line. This guide will teach how to create and modify any text files without the use of a text editor on the Command line.

Create a file

This command simply lets you create files:

touch phpinfo.php

This command simply lets you create the files and start adding content. Press Ctrl+D to stop.

cat > phpinfo.php

Append content

This simply lets you append some text. In this case, <?php phpinfo();.

echo "<?php phpinfo();" > phpinfo.php

This command simply lets you append to the file specified. Press Ctrl+D to stop.

echo "<?php phpinfo();" > phpinfo.php

View a file’s content

cat phpinfo.php
<?php phpinfo();

‘Nuff said!

 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!