Welcome to Part two on my Series on VIM for PHP Development
Before we start, I will preface this with the comment that I assume you have read part 1 of this series located here, and that you have a basic understanding of how to navigate and use vim (the vimtutor is a great start).
Today I'm going to talk about the basic configurations in vim that work nicely for coding with PHP. The features that should work by the end of this series are Autobackup, Autoindention, File Type detection and syntax highlighting, spell checking, code folding, and a few other niceties that make life a bit easier.
Autobackup
This is different from autosave. Every time you open a file to edit it, it saves the original, and creates a backup every time you change it. (It only keeps one version back. But you always have the original.) This has saved my butt on more than one occasion, especially when working on complex code that belongs to someone else, I find I make "debug" changes, and have to undo all of them nicely, or I accidentally delete a few lines to many and save... this is a life saver. Some people do not like it because it generates extra files... but those can easily be cleaned up via command line.
To turn on the backup features built into vim at these lines to your .vimrc file:
" Backup Options -> Some People may not want this... it generates extra file s set backup " Enable Backups set backupext=.bak " Add .bak extension to modified files set patchmode=.orig " Copy original file to with .orig extension Before saving.
You can change the extensions to anything you like it is just a matter of preference.
File Type Detection
For many of these features to work you need to enable file type detection so vim can tell what type of file you are editing and "help" you apropriately. To turn on file type detection you need to paste this code into your .vimrc file
" File Type detection filetype on filetype plugin on
Tabs and spacing
Everyone like to look at well formatted code (well most people at any rate...) but no one likes to take the time to type it. I am no exception. This set of options will allow you to control the tab stops and spacing used on the page. (This does not auto indent... it specifies HOW to indent.) There are a lot of idea about proper indention and it all depends on who you ask as to what is right. I personally stick to the PEAR Coding Standard definition of what is right (because they right the CodeSniffer plug-in I use later) however you can chose any standard you like. Some people like 2 spaces, some people like 4 spaces... some people like the tab character... etc... all a matter of preference.
To set you tab and spacing you can place this code in your .vimrc
" Set Tabs and spacing for PHP as recommended by PEAR and Zend set expandtab set shiftwidth=4 set softtabstop=4 set tabstop=4
This basically turns tabs into spaces, and uses 4 spaces for all your various indention. You can change this to anything you like but it is very handy to have it automatically do 4 spaces when I hit tab.
Auto-indention
Ok now that we have our tabs setup... I like to let vim do the indention for me automatically. 99% of the time it gets it right (1% it doesn't...) so it is usually a time saver for me. If you find you are fighting it... you can turn it off. So now when you make a declaration it will automatically indent your code for you. Here is an example:
class ClassName
{
function __construct(argument)
{
// code...
}
}
When I created my class it automatically aligned my {} characters and indented my code. To get it to do this you need to paste the following code into your .vimrc file.
" Set Auto-indent options set cindent set smartindent set autoindent
This turns on C style indents, Smart Indents, and Autoindent. You can view the vim help for more details about them.
Spell check
One of the great features I have discovered in vim 7, is spell checking code... for a very bad speller like my self it is a life saver. Especially since computers care about spelling... close isn't good enough when it comes to variable names (This is a handy way to make sure your vars are correct...) Spell check is simple to turn on, not so simple to use. To turn it on you need to paste the following code into your .vimrc file.
" Enable Spell Checking set spell
That was simple... using spell check is a bit harder, I still have to look up the keys to do this...
- ]s Moves to the next misspelled word
- [s Moves to the previous misspelled word
- z= Brings up the list of spelling options (possible spellings), and you can select your word from a list by number.
- zg Adds a word to the dictionary
- zug Removes a (undo) the addition of a word to the dictionary
Code folding
I am not a huge fan of code folding until I need it... then I'm thrilled with it. Code folding shrinks code down to take up a limited visual space on the screen at any given moment. So I include it here but I don't usually have it turned on by default it just bothers me, however I know many coders that love to have it by default so they can focus on what they are working on. To enable code folding (you must have file type detection on from above) you need to paste this code into your .vimrc file.
" Enable Code Folding set foldenable set foldmethod=syntax
This code tells vim to fold the code using syntax structures for the language you are using (for me that is php). Using Folds took me a while to get the hang of. Here are the most basic commands:
- zo Open the fold under the cursor
- zO Recursively open the fold under the cursor
- zc Close the fold under the cursor
- zC Recursively close the fold under the cursor
There are a lot of other commands and features for folds you can find more information by typing ":help folding" into vim (when in command mode)
Line Length Alert
Some coding standards out there recomend keeping lines of PHP less than 80 characters in length when ever possible. (Like PEAR Coding Standard) It is handy to know when you have crossed over 80, without forcing you to go to the next line. SO, this little peice of code will highlight the text past the 80th character so that you know you have gone to far and that you should go to the next line if possible. If you don't want this don't put it in your .vimrc. (I have mixed feelings about this. when I have to work with other people's code who don't care, it makes the code ugly and unhappy on the eyes... on my code I like it so I can keep my line length in check.) If you want this feature paste this code into your .vimrc
" Show lines that exceed 80 characters match ErrorMsg '\%80v.\+'
You can change the number from 80 to any other standard you like (some standards say 75, others don't care), like everything else it is personal choice.
Line Numbers
How can you code php without line numbers? Well it is possible, but it sure helps when you get an error on line 548 to be able to figure out where line 548 is quickly. So there are a few ways to do that. You can type ':548' while you are in command mode and it will take you to line 548, and/or you can turn on line number and see were line 548 is visually. (I use both... it really helps when comparing diff files.) So if you want line numbers on the screen, paste the following code into your .vimrc file.
" Turn on Line numbers set number
If you decide you don't want line numbers you can always type ":set nu!" and this will turn off line numbers for the rest of your session, to turn them back on you can type ":set nu" or ":set number" again... and it will turn them back on.
Handy helpers for VIM
Status bar and Ruler
One thing that helps me, it may not help you, is the status bar, it tells you the status of what is going on. It tells you where your at and what you are doing. It is located at the bottom of the screen (usually... you can move it if you like, I think..). If you want the status bar ON you can paste the following code in your .vimrc
" Show a status bar set ruler set laststatus=2
File Tabs
One of the nice features (I beleive started in vim7) is the ability to support TABS, just like Eclipse, Netbeans, or Komodo. You can open multiple files in a single instance of vim, and have them separated by "tabs". I use this code to show the tab listing across the top of vim
" Show Tab Bar set showtabline=2
Using tabs is a bit "interesting" and take some getting used to but is very handy once you get the hang of it.
- To open a new tab type the command ":tabe" for "edit" or ":tabnew" This opens a new tab
- To close a tab type the command ":tabc" for close or ":tabclose" This will close the current tab
- To switch between tabs type "gt" (next tab) and "gT" previous tab.
Tabs can do a LOT more, for more information on how to use them and what they can (and can't) do, type ":help tabs" in command mode.
Searching and Finding
Ok, Last but not least is the ability to search and find effectively. As a programmer it is a must. I find I typically want my searches case insensitive, I want them to wrap around the entire document (when it gets to the end start over), and I want it to visually highlight all cases of the search text on the screen. To do this you can paste the following code into your .vimrc file.
" Set Search options highlight, and wrap search set hls is " highlight search text throughout the document. set wrapscan " wrap the scan around the document set ic "ignore case in search
Summary
You should now have vim nicely configured to go general purpose coding for PHP (and other languages too...) In my next section I will talk about some of the plug-ins that I find very helpful to make vim a full fledged PHP IDE, capable of competing with Eclypse, Netbeans, or Komodo.
If you would like to download the .vimrc file "SO FAR" you can do so here:
" Backup Options -> Some People may not want this... it generates extra files set backup " Enable Backups set backupext=.bak " Add .bak extension to modified files set patchmode=.orig " Copy original file to with .orig extension Before saving. " Set Tabs and spacing for PHP as recommended by PEAR and Zend set expandtab set shiftwidth=4 set softtabstop=4 set tabstop=4 " File Type detection filetype on filetype plugin on " Set Auto-indent options set cindent set smartindent set autoindent " Show lines that exceed 80 characters match ErrorMsg '\%80v.\+' " Show a status bar set ruler set laststatus=2 " Show Tab Bar set showtabline=2 " Set Search options highlight, and wrap search set hls is set wrapscan set ic "ignore case in search " Enable Spell Checking set spell " Turn on Line numbers set number " Enable Code Folding set foldenable set foldmethod=syntax
Hopefully this helps people out, it has made my coding much easier.
Page Information:
- Tags: VIM, PHP, Web Development, IDE
- Description: Getting VIM right for PHP Development Part 2
![Validate my RSS feed [Valid RSS]](http://taggedzi.com/skin/images/valid-rss-rogers.png)
