Some tips for building vim as a custom PHP development tool _php Tips

Source: Internet
Author: User
Tags documentation php development environment php error phpinfo

Although Vim is essentially just an editor. But with some proper plug-ins, vim can become a fully functional IDE as well. The author has been using Vim for a long time, after repeated experiments, configured an efficient PHP development environment, for home travel, murder, pillage the necessary good products.

Install Vim
Since most readers use the Windows environment, this article uses Windows as the operating environment. However, thanks to Vim's excellent cross-platform features, configuration files can be used in Linux versions of Vim simply by modifying them.

The latest version of Vim is currently 7.2, and if there are no specific reasons to recommend installing the latest version.

Download Address: http://www.vim.org/download.php#pc
Download file: Ftp://ftp.vim.org/pub/vim/pc/gvim72.exe

Do not install in a space, the Chinese directory, others remain the default. The author's installation directory is C:/apps/office/vim, and the following text is also subject to this. In order to describe the convenience, the author uses $VIM to represent VIM's installation directory. For example, if your vim is installed in a d:/vim/, the $VIM represents d:/vim/.

Start configuring VIM
Our configuration of vim is divided into several steps.

Automatically take effect after modifying _VIMRC

Open $VIM directory, you can see that there is a _VIMRC file, with VIM Open this file, delete all content, after the last insert two lines:

"AutoLoad _VIMRC
autocmd! Bufwritepost _VIMRC Source%

The above command allows us to automatically load the _VIMRC file when we edit the _VIMRC file through vim and save it so that our customizations to vim take effect immediately (no need to restart vim).

Make Vim in the culture

Add the following text at the top of the _VIMRC:

"Disable VI s compatible mode.
Set nocompatible

"set Encoding=utf-8
set fileencodings=ucs-bom,utf-8,gbk,default,latin1

" use Chinese Help
Set HELPLANG=CN

The above code is to disable the VI compatibility mode (the original VI function is too few, do not need to consider compatibility), in accordance with Utf-8, GBK order to detect file encoding, and set help for Chinese. However, setting set HELPLANG=CN does not immediately see Chinese help, we also have to download the Chinese help file.

Download Address: http://vimcdoc.sourceforge.net/
Download file: vimcdoc-1.6.0.tar.gz

After the Chinese help file is compressed, copy all files from the doc subdirectory in the compressed package to the $VIM/vimfiles/doc directory. Then enter: Help command to see the Chinese helper.

Chinese help is still in the 7.1 edition, but it does not affect our use.

Set font

Select the Vim menu "edit"-> "Select Font", you can specify the favorite display font for VIM. The author uses the Consolas font, the size is set to 9pt. This setting shows that the code is very beautiful, but Chinese is a bit distorted.

When set, enter the command: Set Guifont can view the current font settings and write the settings to the _VIMRC file.

"Set GUI options
if has (" gui_running ")
 set Guifont=consolas:h9
endif

The If ... endif in the above code is a conditional judgment structure. Indicates that the font is set only when we use VIM in the graphical interface version.

Make Basic settings for editing PHP code

Now use vim to open. php files look very difficult to see, not to mention the code highlighted, the line number is not displayed. So also add the following to _VIMRC:

' Enable syntax highlight
syntax enable
 
' show line number
set Nu

' show matching bracets
set SHOWMATC H
 
"Basic editing options
set Expandtab
set shiftwidth=2
 
au FileType html,python,vim,javascript setl shiftwidth=2
au FileType html,python,vim,javascript setl tabstop=2
au FileType java,php setl shiftwidth=4
au FileType java,php setl tabstop=4
 
set smarttab
set LBR
set tw=0
 
"Auto indent
set ai< c18/> "Smart indet
set si
 
" C-style indeting
set cindent
 
"Wrap lines
set Wrap

The above settings enable formatting highlighting, line number display, and parentheses matching, automatic indentation and other editing functions, for most cases you can get the ideal editing experience. However, the support for the. php file is still not perfect, and you need to download a special PHP plugin.

Download Address: http://www.vim.org/scripts/script.php?script_id=1571
Download file: php.tar.gz

Copy the Php.vim to the $VIM/vimfiles/syntax directory.

Set a favorite color scheme

The default color scheme is believed that few people will like, you can see the color scheme and download (hundreds of) through the URL below.

Download URL: http://www.cs.cmu.edu/~maverick/VimColorSchemeTest/index-c.html

This URL lists more than 300 color schemes and the actual display effect, click the scheme name to download to a. vim file. Place the file in the $VIM/vimfiles/colors directory and add it in _VIMRC:

"Set color schema
ColorScheme oceandeep

These two lines of code need to be added to the IF has ("gui_running") ... in the ENDIF code block, for example:

If has ("gui_running")
 set guifont=consolas:h9
 "Set color schema
 ColorScheme oceandeep
endif

After you set it up, it looks pretty much better.:-)

More useful editing settings

Although not specifically for editing. php file settings, but these options can make vim better use, it is recommended to add:

"Sets How many lines of history VIM Har to remember
set history=400
 
" set to auto read when a file is changed fro The M
the outside set Autoread
 
"Have the mouse Enabled All" ""
 
"" "," set Mouse=a "don't redraw, when runnin G Macros. Lazyredraw
Set LZ
 
"Set 7 lines to the Curors-when moving vertical.
Set So=7
 
"The CommandBar is 2 high
set cmdheight=2
 
" Change buffer-without saving
set hid
 
"Ig Nore case when searching
' set ignorecase
set Incsearch
 
' Set magic on
set Magic
 
