Skip to content

Instantly share code, notes, and snippets.

@JoshDevHub
Created May 6, 2023 03:05
Show Gist options
  • Save JoshDevHub/728f0c8d46b89fde37cf88bd1d6b7211 to your computer and use it in GitHub Desktop.
Save JoshDevHub/728f0c8d46b89fde37cf88bd1d6b7211 to your computer and use it in GitHub Desktop.
My LazyVim Setup

Basic LazyVim Setup with Ruby/Rails

A very basic setup for ruby/rails development using LazyVim

Prerequisites

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.

Ruby/Rails Stuff

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).

LSP config

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.

Treesitter

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.

Vim-Rails

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.

@crespire
Copy link

crespire commented May 7, 2023

Nice! For code folding, you can add nvim-ufo:

-- plugins/nvim-ufo.lua
return {
  {
    "kevinhwang91/nvim-ufo",
    dependencies = { "kevinhwang91/promise-async" },
  },
}

Then configure to use Treesitter in options.lua

--- config/options.lua
local vim = vim
local opt = vim.opt

opt.foldmethod = "expr"
opt.foldexpr = "nvim_treesitter#foldexpr()"

@a-chacon
Copy link

Hey! I was looking the way to format the html.erb files, but I can't do it work. I am new with LazyVim. I try installing the formatter erb-formatter with mason. But still I receive the message "No formatter available" when try to format a html.erb file. Do you know what can I do for making it works?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment