Tips for building Vim as a custom PHP development tool

Source: Internet
Author: User
Tags php development environment php error
This article mainly introduces some tips for setting up Vim as a custom PHP development tool. Vim and Emacs are recognized as the most powerful and popular code editor in the world, for more information, see vim, which is essentially an editor. But with some appropriate plug-ins, vim can also become a full-featured IDE. I have been using vim for a long time. after repeated experiments, I have configured an efficient PHP development environment, which is a perfect product for traveling at home, killing people and killing goods.

Install vim
Since most readers use Windows, this article uses Windows as the runtime environment. However, thanks to vim's outstanding cross-platform features, the configuration file can be used in vim of Linux only with simple modifications.

Currently, the latest version of vim is 7.2. if there is no special reason, we recommend that you install the latest version.

: Http://www.vim.org/download.php#pc
Download File: ftp://ftp.vim.org/pub/vim/pc/gvim72.exe

Do not install it in a directory with spaces or Chinese characters during installation. keep the default values for other files. The installation directory is c:/apps/office/vim. For convenience, I use $ VIM to represent the vim installation directory. For example, if your vim is installed in d:/vim/, $ VIM indicates d:/vim /.

Start to configure vim
We have several steps to configure vim.

Modified _ vimrc automatically takes effect

Open the $ VIM directory and you will see a _ vimrc file. use vim to open the file. after deleting all the content, insert the following two lines at the end:

" autoload _vimrcautocmd! bufwritepost _vimrc source %

The above command allows us to automatically load the _ vimrc file when editing and saving the _ vimrc file through vim, so that our vim customization can take effect immediately (no need to restart vim ).

Enable culture in vim

Add the following text to the top of _ vimrc:

" disable VI's compatible mode..set nocompatible" set encoding=utf-8set fileencodings=ucs-bom,utf-8,gbk,default,latin1" use chinese helpset helplang=cn

The purpose of the above code is to disable the vi compatibility mode (there are too few original vi functions and there is no need to consider compatibility) and check the file encoding in the UTF-8 or gbk order, set help to Chinese. However, setting set helplang = cn does not immediately display Chinese help. we have to download the Chinese help file.

: Http://vimcdoc.sourceforge.net/
Download File: vimcdoc-1.6.0.tar.gz

After obtaining the Chinese help file compressed package, copy all the files in the doc subdirectory in the compressed package to the $ VIM/vimfiles/doc directory. Enter the help command to view the help in Chinese.

The Chinese help version is 7.1, but it does not affect our use.

Set font

Select "edit"> "select font" in the vim menu to specify the font you like for vim. I use the Consolas font and set the size to 9pt. This setting shows that the code is very beautiful, but the Chinese language is a little distorted.

After setting, enter the command: set guifont to view the current font settings and write the settings to the _ vimrc file.

"set gui optionsif has("gui_running") set guifont=Consolas:h9endif

In the above code, if... Endif is a condition judgment structure. Indicates that the font is set only when we use vim in the graphic interface version.

Basic settings for editing PHP code

Now it looks ugly to open the. php file with vim, not to mention code highlighting, and even the row number is not displayed. Therefore, add the following content to _ vimrc:

" Enable syntax highlightsyntax enable " Show line numberset nu" show matching bracetsset showmatch " Basic editing optionsset expandtabset shiftwidth=2 au FileType html,python,vim,javascript setl shiftwidth=2au FileType html,python,vim,javascript setl tabstop=2au FileType java,php setl shiftwidth=4au FileType java,php setl tabstop=4 set smarttabset lbrset tw=0 "Auto indentset ai " Smart indetset si " C-style indetingset cindent " Wrap linesset wrap

The preceding settings enable the formatting highlight, line number display, and edit functions such as parentheses matching and automatic indent. This gives you an ideal editing experience in most cases. However, the support for. php files is not complete yet. you need to download a dedicated php plug-in.

: Http://www.vim.org/scripts/script.php? Script_id = 1, 1571
Download File: php.tar.gz

Copy the php. vim file to the $ VIM/vimfiles/syntax directory.

Set your favorite color scheme

The default color scheme is expected to be liked by a few people. you can view and download the scheme on the following URL (several hundred ).

Http://www.cs.cmu.edu /~ Maverick/VimColorSchemeTest/index-c.html

This website lists more than 300 color schemes and their actual display effects. click the solution name to download to a. vim file. Put the file in the $ VIM/vimfiles/colors directory and add the following in _ vimrc:

" set color schemacolorscheme oceandeep

These two lines of code need to be added to if has ("gui_running ")... In the endif code block, for example:

if has("gui_running") set guifont=Consolas:h9 " set color schema colorscheme oceandeependif

After setting, the display effect will be much more beautiful :-)

More useful editing settings

Although it is not specific for editing the. php file, these options can make vim easier to use. Therefore, we recommend that you add:

