Thoughts about tech, programming, and more.

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.

Indent getting reset while typing · Issue #2566 · nvim-treesitter/nvim-treesitter
Describe the bug Take the snippet: def foo if bar end Up to this point everything works great. But once you chain a method call to bar, it dedents. Running this in the playground, i think it is bec…

Subscribe to Daniel Lemky

Sign up now to get access to the library of members-only issues.
Jamie Larson
Subscribe