Solved

vim php indentation

Posted on 2004-04-28
7
1,144 Views
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.

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


Thanks
0
Comment
Question by:bobwood2000
  • 3
  • 3
7 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility

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
0
 

Author Comment

by:bobwood2000
Comment Utility
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).
0
 
LVL 10

Expert Comment

by:eeBlueShadow
Comment Utility
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
      endif
      if cline =~ '\?\>' " Please fix this...
            let ind = ind - &sw
      endif

so even the original writer couldn't get it to work
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:bobwood2000
Comment Utility
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.
0
 
LVL 10

Accepted Solution

by:
eeBlueShadow earned 300 total points
Comment Utility
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
to
setlocal indentkeys+=0=,0),=EO,<>>

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

and it should work fine.

_Blue
0
 
LVL 10

Expert Comment

by:eeBlueShadow
Comment Utility
The code is written in Vims own script language, type :help vim-script-intro for more info

_Blue
0
 

Author Comment

by:bobwood2000
Comment Utility
Works wonderfully! Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now