vim php indentation

Posted on 2004-04-28
Last Modified: 2006-11-17
This is admittedly a minor issue, but given how much time I spend using vim, I would very much appreciate an answer.

How can I set vim's indentation such that <?php and the corresponding ?> have the same indentation? Currently when I type ?> the indentation remains as great as the preceding line's indentation, rather than the indentation of <?php. I've tried cindent, autoindent, and even smartindent.

      <span id="StatusText">
      <hr />
            echo "username: " . $username . "<hr />";
            echo "balance: " . $balance . " <hr />";
            <br />
                  echo "hello"

Question by:bobwood2000
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 12

Expert Comment

ID: 10947685

set autoindent
set tabstop=3D8                   " Tabs are always 8 characters!!!
set softtabstop=3D4 shiftwidth=3D4  " default character indentation level
set expandtab                   " use spaces instead of tabs for indentation

set ruler           " show cursor postion
set esckeys         " allow cursor keys in insert mode
set showcmd         " Show (partial) command in status line.
"set hlsearch        " Highlight all matches of the last search pattern (so=
metimes annoying, good for code)
set showmatch       " show matching parens/brackets
set backspace=3D2     " allows for backspace beyond current insert session
set joinspaces      " insert 2 spaces after a period when joining lines
set fileformat=3Dunix " The Right Way(tm)
" Don't be noisy, don't flash the screen
set noerrorbells visualbell
" note : this only works for the console!  it is reset when gvim starts
set t_vb=3D

Author Comment

ID: 10947957
I tried your vimrc file, but it doesn't seem to change anything. I did have to modify it a bit though (eg. changed 3D8 to 3), such that it would work with my version of vim (6.2.426).
LVL 10

Expert Comment

ID: 10948012
I get annoyed with the same thing.

You might find it interesting that the short tags <? and ?> pair up correctly. In fact, the indent file for PHP (the rules about when to indent) say:

      " Indent after php open tags
      if line =~ '<?php'
            let ind = ind + &sw
      if cline =~ '\?\>' " Please fix this...
            let ind = ind - &sw

so even the original writer couldn't get it to work
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.


Author Comment

ID: 10948893
Interesting...what file did those regular expressions come from? And, why does "let ind = ind - &sw" cause the indent to change?

For me the short tags (e.g. <? and ?>) don't cause any change in indenting at all. However, I have to use the long tag (e.g. <?php) since otherwise it confuses the <?xml tag with <?php.
LVL 10

Accepted Solution

eeBlueShadow earned 300 total points
ID: 10949334
OK, I have a solution.

I have Vim installed on Windows, and in the vim62 installation directory there is a folder called indent. In there is a file php.vim (you could just search for a file called php.vim)

Change the following lines:

setlocal indentkeys+=0=,0),=EO
setlocal indentkeys+=0=,0),=EO,<>>

      if cline =~ '\?\>' " Please fix this...
      if cline =~ '?>' " Fixed!

and it should work fine.

LVL 10

Expert Comment

ID: 10949460
The code is written in Vims own script language, type :help vim-script-intro for more info


Author Comment

ID: 10949536
Works wonderfully! Thanks!

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Building a glossary into your website 9 58
php hashing methods 3 49
Can't Find the Homepage on my site 4 46
Load string Array from file 23 42
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question