" Sets how many lines of history VIM har to rememberset history=400 " Set to auto read when a file is changed from the outsideset autoread " Have the mouse enabled all the time:set mouse=a " Do not redraw, when running macros.. lazyredrawset lz " set 7 lines to the curors - when moving vertical..set so=7 " The commandbar is 2 highset cmdheight=2 " Change buffer - without savingset hid " Ignore case when searching" set ignorecaseset incsearch " Set magic onset magic " No sound on errors.set noerrorbellsset novisualbellset t_vb= " How many tenths of a second to blinkset mat=4 " Highlight search thingsset hlsearch " Turn backup offset nobackupset nowbset noswapfile " smart backspaceset backspace=start,indent,eol " switch buffers with Tabmap 
 
   :bn
  
   map 
   
     :bp
    
   
  
 

After some transformation, our vim can easily edit the. php file. However, there is still much work to be done to build a php development environment.

Build a PHP IDE


The left side of IDE is the directory navigation, the middle is the editing area, and the right side is the method list, which is used to quickly jump to an opened file. In the editing area, press CTRL + X to display the list of opened files.

Other functions such as auto-completion and code template are all available. After reading the beautiful ones, let's build a php ide step by step.

Use NERDTree to implement directory navigation

It is normal to edit multiple files simultaneously during PHP application development. Therefore, you must have a convenient directory navigation tool to quickly switch between directory structures and find the files to be edited.

There are many plug-ins in vim that provide such functions, such as project and winmanager. But I personally think it is best to use The NERD Tree plug-in. The NERDTree not only displays the complete directory tree structure, but also sets any directory as the root directory. It also provides the directory navigation Bookmarks function, which is very convenient.

: Http://www.vim.org/scripts/script.php? Script_id = 1, 1658
Download File: NERD_tree.zip

To decompress the package, extract the complete directory structure from the package to the $ VIM/vimfiles directory. After that, find the $ VIM/vimfiles/doc/NERD_tree.txt file and the $ VIM/vimfiles/plugin/NERD_tree.vim file respectively. Then enter the command helptags $ vim/vimfiles/doc in VIM to add the help document of NERDTree to vim.

Add the following content in _ vimrc:

" NERDTreemap 
 
   :NERDTreeToggle
  
 

After vim is restarted, press the F10 key to view a directory tree on the left. In the directory tree window, press? Key to view detailed help information.

The most common operation keys are:

In addition, in the directory tree window, enter the directory: Bookmark favorites name to add the directory where the cursor is located to favorites. Next use: the BookmarkToRoot favorites name can be directly transferred to this directory, and this directory is used as the root directory. For more commands, see the help document of NERDTree.

Use taglist for code navigation

To solve the directory and file navigation problems, we also need to provide an auxiliary means for code redirection. taglist is such a plug-in. Taglist can list classes, functions, constants, and even variables defined in open files.

: Http://www.vim.org/scripts/script.php? Script_id = 1, 273
Download File: taglist_45.zip

Decompress the package to the $ VIM/vimfiles directory, and use the helptags $ VIM/vimfiles/doc command to index the help document of the taglist plug-in. The taglist plug-in must depend on the ctags program to work. Currently, the common ctags version is Exuberant Ctags.

: Http://ctags.sourceforge.net/
Download File: ec57w32.zip

Copy the ctags.exe in the compressed package to the $ VIM/vim72 directory. Ctags.exe and gvim.exe should be in the same directory.

Add the following content in _ vimrc and set the taglist plugin:

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" => Plugin configuration"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" taglistlet Tlist_Auto_Highlight_Tag = 1let Tlist_Auto_Open = 1let Tlist_Auto_Update = 1let Tlist_Close_On_Select = 0let Tlist_Compact_Format = 0let Tlist_Display_Prototype = 0let Tlist_Display_Tag_Scope = 1let Tlist_Enable_Fold_Column = 0let Tlist_Exit_OnlyWindow = 0let Tlist_File_Fold_Auto_Close = 0let Tlist_GainFocus_On_ToggleOpen = 1let Tlist_Hightlight_Tag_On_BufEnter = 1let Tlist_Inc_Winwidth = 0let Tlist_Max_Submenu_Items = 1let Tlist_Max_Tag_Length = 30let Tlist_Process_File_Always = 0let Tlist_Show_Menu = 0let Tlist_Show_One_File = 0let Tlist_Sort_Type = "order"let Tlist_Use_Horiz_Window = 0let Tlist_Use_Right_Window = 1let Tlist_WinWidth = 40let tlist_php_settings = 'php;c:class;i:interfaces;d:constant;f:function'

Check 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 error format for parsing PHP error output setlocal errorformat=%m\ in\ %f\ on\ line\ %l endif execute “silent make %” set makeprg=make execute “normal :” execute “copen”endfunctionmap 
 
   :call CheckSyntax()
  
   “}}}
  
 

Then we can press F6 to execute it ~~ Note:

if &filetype!=“php” echohl WarningMsg | echo “Fail to check syntax! Please select the right file!” | echohl None returnendif

This section specifies that the file must be in. php format, so do not try it with the wrong file.

OK. Let's test it. create a php file phpinfo. php and write the following code:

<?php phpinfo()>>?>

Of course, the syntax is obviously incorrect. press F6 and the result is as follows:

Change

<?php phpinfo()?>

Save and press F6. The result is as follows:

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.