0

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 :)

user3640967
  • 528
  • 2
  • 9
  • 16

1 Answers1

1

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.

  • Thanks this is great! I have one more question though - currently when I type brackets etc they are automatically moved and indented... Isn't this due to auto formatting rather than snippets, since I am not expanding anything with the tab key? How can I change these settings? :) – user3640967 Jan 27 '16 at 09:28
  • Yes, it's not an snippet. There is probably a script creating this behavior. Maybe if you post here your vimrc file I can see what script is doing that. –  Jan 27 '16 at 22:47
  • Ahhh I assumed it was a standard function! Now I've emptied my vimrc it no longer happens - I'll remove them one at a time until I find the culprit. Thanks for your help. I'll accept your answer too since it will achieve what I asked (even though what I asked turned out not to be what I meant). – user3640967 Jan 28 '16 at 13:35
  • It seems to be caused when I set "syntax enable". I thought enabling syntax would just enable the highlighting, not the formatting. Do you know if there's a way I can enable highlighting without it also applying it's formatting rules? I am editing PHP files if that makes any difference. – user3640967 Jan 28 '16 at 13:52
  • 1
    I think the best approach to what you're trying to to is to change the script that's creating the behavior. The `syntax on` looks for scripts in you `.vim` folder and loads them. In the example showed in the question, you want to change just the bracket position. So, if you learn a little of Vimscript you'll be able to change the behavior just by finding the code and altering it. And that's the downside in Vim: you're the developer of your own editor. My advice is: look in all `indent` folders for php scripts, and try to find the code that creates this behavior. –  Jan 29 '16 at 00:42
  • Brilliant - This is exactly what I was looking for! And I don't consider that to be a downside at all ;) – user3640967 Jan 29 '16 at 09:19