How to Autoindent Ruby source code in Vim
-
18-03-2021 - |
Question
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
Solution
vimfiles includes ruby code smart indention and a lot of other useful things
ruby code is automatically formatted like
class Foo
def bar
if xxx
blah
else
blahblah
end
barfoo
barfoo
end
end
OTHER TIPS
Try using smartindent
instead of cindent
(which follows C-like indent behaviour), and turn on the filetype specific indent.
You'll also probably need to turn off vi compatibility.
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
This worked for me.
" Ruby indentation from http://ubuntuforums.org/showthread.php?t=290462
if has ("autocmd")
filetype indent on
endif
In my case this is what fixed my indentation issues (e.g. jumps in random places):
set smartindent
set noautoindent
filetype indent off