Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Autoindent Ruby source code in Vim

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

like image 583
user815693 Avatar asked Dec 16 '11 15:12

user815693


People also ask

How do I Autoindent in Vim?

To automatically indent when editing a file in Vim, enable the auto indenting feature using the :set autoindent flag in command mode: Press Enter, and this will auto-indent the file you are currently editing. If you set the auto-indent feature in Vim in command mode, it does not persist upon closing the editor.

How do I fix tabs in Vim?

Fix indentation in the whole fileStart in the top of a file (to get there, press gg anywhere in the file.). Then press =G , and Vim will fix the indentation in the whole file. If you don't start in the beginning of the file, it will fix indentation from current line to the bottom of file.

How do I turn off auto indent in Vim?

How to Turn Off Vim Auto Indent. You can also temporarily turn off automatic indenting by typing :set paste in command mode. (:unset paste to turn it back on again.) This is useful — as you might guess from the command name — if you're pasting in chunks of text or code to avoid getting annoying extraneous indents.

How do I format in Vim?

It's possible to reformat an entire file, or a section of a file, using Vim's built-in = filter. Vim veterans often find this operator to be one of the most useful in their repertoire, but so common that it becomes second-nature and is rarely mentioned. In normal mode, typing gg=G will reindent the entire file.


2 Answers

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
like image 108
m0tive Avatar answered Sep 28 '22 05:09

m0tive


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
like image 45
zed_0xff Avatar answered Sep 28 '22 05:09

zed_0xff