如何在Vim中自动添加Ruby源代码

时间:2011-12-16 15:57:10

标签: ruby vim auto-indent

假设set cindent中有.vimrc
def func()后跟Enter,然后输入end,它缩进(不与def对齐)

如何重新加入end关键字(将其与def对齐)。

即使使用endwise.vim plugin也无法解决问题 https://github.com/tpope/vim-endwise.git
它会自动添加 end关键字,但会再次添加indented

4 个答案:

答案 0 :(得分:22)

尝试使用smartindent代替cindent(遵循类似C的缩进行为),并启用特定于文件类型的缩进。

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

尝试将此添加到您.vimrc:

" Turn off vi compatibility
set nocompatible

set smartindent
set autoindent

" load indent file for the current filetype
filetype indent on

答案 1 :(得分:2)

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

ruby​​代码自动格式化为

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

答案 2 :(得分:0)

这对我有用。

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

答案 3 :(得分:0)

在我的情况下,这是解决我的缩进问题的原因(例如,在随机位置跳转):

set smartindent
set noautoindent
filetype indent off