How do you use indent in vim for web development?

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


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.


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...


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.

链接地址: http://www.djcxy.com/p/49536.html

上一篇: 用Vim缩进多个文件(LaTex)

下一篇: 你如何在vim中使用indent进行web开发?