如何在Vim中自动缩进Ruby源代码?

时间:2021-02-25 00:25:21

suppose I have set cindent in .vimrc
def func() followed by Enter, and then type end, it is indented(not aligned to the def)

How to reindent the end keyword(align it to the def).

Even using endwise.vim plugin doesn't fix the problem
https://github.com/tpope/vim-endwise.git
It adds automatically the end keyword but again indented

假设我在.vimrc def func()中设置了cindent(),然后输入Enter,然后输入end,如何重新插入end关键字(与def不一致)(将它与def对齐)。即使使用竖着。vim插件没有修复问题https://github.com/tpope/vim-endwise.git,它自动添加end关键字,但是再次缩进

4 个解决方案

#1


2  

vimfiles includes ruby code smart indention and a lot of other useful things

vimfiles包括ruby代码智能缩进和许多其他有用的东西

ruby code is automatically formatted like

ruby代码是自动格式化的

class Foo
  def bar
    if xxx
      blah
    else
      blahblah
    end
    barfoo
    barfoo
  end
end

#2


22  

Try using smartindent instead of cindent (which follows C-like indent behaviour), and turn on the filetype specific indent.

尝试使用smartindent而不是cindent(它遵循类似c的缩进行为),并打开filetype特定的缩进。

You'll also probably need to turn off vi compatibility.

您可能还需要关闭vi兼容性。

Try adding this to you .vimrc:

试着给你加上这个。

" Turn off vi compatibility
set nocompatible

set smartindent
set autoindent

" load indent file for the current filetype
filetype indent on

#3


0  

This worked for me.

这为我工作。

" Ruby indentation from http://ubuntuforums.org/showthread.php?t=290462
if has ("autocmd")
    filetype indent on
endif

#4


0  

In my case this is what fixed my indentation issues (e.g. jumps in random places):

在我的例子中,这是解决我的缩进问题的方法(例如在随机位置的跳转):

set smartindent
set noautoindent
filetype indent off

#1


2  

vimfiles includes ruby code smart indention and a lot of other useful things

vimfiles包括ruby代码智能缩进和许多其他有用的东西

ruby code is automatically formatted like

ruby代码是自动格式化的

class Foo
  def bar
    if xxx
      blah
    else
      blahblah
    end
    barfoo
    barfoo
  end
end

#2


22  

Try using smartindent instead of cindent (which follows C-like indent behaviour), and turn on the filetype specific indent.

尝试使用smartindent而不是cindent(它遵循类似c的缩进行为),并打开filetype特定的缩进。

You'll also probably need to turn off vi compatibility.

您可能还需要关闭vi兼容性。

Try adding this to you .vimrc:

试着给你加上这个。

" Turn off vi compatibility
set nocompatible

set smartindent
set autoindent

" load indent file for the current filetype
filetype indent on

#3


0  

This worked for me.

这为我工作。

" Ruby indentation from http://ubuntuforums.org/showthread.php?t=290462
if has ("autocmd")
    filetype indent on
endif

#4


0  

In my case this is what fixed my indentation issues (e.g. jumps in random places):

在我的例子中,这是解决我的缩进问题的方法(例如在随机位置的跳转):

set smartindent
set noautoindent
filetype indent off