Sublime Forum

Spaces around maths and logic operators

#1

Having got Sublime installed, package control working, colo(u)r schemes installed (even for the jack language) and even some snippets going (with everyone’s help), I’ve run into the next challenge !

I would like to have a keybinding (?)(I don’t know whether this is the best solution) so that when I type a math(s) operator instead of say ‘=’ what is outputed to the screen is ’ = '.

In the following pseudo code:

*if (x==((5+(y-4))z))

would become

if ( x == (((5 + (y - 4)) * z) )

(Brackets I’m not fussy about)

So basically to save me typing spaces all the time. I’ve tried using a snippet but ‘=’ doesn’t seem to be recognised as a valid tabTrigger.
Also I want this to happen for all languages too.
I’ve looked at this question

I’ve had a look at this

https://www.sublimetext.com/docs/3/key_bindings.html

And I was hoping someone might have answered this post

If anyone could give me the idiots guide that’d be much appreciated
Regards, Lozminda

0 Likes

#2

To do this via a key binding, you could do something like the following:

{ "keys": ["="], "command": "insert", "args": {
    "characters": " = "
}},

Now whenever you press =, the binding triggers and inserts a leading and trailing space.

Note however that if you do this, you lose the ability to type an = character without having spaces around it; if you wanted to type == or <= for example you have to go through more steps (or make additional key bindings) that trigger in those circumstances:

{ "keys": ["=", "="], "command": "insert", "args": {
    "characters": " == "
}},

{ "keys": ["<", "="], "command": "insert", "args": {
    "characters": " <= "
}},

To some degree you can mitigate that with a context entry in the key binding to make it only trigger in specific cases, if the cases where you want it to happen are specific enough.

On the whole things like code style issues might be better addressed by using a code-tidy type tool to reformat your text for you on save (or manually) than by having a series of key bindings to do this, though.

3 Likes

#3

Can be an overkilling, but you may prefer a code formatter of the target language?

Or a simple plugin which prepend/append spaces basing on scope.

0 Likes

#4

Thanks to you both. In the spirit of many other computing forums I thought I’d have a go at a solution myself, whilst waiting for a reply.
Basically I recorded a macro and then attached that to a key binding as shown by this youtube tutorial (I think it’s for S2 tho, but it worked)

(It’s a good tutorial.)

However I’ve got to record a macro for each symbol as I can’t work out how to do a general macro, or insert the character that i’ve pressed into the keybinding. (So that’s a macro for each operator, then a keybinding for each macro !)

Here’s an example of my keybinding in case this conversation is of any use to anyone else…
{ “keys”: ["="], “command”: “run_macro_file”, “args”: {“file”: “res://Packages/User/space_symbol_space.sublime-macro”} }

OdatNurd, I think your suggestion is much better than my solution, and I hadn’t even thought about >= etc !

Re code formatter, great idea, any suggestions ? Tho I think I’m gonna go with the keybindings first of all…

Sublime 3 is winning me over and what a great forum too !

:star_struck:

0 Likes

#5

ps Now I look into this more carefully, getting a context into my key-bindings is looking a tad tricky, I’m beginning to think a code formatter is much better, but then I’m gonna have to set that up too, I guess
Possibly the easiest solution is to forget about operator phrases (>=) (I don’t kno if that’s the right name) and just set up a macro that deletes a space and puts the cursor back. eg:

Typing “=”+"=" gives: = = {cursor position}, crtl+shift+Del (or something) which does a macro gives: == {new cursor position}.
The macro having sent the cursor back, deleted the two spaces in the middle and then returned the cursor to the right place.
Inelegant, but a lot less work !
If anyone’s got a better idea before i launch in, much appreciated.
Lozminda

0 Likes

#6

All else being equal, the binding I showed above is going to get you the same result as creating a macro but with less work, particularly if you’re going to have many such keys bound. A macro is useful if you also need to move the cursor around and take other actions, but for straight text insertion, insert or insert_snippet is an easier way to do the same thing that doesn’t require you to create macro files.

Neither method lets you access the key that was pressed in order to include it in the result though; for that you would need a plugin to create a custom command, which could determine what key was pressed and include it in the result. Such a plugin would be easy to create but it’s all-encompassing in a way that you definitely need to apply context to it because such a plugin would trigger for every character you insert otherwise.

I did videos on key bindings and key binding contexts recently which may provide some extra information than the one you linked above (though they just cover bindings in general and nothing macro related).

Code formatting tools are usually specific to the language or file type that they’re used for, since they require knowledge of the code and how it should look. Depending on the type of file that you’re editing, you can google for something like a “formatter” or “prettifier” for that particular language.

Once you find one (if there is one) then you can check Package Control to see if it has a package that can use that directly. If not, then you can always set it up manually as well. Sublime can execute any external program that you like via a build system or the exec command.

If it comes to that, we can provide more instruction; I also have a video series on build systems as well, which may provide some insight into how that aspect works.

0 Likes

#7

Thank you, I’ll check out your vids later, time to do the washing up and ting.

ATB

0 Likes

#8

Great vids OdatNerd.

I did of course run into all the problems that everyone predicated I would, having used sublime for a few tens of hours.

So having spaces round your mathematical symbols makes the code easier to read (IMHO) but sometimes you don’t want spaces around symbols; for example comments, for double symbols (the C family use == ++ etc) so not only do i have a macro for space removal but I hit on an easy fix:
instead of

{ “keys”: ["="], “command”: “insert”, “args”: {“characters”: " = "}},

i’ve implemented

{ “keys”: [“ctrl+alt+=”], “command”: “insert”, “args”: {“characters”: " = "}},

hence pressing ctrl+alt+= gives me an equals with spaces either side, a quick hack !

However it doesn’t seem possible to use this key '| ’
‘|’ is “logical or” in the C family. Any thoughts on how to use this key in a keybinding. I get

Error trying to parse file: Unexpected character, expected a comma or closing bracket in Packages/User/Default (Linux).sublime-keymap:9:35

when i try to use this ’ \ ’ key. Happy new year to y’all !:keyboard::computer_mouse::star_struck:

0 Likes

#9

A sublime-keymap file is a JSON file, and in JSON the \ character is special and is an escape character; that is, it’s used to indicate to the JSON parser that the character that comes after it should be treated as a regular character instead of as a special character.

In order to use it in a key binding, you thus need to escape it by using it twice in a row, \\; that tells the JSON parser that it should be treated as a standard \ character.

If you’re not already using it, I would highly recommend the PackageDev package; despite it’s name, it’s not only for package developers (or if you will, since customizations to Sublime are done in the User package at some level everyone is a package developer).

One of the many features the package adds is enhanced syntax highlighting for Sublime resource files; it shows you visually where files are broken to make issues like this clearer:

In the first binding, the \ character is escaped (which is visually distinct as well) and the binding works; in the second case the entire file is broken because the sequence \" means “literal double quote”, which breaks the file.

0 Likes

#10

Wow thanks for that speedy reply. Awesome. Unfortunately i think i had some trouble with package dev, but that’s for another post. Thanks your help.
Hippy new year !

0 Likes

#11

PackageDev for User Defined colour scheme

My last post is that i was having trouble installing Package Dev

last post.

I guess that should be a separate post, but at the mo am subliming freely, so it’s no bother ATM

Ps Have a sublime new year !

0 Likes