Ruby indenting in Neovim
One annoyance I had to work through when I started using Neovim was an auto-indenting issue for Ruby code.
Whenever I would write a method, the first line inside the method that should be indented one step in would be auto-indented to align with the previous line as soon as a .
is typed.
# I would start by writing this:
def index
@posts =
# but as soon as I entered a "." the auto-indenting would change it to this:
def index
@posts = Post.
The issue is a result of the Treesitter parser recognizing .
as an indent key. Not using Treesitter or disabling Treesitter's auto-indenting are two ways to resolve the issue, but if you're looking to continue to use Treesitter and its parsing (as I was), you'll need to use a different approach.
A way to address the issue while still using Treesitter is to add the following to your Neovim config.
vim.cmd([[autocmd FileType ruby setlocal indentkeys-=.]])
This code removes .
as an indent key so that auto-indent is no longer triggered when a .
is typed.