简体   繁体   English

在Vim中格式化Ruby代码

[英]Format Ruby code in Vim

Just moving over to Vim at the moment. 现在转移到Vim。 In TextMate I could format code by hitting Cmd - Alt - [ . 在TextMate中,我可以通过按Cmd - Alt - [来格式化代码。 How do I achieve the same in Vim? 我如何在Vim中实现同样的目标?


See the answer below for the command. 请参阅以下答案以获取该命令。 I found I also needed the following in my .vimrc so that Vim knew how to autoindent Ruby. 我发现我在.vimrc也需要以下内容,以便Vim知道如何自动提交Ruby。

if has("autocmd")
  filetype indent on
endif

Vimcasts has a useful screencast on this subject that you may be interested in Vimcasts一个关于这个主题的有用的截屏视频 ,你可能会感兴趣

gg=G

gg => go to start of file
=  => apply autoformatting
G  => ... to the end of file

When I see questions about Vim and reformatting and reindenting, I usually feel confusion. 当我看到关于Vim和重新格式化和重新定位的问题时,我通常会感到困惑。 But it is pretty easy. 但这很容易。

Reindenting, done with = key, is a process of shifting line indetation without inserting any line ends - no hard wrapping. 使用=键完成的重新定义是一个在不插入任何行结束的情况下移动行不对齐的过程 - 没有硬包装。 Simply put, beginning columns of the selected lines can change, but the content cannot. 简单地说,所选行的起始列可以更改,但内容不能。

On the other hand, reformatting is complete rewrite of a selected piece of code. 另一方面,重新格式化是完全重写所选代码。 Simply put, everything is deleted and written again according to the language rules defined in Vim. 简单地说,根据Vim中定义的语言规则删除所有内容并重新编写。 Easy, huh? 好吗,对吧?

The usual patern for reindentation is to go to the beginning of the file ( g g ), change to line selection ( V ), go to the end of the file ( G ) and perform reidentation ( = ). 用于重新注册的常用模式是转到文件的开头( g g ),更改为行选择( V ),转到文件的末尾( G )并执行重新定位( = )。

That's indenting in vim : g g V G = 那是在vim中缩进g g V G =

Reformatting pattern starts with the very same keys ( g g V G ), but instead of equal key, you do g q - reformat Vim command. 重新格式化模式以相同的键( g g V G )开始,但不是使用相同的键,而是执行g q - 重新格式化Vim命令。

That's formatting in vim : g g V G g q 这是vim中格式g g V G g q

This works out-of-box in every Vim instance, even with plain text. 这在每个Vim实例中都是开箱即用的,即使是纯文本也是如此。 Only when Vim does not understand the programming language you need to provide it with correct formatting rules (usually bunch of .vim files which have to go to the .vim directory structure). 只有当Vim不理解编程语言时,才需要为它提供正确的格式化规则(通常是一堆必须转到.vim目录结构的.vim文件)。

Reformatting for Ruby works only when plugin vim-ruby is installed. 只有安装了插件vim-ruby时,重新格式化Ruby才有效。

I had to publish this on my blog ;-) Isn't Vim cool? 我不得不在我的博客上发布这个;-) Vim不是很酷吗? It is. 它是。

Try: 尝试:

gg=G

in normal mode. 在正常模式下。

If you're looking for more than just indentation, have a look at ruby-beautify . 如果您正在寻找的不只是缩进,请看看ruby-beautify It can be integrated with vim through vim-autoformat . 它可以通过vim-autoformat与vim集成。

I released a VIM plugin that will do some more comprehensive formatting for Ruby files. 我发布了一个VIM插件,它将为Ruby文件做一些更全面的格式化。 In addition to indenting, it does things like remove trailing whitespace, and consistently spaces out method declarations: 除了缩进之外,它还可以删除尾随空格,并始终将方法声明空格化:

vim-autoformat-rails VIM-自动套用格式护栏

对于小缩进,请尝试: =}=)

ggVGgq将根据当前文件类型重新格式化整个文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM