views:

284

answers:

3

I'm starting to use Linux and Vim at work. I'm started reading vims documentation and creating my own .vimrc file and such.

I'm a web developer working with HTML, XML, CSS, JS, Python, PHP, ZPT, DTML and SQL.

I would like to have an indent feature like this one: for each language/set, a corresponding indent solution.

So, in js, writing function test(){|} would turn in

function test(){
    |
}

If php, writing <?php function test(){|}:

<?php
    function test(){
        |
    }
?>

...and such. Writing a function definition in Python, and then creating a for loop sentece, it would automatically create an indent.

I'm starting with autoindent, smartindent, cindent but I'm a little confused about their differences.

How do the indent in vim works? Am I supposed to download plugins for each language? Is the behavior I described possible with already existing plugins you're used to or do I have to create it?

I keep seeing people using Vim and I'm trying to do this as well since the machine I'm using is too limited, but I'm afraid I won't be able to have a decent auto indenting solution in it. And I really think that having to manually indent code all the time (instead of sometimes only) is a waste of time and it's against vim's "MOTTO" I've seen called "productivity".

(I have used autoindenting in a little small project in Visual Studio, and really liked their approach. Is there a plugin for that?)

A: 

vim usually comes with a bunch of syntax plugins for different languages. if you want to use those for indenting, you will need:

set autoindent
filetype indent on

you might also need syntax on but i'm not sure if that's needed for indenting. couldn't hurt though...

Igor
+2  A: 

Vim is usually pretty smart about indenting once you define the correct settings for tab size and the like. (Edit: As Igor mentions in the other answer, be sure to turn on filetype-specific indenting.) It seems that you want vim to automatically insert newlines though, which I don't think it can do without a plugin.

However, I think you may want to look at snipMate, which is a plugin that defines a large number of 'snippets' for different programming languages, and you can also define your own. It's basically a kind of improved tab-completion:

One example:

php<tab>

turns into

<?php
|
?>

With | being your cursor. Some snippets even define multiple cursor-positions which you can switch to with another press of tab.

DataWraith
I'm already using snipMate... but I would like to have a bultin solution about indentation instead of creating all the identation I need with plugins, with all the languages I described that I work with.
Somebody still uses you MS-DOS
Hm. If you're not happy with snipMate and 'filetype indent on', I think there is no easy way to do what you ask for without basically parsing the language you're writing in. You could try to use autocmds to script vim to insert the newlines where you want them (via :s/x/y/g) and then invoke vim's "indent this paragraph correctly"-functionality (press {=}). Just defining more snipMate snips would probably be easier though.
DataWraith
Well, you have a point. I'm going to search for this approach, and wait some days to have more answers to my question. Thanks anyway!
Somebody still uses you MS-DOS
Thanks for answering, I'm going to use more the snipmate approach with the solution I provided. Thanks!
Somebody still uses you MS-DOS
A: 

I found the setup for this based on a blog post:

set autoindent
inoremap {<CR> {<CR>}<Esc>O<Tab>

Having this with snipmate.vim and autoclose.vim is working flawlessly.

Somebody still uses you MS-DOS