I'm a programmer specialising in performant and scalable systems using PHP and Ruby and cooking


Published:

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):

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)

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

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.

### 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:

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.

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.

UPDATE I've just been told I've not 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 advantages. 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.