Emacs setup for rails (7)

I looked around and maybe someone is/was still using emacs with rails in the last years and willing to share the setup/modes/autocompletion etc used. I am learning and tried rinari but now just using web-mode. Any insights much appreciated, thank you.

2 Likes

The most Rails-specific package I’m using is robe-mode – its ability to jump to the definition of any piece of code, either within the current project or in any of the used gems is really helpful. Despite Ruby being very dynamic, it works well enough most of the time – sometimes you have to choose from a list of alternatives where to jump to. It runs a console process in the background using inf-ruby (make sure inf-ruby is at the latest version) with the current Rails app loaded, therefore only one project can be loaded at a time. Also it requires a bundled pry, but I can live with that. Maybe the same or a better experience can be provided with eglot and an LSP server, but thanks to robe-mode I haven’t felt the need for it.

I’m using chruby to activate a project’s ruby version within Emacs. This is a necessity when Emacs wants to do project specific Ruby calls obviously.

Then there’s rspec-mode – really handy to quickly run the example or context at the current line. It outputs to a standard compilation buffer; if there are failures the backtrace lines are clickable links, and with C-u g the last rspec command can be modified to rerun. If a system test fails and outputs the link to a screenshot, I use find-file-at-point to view the screenshot (I wish it was clickable as well, or even better, displayed inline in the compilation buffer). Not sure if there is an equivalent for minitest, I wish I had one set up every time I encounter a project without rspec.

bundle-open is useful from time to time as well.

I have custom functions to generate and run migrations (possibly taken from rinari) – a little timesaver but not crucial. I’m happy to use the command line rails for generators, rake tasks etc., no need to move everything inside Emacs in my personal opinion.

flymake to run rubocop inside Ruby buffers, but not activated by default – it’s not useful when reading other people’s code.

Of course there is ruby-mode to edit Ruby files, and it makes sense to configure it to be activated for special files such as Gemfile. From time to time its indentation doesn’t do what I want, so I switch to enh-ruby-mode – don’t take this as a recommendation though, it’s just due to my inability to figure out how to fix this :wink: . ruby-tools has a few useful functions, notably ruby-toggle-string-quotes.

And of course tons of other not Rails specific Emacs packages that make up a good developer experience (magit :clap:!), no point in listing them all here.

1 Like

therefore only one project can be loaded at a time

I re-read GitHub - dgutov/robe: Code navigation, documentation lookup and completion for Ruby documents and found out I was wrong on this one, it’s documented there how to run it for several projects at once.

1 Like

Hi there Til, thank you so much for taking the time to answer in such detail, great. Very helpful.

robe-mode is very helpful indeed - did not know about it, installed and after removing rvm and simplifying my setup it is happy and runs now. oh and company-robe - great autocompletion

chruby and ruby-install: I used rvm but this article makes sense to me so I switched. Using the .ruby-version files for jekyll too - I see rails has them by default?

rspec-mode: I installed it and it works (I think) just have to learn about how to use it.

bundler and bundle-open - yes :slight_smile:

still playing with enh-ruby-mode and ruby-tools but yes, - I took all your advice/tips and implemented them and quite happy with this setup now. I use yasnippets and magit and I like hyperbole for looking up things and its M-RET magic. And I also use the commandline but vterm is really nice now in 29.

I’ll come back to this thread, just wanted to let you know how much I much appreciate your advice and that it works for me.

1 Like

Happy to hear that these are working for you. The article on chruby makes sense, yes, and I don’t mean to bash on rvm and rbenv – I’ve used both in the past and they were useful then, but chruby is much simpler. E.g. in emacs I can do M-! env [RET] and inspect PATH, GEM_PATH etc. if anything is not working, and even change them manually with setenv if needed.

I’ll have a look at vterm, sound promising.

I got eglot running to take a look but I like robe-mode with documentation lookup better - here is what it looks like:

I did place the auto.sh but it seems that vterm does not pick it up on dirchange so I made a function where it goes to the proper dir, reloads bashrc and reports rails and ruby version. Then I have the ssl server running in terminal and use vterm for generating, console etc. - very nice setup so far. it seems I can close a project and its buffers and run another one and robe does not have to be restarted?

1 Like

I’m using Emacs for Rails development for many years now. No sophisticated ide-like setup though. These are basically the tweaks that make it for me:

  • enabling ruby-mode for more than .rb file, e.g. .rake, Gemfile, etc.
  • modes for Yaml, Sass, Haml, etc.
  • selecting a region and shifting it right/left - copied Elisp code from somewhere
  • macros / keybinding for common tasks like rgrep, goto-line etc.

If you are interested, find my .emacs file here.

2 Likes

A promising development is the integration of Treesitter in Emacs – a library that knows the syntax of many programming languages and can provide structural operations on source code, ideally replacing complicated regexps that are being mostly up to now.

In Emacs 29, ruby-mode has been split into a ruby-base-mode from which the original ruby-mode inherits, but there’s also a ruby-ts-mode now that uses Treesitter. I’ve tried it a bit and found it very promising, with still some quirks. Definitely something to keep an eye on in the future.

Hey Thank you for the init - I noticed that I was editing my de.yml in fundamental mode :slight_smile: so much better now. The C-m seems to be in place already in yaml-mode? And the shift region looks very useful too. Happy to see I am not alone. Heres my wallpaper for anyone interested - http://0x0.st/H2oJ.png (magnetic rails it seems)

treesitter looks very nice yes. keeping an eye on it. ( I even got it installed for trying out but too happy with the current setup as a beginner).

I found now that learning ruby on rails with copilot and chatgpt4 might be a thing for people like me who are medium smart. Pretty satisfying to build something from formulating what I think I want, getting immediate feedback that works and explanations. Its really great because I have enough knowledge to suggest more DRY ways of doing something chatgpt suggested while not enough experience to get annoyed with the AI.

I upgraded to plus today and I am using xenodiums chatgpt-shell in emacs but I can’t get it to use the gpt-4 model, customize says I can customize the model value but it does not work. (there is no model 4) Sticking with 3.5-turbo at the moment and using the 4 droid in a browser. Early days but I will report back or blog about it hey.