Skip to content

Categories:

Cost effective coding with Vim

Before i start I’d just like to make a note that I’m not trying to tell you the best way to code, or the best software available; simply how I do it as an aid to those starting out.

There are a lot of software solutions around varying in cost and usefulness to aid in developing code and managing projects, but which one should you choose?

Often it comes down to cost but what do I mean by cost. Now obviously you can look at something like Adobe Dreamweaver CS4 (PC) and see that at a cost of £378.35 new (at time of posting) this is out of reach of most peoples budgets.

There a lot of free solutions available that all do a great job, like PHPEdit, but what most people don’t realise is that there are hidden costs with these options. Let’s use PHPEdit and Eclipse for PHP while greate products for PHP development, have not quite been up to the task.

So what other costs are there? Well for most people these are simple solutions that are spot on and meet their requirements, but for me they just weren’t up to scratch. A project i was working on recently had become quite large and the code base was very extensive, trying to manage the project in most of these solutions was impossible, simply because the project was so large. So the first hidden cost found was the requirements on my development PC. To effectively use these solutions I would need to spend out to get a high spec computer just to allow me to view my project and load files from the editor.

Ok so for most people that’s not an issue, but have you ever thought about the time it takes to setup one of these environements and tweak settings until you are happy with the result? It can take ages! In comes VI.

VI Improved (VIM)

VI is a standarad editor available on all linux flavours, either pre-installed or simple to install from packages. Although great VI is not a develpoment environment, it is simply a text editor, free and as a bonus supporting charity.

Here is a small snippet describing VIM from their website:

Vim is an advanced text editor that seeks to provide the power of the de-facto Unix editor ‘Vi’, with a more complete feature set.

Vim is a highly configurable text editor built to enable efficient text editing. It is an improved version of the vi editor distributed with most UNIX systems.

Vim is not for everyone, for one the Vim website maintains that Vim WILL NOT hold the users hand, unlike many of the solutions out there today. What it will do is enable you to code in an efficient and cost effective way.

Personally I love Vim and use it for all my own development, it’s so easy to setup and is great for “just working” without all the fussing with settings. Not only that, if i need something more configured to a particular need, Vim does it.

Great! So what can I do with Vim?

I’m not going to go through every aspect of Vim and options you can set, but here are a few i sue when working with PHP, especially when working with frameworks like Symfony.

Tabs, Shift width and spacing

To keep code tidy i like to stick to the same code layout standards as the framework i’m working in. TO do soVim can be configured to use specific settings for tabs, lettings spacing, line heights etc. So first let’s take a look at tab spacing.

Setting up Vim couldn’t be easier simply edit a Vim config file (to be refered to vimrc):

$ vi ~/.vimrc

Note my usage of the ‘vi’ command to edit the file. This will use my default settings with Vim to open the file in a text editor instance.

Now let’s add some settings, i’m not going to go through all the command available to Vi and am going to assume you can use unix systems and Vi before attempting any of my suggestions. If not here is some great documentation.

set tabstop=2

This will set the number of spaces a tab is equal to (mmost systems this defaults to 4)

set expandtabs

This will expand all tabs into spaces, I find this makes it easier to manage code layout

set shiftwidth=2

This is mostly used for code indenting and for moving blocks of code. Best to make this match your setting for tabspace

set autoindent

This will enable auto indenting of code on carriage return

set smartindent

This will make Vim attempt to work out where it should tab in , or out, the next line of code. For example:

   function test() {
   -->
< --}

These are some of the settings I use for my own development. Now that we have this in our vimrc file Vim will automatically use these settings whenever we edit a file with Vim. There is a way to use seperate Vim settings with different code lanuages but I will not cover this in this post.

Note if you don’t want these settings all the time, you can run them during edit time by typing them directly into the editor screen e.g.

:set tabstop=2

Syntax Highlighting

Another great feature of Vim is the ability to turn on syntax highlighting. Vim will use patterns to match code and highlight it making coding easier. For PHP in particular this is great since Vim nows about many of the built in PHP functions and will highlight them as such, making it easier to spot mispellings before you attemp to run the script.

Turning on syntax highlighting is a breeze, again this command can be run in the editor during use.

set syntax=on

