- Harrison Broadbent
This is a quick guide showing you how to use VS Code to edit secrets in your Rails apps when you run
rails credentials:edit. By default, Rails will use the
$EDITORenvironment variable, which is typically
nano; Luckily, It's easy to change, and I've included a handy script to streamline the process!
Table of Contents
I use Rails credentials a lot! They're a handy, native alternative to storing environment variables in a
.env file, and I've previously written about switching from environment variables to Rails credentials.
One small thing that annoys me though is the default editor. A lot of Ruby on Rails developers use VS Code, but
rails credentials:edit will use the
$EDITOR environment variable to edit your secrets. This is typically
nano, both of which can be a bit cumbersome.
To help, I wrote this short article. I'll show you how to use VS Code to edit your Rails credentials instead. I also share a handy script,
bin/credentials:edit, which runs
rails credentials:edit with a default editor exported. Let's go!
Use VS Code to edit Rails Credentials
To edit your Rails credentials using VS Code, you want this command —
EDITOR="code --wait" rails credentials:edit
"So Harrison", I hear you ask, "...what does this command do?"
rails credentials:edit looks at the
$EDITOR variable, we set it to
code --wait. This instructs our command to use VS Code to edit our secrets.
Note: If you leave off the
--waitflag, VS Code will launch, but the
credentials:editcommand won't wait for you to make your edits, so editing your credentials won't work.
Then we run the actual
rails credentials:edit command. Because we've exported the correct
$EDITOR value, this command will launch VS Code, open the credentials file for you to edit, and wait until you save and close the file again. The
rails credentials:edit command will hang until you've closed the file in VS Code.
If all goes well, VS Code should launch and let you edit your secrets, like so —
This is how I typically edit secrets in my Ruby on Rails apps. I rely on the history search function of my terminal (
ctrl + r) to run the command; I'll start typing
EDITOR, and hit enter on the suggestion for the full command, which works well enough.
Lately though, I've been experimenting with wrapping this command up as a little script, which I've shared below.
Note: Of course, you could also export
code --waitas the default value for
.zshrc. I prefer to use
nanofor most things, so the command above sets it inline.
Handy script with a default $EDITOR exported
Recently I've been adding this tiny script to my Ruby on Rails apps; It wraps the command from above and saves you having to search your terminal history to find it again.
Here's the script —
# handy script to run `rails credentials:edit` with EDITOR exported.
export EDITOR="code --wait"
bin/rails credentials:edit $@
$@symbol in the script above is crucial!
Any arguments you pass the
credentials:editcommand come through in the
$@symbol. For instance, if you run
bin/credentials:edit --environment=developmentto specify environment-level credentials,
--environment=developmentwill get passed through as
$@, and the command will work as you'd expect.
You'll need to create the file and make it executable as well; The full commands for doing that are —
# 1. create the file
# 2. edit the file and add the script contents
nano bin/credentials:edit # use whatever editor you like
# 3. make the script executable
chmod +x bin/credentials:edit
# 4. run it!
Enjoy! You can change the name of the script to whatever you want, but I think
bin/credentials:edit makes a lot of sense, since it mirrors the
rails credentials:edit command.
Thanks for reading! I hope you found this quick article helpful. I prefer using VS Code to edit my Rails credentials; I find it easier to copy and paste things around in VS Code compared to
nano, and it's easier to get the correct YAML formatting.