在 Vim 中格式化 Ruby 代码

发布于 2024-09-24 21:48:29 字数 261 浏览 1 评论 0原文

目前正在转向 Vim。在 TextMate 中,我可以通过点击 Cmd-Alt-[ 来格式化代码。我如何在 Vim 中实现同样的效果?


请参阅下面的命令的答案。我发现我的 .vimrc 中还需要以下内容,以便 Vim 知道如何自动缩进 Ruby。

if has("autocmd")
  filetype indent on
endif

Just moving over to Vim at the moment. In TextMate I could format code by hitting Cmd-Alt-[. How do I achieve the same in 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.

if has("autocmd")
  filetype indent on
endif

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

童话 2024-10-01 21:48:29

Vimcasts

gg=G

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

Vimcasts has a useful screencast on this subject that you may be interested in

gg=G

gg => go to start of file
=  => apply autoformatting
G  => ... to the end of file
江湖彼岸 2024-10-01 21:48:29

当我看到有关 Vim 以及重新格式化和重新缩进的问题时,我通常会感到困惑。但这很容易。

重新缩进,使用 = 键完成,是一个在不插入任何行尾的情况下移动行缩进的过程 - 没有硬换行。简而言之,所选行的起始列可以更改,但内容不能更改。

另一方面,重新格式化是对选定的代码段进行完全重写。简单来说,就是根据 Vim 中定义的语言规则删除所有内容并重新写入。容易吧?

重新缩进的通常模式是转到文件的开头 (gg),更改为行选择 (V),转到文件末尾 (G) 并执行重新标识 (=)。

这就是 vim 中的缩进:ggVG=

重新格式化模式以完全相同的键开始(ggVG ),但不是等键,而是执行 gq - 重新格式化 Vim 命令。

这是vim 中的格式ggVGgq

这在每个 Vim 实例中都是开箱即用的,即使是纯文本。仅当 Vim 不理解编程语言时,您才需要为其提供正确的格式规则(通常是一堆必须转到 .vim 目录结构的 .vim 文件)。

仅当安装了插件 vim-ruby 时,重新格式化 Ruby 才有效。

我必须在我的博客上发布; -) Vim 是不是很酷?这是。

When I see questions about Vim and reformatting and reindenting, I usually feel confusion. 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. Easy, huh?

The usual patern for reindentation is to go to the beginning of the file (gg), change to line selection (V), go to the end of the file (G) and perform reidentation (=).

That's indenting in vim: ggVG=

Reformatting pattern starts with the very same keys (ggVG), but instead of equal key, you do gq - reformat Vim command.

That's formatting in vim: ggVGgq

This works out-of-box in every Vim instance, even with plain text. 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).

Reformatting for Ruby works only when plugin vim-ruby is installed.

I had to publish this on my blog ;-) Isn't Vim cool? It is.

椒妓 2024-10-01 21:48:29

尝试:

gg=G

在正常模式下。

Try:

gg=G

in normal mode.

为你鎻心 2024-10-01 21:48:29

如果您想要的不仅仅是缩进,请查看 ruby-beautify
它可以通过 vim-autoformat 与 vim 集成。

If you're looking for more than just indentation, have a look at ruby-beautify.
It can be integrated with vim through vim-autoformat.

撞了怀 2024-10-01 21:48:29

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

ggVGgq will reformat the entire file according to the current filetype

云朵有点甜 2024-10-01 21:48:29

我发布了一个 VIM 插件,可以对 Ruby 文件进行更全面的格式化。除了缩进之外,它还执行诸如删除尾随空格以及始终间隔方法声明之类的操作:

vim-autoformat -导轨

I released a VIM plugin that will do some more comprehensive formatting for Ruby files. In addition to indenting, it does things like remove trailing whitespace, and consistently spaces out method declarations:

vim-autoformat-rails

久而酒知 2024-10-01 21:48:29

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

For small indentation, try:
=}, =)

全部不再 2024-10-01 21:48:29

有人提到。但我在这里分享。以便其他人可以轻松找到它。

ggVG=

Someone mentioned. but I share here. so that other people can find it easily.

ggVG=
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文