' No sound on ER Rors.
Set Noerrorbells
set Novisualbell
set t_vb=
 
"How many tenths of a second to blink
set mat=4
 
" Hi Ghlight search Things
set hlsearch
 
"Turn backup off
set nobackup
set NOWB
set Noswapfile
 
"Smart backspace
set backspace=start,indent,eol
 
" switch buffers with Tab
map <C-Tab>: Bn <CR>
Map <S-Tab>:bp<cr>

After some transformation, our vim has been able to easily edit the. php file. But there's still a lot of work to be done to build a PHP development environment.

Build PHP IDE


On the left side of the IDE is directory navigation, the middle is the editing area, and the right side is a list of methods for quick jumps in files that have already been opened. Pressing the CTRL+X key in the editing area also displays a list of open files.

Other functions, such as automatic completion, code template and so on, everything. After looking at the beautiful screenshot, let's Build the PHP IDE step by step.

Using Nerdtree to realize directory navigation

It is normal to edit multiple files at the same time when developing PHP applications. So you have to have a handy directory navigation tool to quickly switch between directory structures to find the files you need to edit.

VIM provides this kind of function the plug-in many, more well-known has project, Winmanager and so on. But I personally think it is best to use the NERD tree this plugin. Nerdtree can not only display the complete tree structure, but can also set any directory as the root directory. and provides the directory navigation bookmark function, is very convenient.

Download Address: http://www.vim.org/scripts/script.php?script_id=1658
Download file: Nerd_tree.zip

When decompressing, the directory structure in the compressed package is decompressed to the $VIM/vimfiles directory. When you are done, you should find $VIM/vimfiles/doc/nerd_tree.txt files and $VIM/vimfiles/plugin/nerd_tree.vim files separately. Then enter the command in VIM: Helptags $VIM/vimfiles/doc to add Nerdtree's help document to Vim.

Finally, add the following in _VIMRC:

"Nerdtree
map <F10>:nerdtreetoggle<cr>

After restarting Vim, press the F10 key, you can see a directory tree on the left. Press in the Directory tree window? Key to view detailed help information.

The most common operation keys are:

Also enter a table of contents in the tree window: The bookmark name can also add the directory where the cursor is located to the Favorites folder. Next use: The Bookmarktoroot favorite name can go directly to the directory and take the directory as the root directory. More commands can refer to the Nerdtree Help documentation.

Implementing code Navigation with TagList

Solve the directory and file navigation problems, we also want to jump between the code to provide auxiliary means, taglist is such a plug-in. TagList can list the classes, functions, constants, and even variables defined in the open file.

Download Address: http://www.vim.org/scripts/script.php?script_id=273
Download file: Taglist_45.zip

The package needs to be fully decompressed to the $VIM/vimfiles directory, and the Help documentation for TagList Plug-ins is indexed with the following: Helptags $VIM/vimfiles/doc command. TagList Plug-ins need to rely on ctags programs to work. The Ctags version currently used is exuberant ctags.

Download Address: http://ctags.sourceforge.net/
Download file: Ec57w32.zip

Just copy the Ctags.exe in the compressed package to the $VIM/vim72 directory. Ctags.exe should be in a directory with Gvim.exe.

Finally, add the following content in _VIMRC, set up the TagList plug-in:

"" "" "" "" "" "" "" "" "" "" "" "" "" "" "
"" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
Tlist_auto_open = 1 Let
tlist_auto_update = 1 let
tlist_close_on_select = 0 let
tlist_compact_format = 0
Let Tlist_display_prototype = 0 let
tlist_display_tag_scope = 1 let
tlist_enable_fold_column = 0
Let Tlist_exit_onlywindow = 0 Let
tlist_file_fold_auto_close = 0 let
tlist_gainfocus_on_toggleopen = 1
Let Tlist_hightlight_tag_on_bufenter = 1 Let
tlist_inc_winwidth = 0 let
tlist_max_submenu_items = 1
Let Tlist_max_tag_length =
tlist_process_file_always = 0 let
tlist_show_menu = 0 Let
tlist_show_one_ File = 0 Let
tlist_sort_type = ' order ' let
tlist_use_horiz_window = 0 let
tlist_use_right_window = 1
Let Tlist_winwidth =
tlist_php_settings = ' php;c:class;i:interfaces;d:constant;f:function '

Check the PHP syntax format in vim
Open your _VIMRC and write and save the following configuration.

"Check the current file code syntax (PHP) {{}

function! Checksyntax ()
 if &filetype!= "php"
 echohl warningmsg | echo Fail to check syntax! Please select the right file! | Echohl None
 return
 endif
 if &filetype== "php"
 Check php syntax
 setlocal makeprg=\ "php\" \-l\ -n\-d\ html_errors=off
 "Set shellpipe
 setlocal shellpipe=>
 " Use the error format for parsing PHP error out Put
 setlocal errorformat=%m\ in\%f\ on\ line\%l
 endif
 execute ' silent make% '
 set MAKEPRG=MAKE
   execute "Normal:"
 Copen "
endfunction
map <F6>: Call Checksyntax () <CR>
"} }}

And then we hit the F6 and we can do it.

If &filetype!= "php"
 echohl warningmsg | echo "Fail to check syntax! Please select the right file! | Echohl None
 return
endif

In this paragraph, it is stipulated that the file must be a. php format, so don't try to get the wrong file.

OK, let's test, create a new php file phpinfo.php, write the following code

<?php phpinfo () >>?>

Of course, it's obvious that the syntax is wrong, press F6, and the results are as follows:

Amended to

<?php phpinfo ()?>

and save, press F6, the results are as follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.