Repl.it code auto-format for C++ and Go
Repl.it is a cloud IDE supporting numerous languages. It lacks one thing though - code auto formatting for some of them. Here's how to fix that.
3 min read
When I'm thinking about where to try out some new language - repl.it comes to my mind first. But for a long time I found it hard to code in the environment without auto-format even for small snippets. It works for Python, JS, but doesn't work for some other languages. However, repl.it allows you to control your virtual machine from the console - and that gives a chance for a fix.
How auto-format should work?
Usually, auto-format is triggered by a shortcut, like
Alt-Shift-F, in Replit it is registered but language formatter itself is not installed, so unless they fix this or let us install our own (via command line, etc) we have to use other ways.
After some digging I found 2 ways we can trigger our format commands:
Replit runs on google cloud pre-emptible VM-s. That means your code can change hosts from time to time and whatever you do in console or cloud environment outside your work folder will be gone once host changes.
Triggering commands manually
While all your program output goes into Console tab (you are able to run commands in it though), you still have access to a separate tab Shell This is where we can test our formatting commands. Default ones:
gofmt -w .
clang-format -i *.*
You might stop there and use them in shell when you want some formatting, but they will be erased from shell cache once host changes - in case of complicated commands that might be a problem. The solution - to create a script
shfile and trigger it from console
Run format when project files change
To watch file changes you can use whatever method/tool you want. When using free accounts better if that tool is simple and installs fast, as you'll need to reinstall it on launch.
For example, you can use when-changed python package just by typing:
pip install when-changed
Then you can setup watching like this (C++ example):
when-changed . clang-format -i *.*
This will make your shell busy, and when host changes you need to re-run this. The solution is to use
.replit file (docs):
It requires field
on, so you need to watch the commands replit uses when launching your repl and copy inside. After that you can pass
onBoot option specifying anything you want to run when repl is booting (host changes)
run="clang++-7 -pthread -std=c++17 -o main main.cpp && ./main" onBoot="when-changed . clang-format -i *.*"
That's it! code will be formatted as you type. However I found using this a bit annoying as it conflicts with user input, you might use block while you type
// clang-format off or try the next method.
Run format before the repl runs
Repl run shortcut is
Cntrl-Enter. It's pretty convenient to use for formatting.
Downside - your repl will run as well. We have no choice as it's the only configurable hotkey.
In previous section we already saw
.replit file in action. Logically, we can put auto-format to work when we are starting a repl as simple as:
run="clang-format \*.\* -i && clang++-7 -pthread -std=c++17 -o main main.cpp && ./main"
You can use any of the methods depending on your use case as none of them is perfect and has its drawbacks. Personally, I prefer the last one Run format before the repl runs.
It can be changed even further to trigger auto-format on single
Cntrl-Enterand fast triggering
Cntrl-Entertwice to run the repl. This needs saving a timestamp and checking the time difference. You can do this by creating a complex command to write into the file and read it on
run, for example.
Thanks for reading! My articles are constantly revised and updated, so bookmark if you want to be up-to-date on a topic 🍺