A very basic setup for ruby/rails development using LazyVim
First, you'll of course need neovim. I personally just use the stable release over nightly just because I dislike when things randomly break, and I have to stop working to deal with it. But do whatever you like.
Probably a good idea to start it and run :checkhealth
to make sure everything works before proceeding.
You'll also likely want a NerdFont installed so that you'll have nice icons in your terminal.
Next, visit the LazyVim Doc Site's installation page and follow the steps. This should set you up with a clean LazyVim config.
Out of the box, the experience with ruby/rails is going to be lacking. So let's change that!
LazyVim allows you to add and customize its existing plugins via the plugins/
directory in your config. So you'll want to navigate there (full path should be $HOME/.config/nvim/lua/
, where should be in a folder with config/
and plugins/
directories).
The first order of business will be setting up a Ruby language server. Fortunately this is very easy. Add a file named solargraph.lua
to your plugins
folder and give it the following contents:
return {
{
"neovim/nvim-lspconfig",
---@class PluginLspOpts
opts = {
---@type lspconfig.options
servers = {
solargraph = {},
},
},
},
}
Very easy stuff. This should give you autocompletion and snippets when working in a ruby file.
The next thing to take care of is adding treesitter parsers. These enhance neovim's understanding of the language's AST, which leads to things like enhanced syntax highlighting, indenting, navigation, etc.
Let's create a file in the plugins/
directory called treesitter.lua
. In this file, we want to have the following:
return {
{
"nvim-treesitter/nvim-treesitter",
dependencies = { "RRethy/nvim-treesitter-endwise" },
opts = function(_, opts)
opts.endwise = { enable = true }
opts.indent = { enable = true, disable = { "yaml", "ruby" } }
opts.ensure_installed = {
"bash",
"embedded_template",
"html",
"javascript",
"json",
"lua",
"markdown",
"markdown_inline",
"python",
"query",
"regex",
"ruby",
"tsx",
"typescript",
"vim",
"yaml",
}
end,
},
}
Okay so I have a couple things going on here. One is that I pull in the endwise
treesitter plugin so that if you type a ruby expression that needs an end
, it'll automatically be inserted for you. Another is that I disable treesitter's indenting for the ruby language. This is because, at least for me, letting treesitter take control of indenting feels terrible with ruby (and same with yaml while I'm at it).
The other big thing is the list of the parsers to install. Most of these are the default parsers that LazyVim comes with anyway, but I've added two important ones. Obviously one of them is the ruby
parser, but another is the embedded_template
parser. This parser will give you a much better experience writing ERB templates.
The final thing I use, at least for now, is Tim Pope's vim-rails plugin, which I think is sick for navigating Rails projects. Create a new file in the plugins/
directory called vim-rails.lua
and paste the following in:
return {
{ "tpope/vim-rails" },
}
Doesn't get much easier than that. If you want to learn more about that plugin, just visit the repo here.
And that's kind of it. Really simple stuff. I do have more things configured than this, and you can poke around my dotfiles to see the other stuff. My other configurations are centered around changing or disabling some of the defaults LazyVim has set up. Definitely encourage people to read the doc site to learn more about how to do this. I'd say one of the things I like about LazyVim so far is how easy it is to change things compared to other pre-built configurations.
Nice! For code folding, you can add
nvim-ufo
:Then configure to use Treesitter in
options.lua