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

13

假设我在.vimrc中设置了set cindent, 在输入def func()后按Enter,然后输入end时,它会被缩进(而不是对齐到def)。
如何重新缩进end关键字(将其与def对齐)?

即使使用endwise.vim插件也无法解决问题。
https://github.com/tpope/vim-endwise.git
它可以自动添加end关键字,但仍然会被缩进

4个回答

24

尝试使用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

我得到了缩进,但是根据文件类型它是错误的?对于Ruby文件,我得到了巨大的制表符缩进,而我应该只得到2个空格? - Redoman
1
禁用 set nosmartindentset noautoindentfiletype indent off - Dorian

1
在我的情况下,以下是解决我的缩进问题(例如,在随机位置跳跃)的方法:
set smartindent
set noautoindent
filetype indent off

1

vimfiles 包含 Ruby 代码智能缩进和许多其他有用的功能

Ruby 代码会自动格式化,就像这样

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

对我有用的是安装 vim-pathogen <br /> https://github.com/tpope/vim-pathogen.git - user815693

0

这对我有用。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接