Showing posts with label vim. Show all posts
Showing posts with label vim. Show all posts

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

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.