Friday, March 15, 2013

How to use vim for Drupal development

View updated version of this blog here.

I am a Linux user, and a drupal developer too. Its been months I am using vim for writing custom drupal modules. Before it was eclipse, eclipse was good but I was having issues because of two reasons. First, I was working on a big project with lots of custom modules and huge database. Loading a module in eclipse editor took several minutes and secondly, my computer is a bit old Intel Core 2 Duo processor and 2GB RAM and . I have always heard being vim as one of the most powerful editors, but I realized it that time when I switched to vim for writing or just reading the code of custom modules.

Like everyone I googled for the vim support for drupal. The web pages that helped me most are these:
Non Linux users can also download and use vim, especially Windows users.

One of the recommended way to install required vim plugins for drupal development is to install them using drush. I had also installed vim plugins in this way. After installing drush, check drush version. Command to check drush version:

drush --version

If drush version is 5.8 then it is okay. Otherwise you need to upgrade drush.

After updating drush, install required vim plugins using the following commands:

drush @none dl vimrc
drush help vimrc-install
drush -v vimrc-install <options>

The first command will dowload the vimrc project. The second commad will show you the options for installing vim plugins.

But I installed it without using any options. I just did this:

drush -v vimrc-install

This will install certain vim plugins that will turn your simple vim to cooool vim. Absolutely ready for writing custom module. But you also need to configure certain vim settings so as to make it more developer friendly. I am providing my vim settings. Beginners can copy the settings, advanced users who knows tweaks and tricks of vim settings can make changes as they desire.


These are default vim settings that are loaded when you open vim. Vim global settings can be done from /usr/share/vim/vimrc while vim local settings can be done from ~/.vimrc. Vim global settings means that the vim settings will be available to all users of your system, while vim local settings are available for the particular logged in user. The file path I have given above are for Ubuntu, so Ubuntu users no worries. Sorry, for non Ubuntu Linux users you have to find it yourself.

Also it is recommended that you set color for your terminal. This will make the code more color distinguishable and easy to understand. Just edit ~/.bashrc and add the following line at the end:

export TERM="xterm-256color"

And do not forget to restart terminal after you have done this.

Now I am going to show you some amazing vim commands (available from plugins you installed earlier of course) that will seriously change your experience of custom module work in Drupal.

NERDTree
NERDTree

TagbarOpen
TagbarOpen

NERDCommeter
Command: NUMBER-OF-LINES/ci

NERDCommenter
Command: NUMBER-OF-LINES/ci

Frequently used shotcuts:
daw - Delete word under cursor
caw - Delete word under cursor and go to insert mode
d$ - Delete from under the cursor till end of line
c$ - Delete from under the cursor till end of line and go to insert mode
START-LINE-NUMBER,END-LINE-NUMBER"+y - Copy lines to system clipboard
gg"+yG - Copy all file content to system clipboard
CtrlXO - Autocomplete

Frequently used commands:
%s/TEXT-TO-SEARCH/TEXT-TO-REPLACE/gc - Interactive find and replace

View documentation of a Drupal function:
Bring the cursor at the start or under the function whose documentation you want to see. Then do:
<Leader>da - To view the documenation in drupal.org OR
<Leader>dc - To view the documentattion in drupalcontrib.org
The most wonderful thing I like about this is, it will automatically identify the core version from the module's info file. Suppose you are working on a 6.x core version module then it will show the documenation of the 6.x version. Cooollll, Right?

More information:
Drupal vimrc installs the following plugins:
pathogen - This plugin helps to manage your other vim plugins
nerdcommenter - This plugins helps you to comment out line of codes
nerdtree - This plugin acts like file explorer in eclipse where you can easily navigate through files in current directory
snipmate - This plugin provides code snippets. Drupal vimrc later adds its own module snippets in it
syntastic - This plugin provides syntax checking. Before writing to any file it will check syntax. Do not write and quit at the same time otherwise it will not be able to show errors, if any
tagbar - This plugin displays a list of functions and variables declared in the file
fugitive - Git wrapper

Other than this I installed another plugin:
SearchParty - This plugin provides color highlighting during text search
colorschemes -  A set of vim colorscheme

Update:
See a collection of vim plugins and settings for Drupal development in Use Vim as IDE for Drupal development, also see them in action.

11 comments:

  1. Will implement next time. Thanks for sharing.

    ReplyDelete
  2. I have visited this blog second time today and read carefully. It's really helpful tips for me. Thanks for sharing. I am also Drupal Developer in CMMI Level3 company. Thanks.

    ReplyDelete
  3. Deciding upon a content management system is highly vital to your success I now only use Drupal. Because As I researched and found out the most popular universities in the US use Durpal, I picked Drupal For hosting I like Pantheon with features like Drush integrations What content management system do you like?

    ReplyDelete
  4. Thanks for sharing your settings with us. This is the great sharing for drupal developers. I also copied them and apply them soon.

    drupal developers india

    ReplyDelete
  5. Hi There,

    Good tips..i found helpful information about plugins in your blog.

    Thanks for sharing.
    Drupal Plugins

    ReplyDelete
  6. This awesome and useful weblog....Thanks for this post, Thanks for publishing this useful weblog...well done.
    Joomla Website Development Company Bangalore | Drupal Web Development Company Bangalore

    ReplyDelete
  7. I am extremely upbeat to you to your time to impart this superb post! Much appreciated a decent alternative to impart this website to us.

    Joomla Website Development Company | Responsive Web Design Company in Bangalore

    ReplyDelete
    Replies
    1. I bet you will also like this blog then http://subhojitpaul.blogspot.com/2014/10/use-vim-as-ide-for-drupal-development.html

      Delete
  8. VIM plugin is suitable for make the possible features in Drupal website development.This blog is really helpful to how to use the VIM plugin in drupal platform.
    Website Designing Company London | Website Designers London

    ReplyDelete

I am eager to hear from you