1

How do I format PHP code that looks like this

class SomeClass
{
    function insideclass()
    {


    }
} 

into this, using Vim without any external tool?

class SomeClass {
    function insideclass() {


    }
} 

I am not arguing that this is the best way to format the code, but this is what we are following in the team.

Edit: Removed reference to the snippet addon, which caused some confusion about the question.

Sudar
  • 18,954
  • 30
  • 85
  • 131

3 Answers3

4

You have two unrelated problems and you won't find a unique solution to both.

  1. You don't like the default class snippet.

    No problem. If it's not already there, create ~/.vim/snippets/php.snippets and customize it to fit your team's rules by following the other answers. Snipmate is not smart enough to adapt itself to your coding style.

  2. Some of your existing code doesn't conform to your coding rules.

    Snipmate won't help at all since it only deals with insertion, not transformation. You are going to need either some external beautifier or a few macros.

    EDIT

    Here is a very simple command that does exactly what you want on your example. I offer no guarantee that it's going to work for everything everywhere. Take it as a naive starting point.

    :g/^\s*{\s*$/normal kJ
    
    • :g/pattern acts on all lines containing pattern, see :h :global.

    • ^\s*{\s*$ matches all single { whatever the amount of whitespace between them and the beginning of the line.

    • normal executes normal commands, see :h :normal.

    • kJ goes up one line and Joins this line with the matched line.

    • Done.

    ENDEDIT

romainl
  • 186,200
  • 21
  • 280
  • 313
  • Actually my question was more towards the second problem. I just snipate addon as an example. My bad. I have edited the question now to clarify it. – Sudar Apr 12 '12 at 09:13
  • Apart from using beautifier, is there any Vim based solution for the second problem? – Sudar Apr 12 '12 at 09:13
  • No turn-key solution I'm aware of. Maybe someone has written a script based on PHP_Beautifier? I don't know. It looks like a typical use-case for macros and substitutions. – romainl Apr 12 '12 at 11:41
  • +1. I knew that `kJ` was the simplest solution but dint know how to execute it on finding a match. Thanks! – Pavan Manjunath Apr 12 '12 at 12:52
  • Wow!! this was exactly what I was looking for. Simple and elegant. I am going to map it to easy to remember key in my .vimrc. – Sudar Apr 12 '12 at 14:41
2

Why not modify the source code of that addon?

For example, change these:

snippet class
    /**
     * ${1}
     */
    class ${2:ClassName}
    {
        ${3}
        function ${4:__construct}(${5:argument})
        {
            ${6:// code...}
        }
    }

to

snippet class
    /**
     * ${1}
     */
    class ${2:ClassName}{
        ${3}
        function ${4:__construct}(${5:argument}){
            ${6:// code...}
        }
    }
alwaysday1
  • 1,683
  • 5
  • 20
  • 36
  • Yeah modifying the addon is definitely an option. I am just trying to see if it possible to convert code from one style to other in general. – Sudar Apr 12 '12 at 06:57
0

Al the snipmate snippets are stored in a directory. Go to that directory and edit the file you want.

The direcorty is stored in the vim directory and called snippets. In there you see a php.snippets file. Go to that file and on line 70 you can edit the snippet for the class.

Wouter J
  • 41,455
  • 15
  • 107
  • 112