Sunday, October 5, 2014

Use Vim as IDE for Drupal development

View the updated version of this blog here.

In my previous blog How to use vim for Drupal development I have mentioned how to use vim for Drupal development. The blog shows some plugins for code completion, drupal code snippets, and other ways for easily writing code in Drupal. I was using vimrc, some settings for configuring vim. Vimrc provides a collection of plugins and settings so that you can quickly setup vim for Drupal.

In this blog I will show you my collection of plugins and settings, and to be honest I found my collection better than vimrc ;)

You can find the collection here in my github repository, instructions for installing are written there.

The rest of the blog is about the purpose of the plugins I have used and how you can use them.

SearchParty:
This plugin highlights multiple occurrences of a searched word in file.
  • * Searches for the next occurrence of the currently selected visual text.
  • # Searches for the prior occurrence of the currently selected visual text.
  • <C-L> Temporarily clears search highlight.
These are the features I mostly used in this plugin.



Vundle.vim:
This is just another plugin manager for Vim. Earlier I was using pathogen, but I found Vundle much cleaner than pathogen. You just specify the plugins in one place, and rest is handled by Vundle.

auto-pairs:
Another vim plugin for auto closing brackets, quotes, etc. There is also surround plugin that does the same job, but you can specify custom auto pair characters in auto-pairs plugin.

ctrlp.vim:
It is the ultimate plugin for fuzzy finder in vim. If you have used sublime text then you will find this feature familiar, the only difference is that you can customize it to best match your needs. Note that, after you select a file to open, you can open it in new tab by pressing <ctrl>-t



neocomplete.vim:
An autocomplete plugin for vim. Earlier I have used YCM for autocomplete. From usage perspective, they are very much similar. I recommend using neocomplete, because it is much lighter and consumes less resource than YCM. neocomplete is completely written in VimScript, while YCM requires python. Some python script will run alongside YCM and it provides the autocompletion feature for vim, and this is the reason your vim will consume ~500MB or may be more. In neocomplete your vim will be satisfied under ~30MB.



nerdcommenter:
This plugin eases the comment toggle in Vim. Mostly I use <leader>ci for inverting comment for a particular block of code. Other than that, you can use <leader>cm to enclose block of code within comments.



nerdtree:
I rarely use this plugin :) But I will tell you the unique feature of this plugin. It allows you to browse files within the current working directory. I always use ctrlp for opening files, and rarely use nerdtree. I guess in future I will remove this plugin from my collection. If you are using my vimrc settings, then you can toggle nerdtree by <F7> key




syntastic:
It is the syntax checking plugin, and that is all it does. And like other plugins you can customize it too to best suit your needs.



vim-airline:
One of the best plugins I have seen. It makes the vim status bar more useful and informative. You can see file format, current branch, file name, etc. in vim status bar. And again, customize it as you want.



vim-better-whitespace:
I helps you identify whitespaces. You can even strip whitespace on save by this command :ToggleStripWhitespaceOnSave You can identify whitespace by writing some code in .vimrc, but I prefer using plugins, it makes the vim configuration more modular.



vim-colorschemes:
A nice collection of vim colorschemes. Choose between dark, light colorschemes and make your vim more sexy :p My favourite is "badwolf" and "molokai".

vim-fugitive:
It is said to be the ultimate git wrapper for vim, but alas I am not using it much. If used with vim-airline it will show current branch in status bar.

vim-session:
A nice plugin that helps you to maintain multiple session in vim. Suppose, you are working on a project, you are writing some custom module and want to finish your work tomorrow, so you will want vim to open the same module file for you the next day, vim-session will do that for you. It also allows you to jump between sessions. Earlier I used to manually open files in vim, and I find it cumbersome.

Commands that I use frequently:
:SaveSession mysession - will save the current session
:OpenSession mysession - open your session



vim-snipmate:
Expand if, for, foreach statements with the press of <tab> key.



vim-snippets:
A collection of PHP snippets, it also includes snippets for other languages say javascript. It works with vim-snipmate.



Last and the most interesting:
drupal-snippets:
Thanks to technosophos for taking the effort of writing drupal snippets. Now you can expand hook_menu, hook_form_alter, hook_block_info, hook_permissions, etc. in the blink of an eye (press <tab> key). If you are not finding snippets for some hook, I recommend fork technosophos's repository and add your snippets there. Or you can add pull requests to his repository.



