Automatically adding spaces between operators and operands


I’m looking for a way to automatically add white spaces between operators and operands. Currently, when I type an assignment statement, it looks like this:
int i=3+4;
there are no spaces, unless I manually add them. My codes usually contain very many assignments and it can get a bit unreadable; for convenience and productivity reasons, I’m looking for an automatic way to get:
int i = 3 + 4;

I know atom-beautify might work, but I would rather use a light-weight package; I do not need any other code formatting than just this one. I write in C/C++.


If you can’t find a package, this might be something that you have to bake for yourself.


I would be very proud if I could! I don’t even know where to begin :confused:


So I found the uncrustify beautifier package that has the feature of sp_compare which does what I want. Does anyone know how I can configure this thing? I don’t know where I would go to add sp_compare=true. I’m running windows 10


Info on how to configure uncrustify is here. You can see an example of a config file here. The easiest way would be to place a file named uncrustify or .uncrustify in your user folder (Windows Explorer doesn’t like creating files with names that start with ., but Atom will do it for you with no complaint) and adding sp_compare=true.

I was going to implement a quick and dirty editor.scan() engine that looked for an expression like /+|-|<|>|(<|>|\!|=|+|-)?=/ and dropped spaces around everything that matched. This way is better.


(1) Install atom-beautify
(2) Use uncrustify beautifier; to install on Widows follow this
(3) Then, go to ../uncrustify/cfg/defaults.cfg
(4) This is the configuration file that dictates the rules for beautifying the code
(5) Locate the lines where it deals with adding/removing spaces. There are many many options and simply replace ignore with add where ever you like.


I wouldn’t change the defaults.cfg file. If you ever change computers, you’re going to reimplement your changes, and if there’s any updating or uninstalling of uncrustify that happens, that file will get wiped out. Once you’ve gotten the settings changed to your preference, you should copy them and create a file in your user folder.


I agree :slight_smile: