user3640967
user3640967

Reputation: 538

How to change VIM PHP auto formatting options

I have tried googling this extensively, but all I can find are plugins which format code in the author's preferred way. What I would like to do is change the auto format options so that I can setup VIM to use the same formatting as the other editors my team uses.

For example, I would like:

public function test($param)
    {
    // code here
    }

rather than:

public function test($param){
    // code here
    }

or

public function test($param)
{
// code here
}

Is this possible without a plugin? Are there formatting templates somewhere that I can edit? Thanks :)

Upvotes: 0

Views: 313

Answers (1)

user5834627
user5834627

Reputation:

Is this possible without a plugin?

Yes.

Are there formatting templates somewhere that I can edit?

Sure. Vim is the most customizable text editor in universe. :)

So, let's start understanding snippets.

Snippets are blocks of text that can be inserted in your text using some pre-defined keys. The idea of snippets is to easily put in your file some chunk of text you use often. Snippets are these "templates" you mentioned.

To use snippets with Vim, you need to install the garbas/vim-snipmate plugin. You probably had it installed, since it seems that you can use them. This plugin search in you .vim folder for .snippets files and open them every time you open a file with predetermined extension. For example, when you create the foo.html file, vim-snipmate plugin searches for the html.snippets file and load it. After that, everytime you type, for example, html and press tab, Vim will write the <html> tag, because in your html.snippets file there's a snippet telling Vim to do so. Every programming language needs its own .snippets file, and loads it at the start. It's common to have a _.snippets file too, that loads with all file extension. It's a global snippet file.

To edit your snippets, you have to find where are your .snippets files. In Linux, open your terminal and type:

cd ~/.vim
find -name *.snippets

And then you'll see where are your snippet files. Assuming they are ~/.vim/snippets, for example, you open your java snippets with a:

vim ~/.vim/snippets/java.snippets

A .snippets file commonly looks like this: java.snippets file

These +-- lines are compressed lines you can expand and contract typing za in normal mode. In the blue line you always see snippet something written. The something is the shortcut you need to type and press tab when you're editing a file to use the snippet. For example in this java.snippets file there is a snippet called snippet po. So, when you're editing a java file, type po and press tab, Vim will inserted protected {}.

Snippets have a simple language, you can understand a lot just by seeing them in the .snippets file and typing them in another one. If you want to understand more about creating snippets, Google about vim snippets, and you'll find lots of stuff about it.

If you find that you don't have snippets in your .vim folder, or have insufficient ones, you can install a lot of excelent scripts with the honza/vim-snippets extension on Github.

Upvotes: 1

Related Questions