You can find the plugins and settings in this repository drupal-vim. Please star it, if you like it. Fork it, if you want to add more features.

Thanks for flying Vim :)

Thursday, July 17, 2014

Drush autocomplete in bash shell

View updated version of this blog here.

For shell users like me, Drush autocomplete is a bliss. This blog will tell you how you can autocomplete Drush commands in bash shell. The commands provided here will work for Ubuntu systems, I am not sure whether they will work for other systems as I do not know where Drush is installed for other systems.

  1. Install Drush in your system. If it is already installed, skip to step 2. Otherwise, you can install it from here
  2. Drush already provides you with bash aliases and autocompletion scripts. We just need to place them in right place.
  3. Execute cat /usr/share/php/drush/examples/example.bashrc >> ~/.bashrc This will append required bash aliases to your current bash settings.
  4. Execute sudo ln -s /usr/share/php/drush/drush.complete.sh /etc/bash_completion.d/ This shell script does the smart autocompletion, and here we link the source with existing bash settings. The script does the smart autcomplete job like, suggesting modules to be enabled/disabled.
  5. All settings are done. Just restart the shell ("exit" the current shell and open a new shell) and you are good to go.
List of bash aliases that you will get:
# Aliases for common drush commands that work in a global context.
alias dr='drush'
alias ddd='drush drupal-directory'
alias dl='drush pm-download'
alias ev='drush php-eval'
alias sa='drush site-alias'
alias lsa='drush site-alias --local'
alias st='drush core-status'
alias use='drush site-set'

# Aliases for drush commands that work on the current drupal site
alias cc='drush cache-clear'
alias cca='drush cache-clear all'
alias dis='drush pm-disable'
alias en='drush pm-enable'
alias pmi='drush pm-info'
alias pml='drush pm-list'
alias rf='drush pm-refresh'
alias unin='drush pm-uninstall'
alias up='drush pm-update'
alias upc='drush pm-updatecode'
alias updb='drush updatedb'
alias q='drush sql-query'

You can write "drush en" and then double press "tab" key for suggestions, you will see list of modules.

Update:
The location of drush.complete.sh may vary according to how you install drush. To find where the script resides, execute sudo find / -name "drush.complete.sh". Suppose the location is /home/subhojit/.composer/vendor/drush/drush/drush.complete.sh, you then execute the command sudo ln -s /home/subhojit/.composer/vendor/drush/drush/drush.complete.sh /etc/bash_completion.d/

Saturday, March 1, 2014

Quick and easy Drush sql sync

View updated version of this blog here.

Drush provides an excellent tool sql-sync to sync database between two machines. I needed this tool to synchronize my local database with production database. Simple use of drush sql-sync was slow, I used it with some options and it was easy and quick. I am going to show you how I did it.

You need to create Drush alias for your Drupal site. Create aliases for local and production setup. If you don't know how to create Drush alias, you can learn it from here. I will use an option for sql-sync that can only be used through Drush alias setup.

Here is a sample Drush alias setup: (mysite.aliases.drushrc.php)

<?php

$aliases['local'] = array (
  'root' => '/var/www/mysite',
  'uri' => 'http://mysite.local',
  'path-aliases' =>
  array (
    '%drush' => '/usr/share/drush',
    '%site' => 'sites/default/',
    '%files' => 'sites/default/files/'
  ),
);
$aliases['live'] = array (
  'root' => '/var/www/mysite',
  'uri' => 'http://mysite.com',
  'path-aliases' =>
  array (
    '%drush' => '/usr/share/drush',
    '%site' => 'sites/default/',
    '%files' => 'sites/default/files/'
  ),
  'remote-user' => 'some-user',
  'remote-host' => 'xx.xxx.xxx.xx',
  'source-command-specific' => array(
    'sql-sync' => array(
      'create-db',
      'no-cache',
      'skip-tables-key' => array('*cache*'),
    ),
  ),
);


If you see the live part of the alias code, I have used source-command-specific, it means if I use live as source any Drush specified inside it will be executed, in our case it is sql-sync.

The options I have used:
create-db - Means create new database. Default is, import will be merged with current database.

no-cache - Do not cache sql-dump

skip-tables-key - Do not import cache tables, as they will be created dynamically. Comma separated multiple keys can also be provided. It can only be used through alias file.

After this alias setup execute drush sql-sync:
drush -v sql-sync @mysite.live @mysite.local