Sublime Forum

Mirroring Whitespace?

#1

Hello guys,

since I am sure to find some fellow nerdy power users around here, I hope to find help to realize a nice feature!
I often find myself in a position where I want whitespace right AND left to my caret.

[SHIFT+SPACE] is not in use!
So I bound a snippet to that shortcut doing exactly that:
" $1 "–

Now comes the real nerdy part. If I made a mistake and want to delete both spaces I instinctively pressed [SHIFT+BACKSPACE], of course without the expected result. I am just a webdeveloper and not so much a hardcore programmer, meaning lacking the skills of implementing this idea.

Do you guys have any idea how to solve that?

Thanks :slight_smile:
Louis

1 Like

#2

I have a silly question, but if, at the point you want to undo, all you have is the cursor surrounded by a space on each side, could you not just create a keybinding for shift+bkspace to the undo command?

alternatively, I guess you would want to bind to two commands - left_delete and right_delete, which currently isn’t possible without a plugin

0 Likes

#3

It is possible. You just need to use a macro for this task.

I would prepare a keybinding with the proper context keys but I’m on mobile atm~

1 Like

#4

Save This @:

/Packages/___Your_Macro_Folder___/Delete Surrounding Spaces.sublime-macro

 

[
	{ "command": "left_delete"  },
	{ "command": "right_delete" },
]

 



Add This @:

Preferences > Key Bindings - User

 

// delete surrounding spaces if they exist
{
	"keys":  [ "shift+backspace" ],
	"command": "run_macro_file",
	"args":	{ "file": "res://Packages/___Your_Macro_Folder___/Delete Surrounding Spaces.sublime-macro" },
	"context": [
		{ "key": "preceding_text", "operator": "regex_match", "operand": ".*\\S " },
		{ "key": "following_text", "operator": "regex_match", "operand": " \\S.*" },
] },

// otherwise do nothing
  // ( this is optional. I personally sometimes hold shift while I'm typing & deleting,
  //   so I prefer `shift+backspace` to function the same as `backspace`. )
{
    "keys": [ "shift+backspace" ],
    "command": "_____NULL_____",
    "context": [ { "key": "preceding_text", "operator": "not_regex_match", "operand": ".*\\S ", "match_all": true } ]
},
{
    "keys": [ "shift+backspace" ],
    "command": "_____NULL_____",
    "context": [ { "key": "following_text", "operator": "not_regex_match", "operand": " \\S.*", "match_all": true } ]
},

 



Edit:

 
I originally recommended a Chain Of Command implementation, but just realized that it uses multiple edit objects - which doesn’t allow you to undo the entire chain with a single undo command.

3 Likes