Simple eh? Making changes to Vims settings pretty much all envolve one liners.

More than one file please…

One of the biggest complaints I hear about people using Vim is that they can’t see more than one file open at a time. Having been used to Dreamweavers tabbed interface to file editing. Well you can, all it takes is some simple commands.

Creating tabs

Opening a file in a new tab is really simple. There are several ways to do it but the easiest way, and usually the way i do it is with:

:tabe /path/to/file

Don’t worry if you don’t know where your file is exactly, Vim wil use the tab key to auto-complete the same way as the shell prompt does.

Moving tabs

Vim recognises tabs has 0 based index, this means your first tab is tab 0. Moving tabs around envolves knowing the numbers of your tabs, but for a smalll number of tabs this is easy. To move a tabs position to alongside another tab you can use:

:tabm 2

This will move the current tab into the place of tab 2 (your third tab) and shift all mthe following tabs along by 1.

Navigating between tabs

When using tabs it’s vital to be able to navigate between tabs, makes them pretty pointless otherwise. Vim has a couple of useful commands that allow you to go back and forth between tabs.

:tabn - Go to the next tab
:tabp - Go to the previous tab

Closing tabs

In Vim you can close a tab the way you would any normal Vim instance (a tab being just a nested instance of Vim). There is another handy command that allows you to close all tabs currently open.

:qall

q being the normal Vim command for closing a tab, qall simply tells Vim to close all instances nested with the current instance.

I’m not an expert and I’d love to hear other peoples experiences with Vim and text editors. As per usual, any corrections or suggestions please let me know and I will put them in.

I’ve just been told I’ve no mentioned anything for those that use Vim on a graphical desktop so I like to point out Vim’s pretty sister gVim. Although very similar gVim does have some advantges. One major one being a GUI interface, so less keyboard commands to remember but also syntax highlighted has much better support and range on a graphical desktop.

Thanks to Simon Green at Bluhalo It for the tip.

Posted in Daily, Guides.

Tagged with , , , , , .


Symfony 1.2, Doctrine 1.1 and saving relationships

Saving related data in forms

When creating a form in Symfony 1.2 sometimes you may want to use Doctrine to automatically save your related data. Since the update to 1.1 there is a small problem in that this doesn’t work.

For example let’s take the Faqs section. Each FAQ can have MANY FAQ groups and each group can have MANY FAQs. So when we edit an FAQ we have the option (checkboxes) to choose which groups to assign the current FAQ to.

Ordinarily Doctrine would handle saving these relationships but unfortunately we have to take care of letting the model know what it needs to save ourselves. We do this by overloading the doSave method of a form in order for us to set the linkage and tell the model that the relations should be set as modified and therefore saved. Fortunately since Doctrine would normally be expected to handle this behaviour there are methods that make this easy for us.

First we need to overload the doSave method and this must match the interface in the form class, in this case PluginFaqForm.class.php:

public function doSave($con = null)
{
}

Then we need to unlink all relations for our groups:

$this->getObject()->unlink('FaqGroups');

“FaqGroups” would of course be replaced with the name of you alias.

Why unlink? well this tells doctrine to remove all references to other records with that alias. This is so that we can correctly blanket remove the relations in case we uncheck any in the form. Because we haven’t passed any other options to this method, it will assume we want to unlink all records and also that it will store these changes for future modifications before committing to the database.

So now we need to atually link our selected groups:

$this->getObject()->link('FaqGroups', $this->getValue('faq_gropus_list'));

We have added the the aias as the first parameter so that doctrine knows which relationships to setup. The second parameter is and array of ids to setup the relationships with, in this case an array of checkbox values taken directly from our form.

Now we need to actually save the changes so lets tell Doctrine to carry on with it’s normal process:

parent::doSave($con);

So this in total looks like:

public function doSave($con = null)
{
  $this->getObject()->unlink('FaqGroups');
  $this->getObject()->link('FaqGroups', $this->getValue('faq_groups_list'));
  parent::doSave($con);
}

All you need to do is drop this into your form to overload the parent methods, then change the alias and away you go. This can be used as many times as you wish just by copying the unlink and link command lines.

Posted in Daily, Guides.

Tagged with , , , .