Sublime Forum

How to change the behaviour of ctrl+delete?

#1

I’ve changed ctrl+right to be more consistent with other text editors.
{ “keys”: “ctrl+right”], “command”: “move”, “args”: {“by”: “words”, “forward”: true} },

I want ctrl+delete to behave like it does in notepad2, notepad++ and visual studio. It is easier to explain with an example.

Example:

[code]
Steps: ("|" is the cursor)
The quick |brown fox.
press ctrl+delete

Expected Result:
The quick |fox.

Actual Result:
The quick | fox.[/code]
Sublime is a great editor but this one annoyance is keeping me from using it as my main editor. (After reading Tip 22 from the Pragmatic Programmer, I’m looking for a text editor to become my main editor.) (Tip 22: Use a Single Editor Well)

(I’m using Windows 7 if that make a difference)

0 Likes

#2

Try this:

[code]import sublime, sublime_plugin, string

class DeleteWordWhitespaceCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.run_command(“delete_word”, {“forward”: True})
for thisregion in self.view.sel():
if(self.view.substr(thisregion.begin()) in string.whitespace):
nonWhitespacePos = thisregion.begin()
while((self.view.substr(nonWhitespacePos) in string.whitespace) and (nonWhitespacePos < self.view.line(thisregion.begin()).end())):
nonWhitespacePos += 1
self.view.sel().add(sublime.Region(thisregion.begin(), nonWhitespacePos))
self.view.run_command(“right_delete”)[/code]
In Key Bindings - User:

{ "keys": "ctrl+delete"], "command": "delete_word_whitespace" },
0 Likes

#3

Thank you!

It took me a few minutes to find that I had to add the class using “Tools > New Plugin”.

I’m excited to see that it is fairly easy to add plugins and change the behavior of keys. I’ll have to read through the rest of the features to see what else I’m missing.

0 Likes

#4

Actually here’s a little more simpler one using a more direct view.erase() instead of view.run_command(“right_delete”). I agree the default Ctrl+Delete behavior should be like this.

[code]import sublime, sublime_plugin, string

class DeleteWordWhitespaceCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.run_command(“delete_word”, {“forward”: True})
for thisregion in self.view.sel():
if(self.view.substr(thisregion.begin()) in string.whitespace):
nonWhitespacePos = thisregion.begin()
while((self.view.substr(nonWhitespacePos) in string.whitespace) and (nonWhitespacePos < self.view.line(thisregion.begin()).end())):
nonWhitespacePos += 1
self.view.erase(edit, sublime.Region(thisregion.begin(), nonWhitespacePos))[/code]

The plugin functionality is my favorite thing about Sublime. It lets you make the editor perfectly how you want it.
The official docs are a bit lacking. These unofficial docs are better: docs.sublimetext.info/en/latest/

0 Likes