如何在 vim 中使用缩进进行 Web 开发?
我开始在工作中使用 Linux 和 Vim。我开始阅读 vims 文档并创建我自己的 .vimrc 文件等。
我是一名使用 HTML、XML、CSS、JS、Python、PHP、ZPT、DTML 和 SQL 的 Web 开发人员。
我想要一个缩进功能像这样的:对于每种语言/集合,都有相应的缩进解决方案。
所以,在js中,编写function test(){|}
会变成
function test(){
|
}
如果php,编写:
<?php
function test(){
|
}
?>
...和这样的。用Python编写一个函数定义,然后创建一个for循环语句,它会自动创建一个缩进。
我从 autoindent、smartindent、cindent 开始,但我对它们的差异有点困惑。
vim 中的缩进是如何工作的?我应该为每种语言下载插件吗?我描述的行为是否可以使用您习惯的现有插件实现,还是我必须创建它?
我不断看到人们使用 Vim,我也在尝试这样做,因为我使用的机器太有限,但我担心我无法在其中提供像样的自动缩进解决方案。我真的认为必须一直手动缩进代码(而不是有时)是浪费时间,而且这违背了我所看到的 vim 的“座右铭”,即“生产力”。
(我在 Visual Studio 的一个小项目中使用了自动缩进,并且非常喜欢他们的方法。有相应的插件吗?)
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?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一旦你定义了制表符大小等的正确设置,Vim 通常会非常聪明地进行缩进。 (编辑:正如 Igor 在另一个答案中提到的,一定要打开文件类型特定的缩进。) 看来你希望 vim 自动插入换行符,我认为如果没有插件它就无法做到这一点。
不过,我想你可能想看看 snipMate,它是一个为不同编程语言定义大量“片段”的插件,您也可以定义自己的。它基本上是一种改进的制表符补全:
一个例子:
变成
With |是你的光标。有些片段甚至定义了多个光标位置,您可以通过再次按 Tab 切换到这些位置。
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:
turns into
With | being your cursor. Some snippets even define multiple cursor-positions which you can switch to with another press of tab.
vim 通常带有一堆针对不同语言的语法插件。如果您想使用它们进行缩进,您将需要:
您可能还需要
syntax on
但我不确定缩进是否需要它。虽然不能伤害...vim usually comes with a bunch of syntax plugins for different languages. if you want to use those for indenting, you will need:
you might also need
syntax on
but i'm not sure if that's needed for indenting. couldn't hurt though...我根据博客文章找到了此设置:
与 snipmate.vim 和 autoclose.vim 一起使用可以完美地工作。
I found the setup for this based on a blog post:
Having this with snipmate.vim and autoclose.vim is working flawlessly.