Home Download Buy Blog Forum Support

How to change the behaviour of ctrl+delete?

How to change the behaviour of ctrl+delete?

Postby gizban on Sun Mar 17, 2013 8:44 pm

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: Select all
Steps: ("|" is the cursor)
The quick |brown fox.
press ctrl+delete

Expected Result:
The quick |fox.

Actual Result:
The quick | fox.

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)
gizban
 
Posts: 2
Joined: Sun Mar 17, 2013 8:32 pm

Re: How to change the behaviour of ctrl+delete?

Postby robertcollier4 on Sun Mar 17, 2013 9:24 pm

Try this:
Code: Select all
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")

In Key Bindings - User:
Code: Select all
{ "keys": ["ctrl+delete"], "command": "delete_word_whitespace" },
robertcollier4
 
Posts: 154
Joined: Sun Feb 24, 2013 5:37 pm

Re: How to change the behaviour of ctrl+delete?

Postby gizban on Sun Mar 17, 2013 9:47 pm

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.
gizban
 
Posts: 2
Joined: Sun Mar 17, 2013 8:32 pm

Re: How to change the behaviour of ctrl+delete?

Postby robertcollier4 on Sun Mar 17, 2013 10:50 pm

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: Select all
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))


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: http://docs.sublimetext.info/en/latest/
robertcollier4
 
Posts: 154
Joined: Sun Feb 24, 2013 5:37 pm


Return to General Discussion

Who is online

Users browsing this forum: Google [Bot], tux. and 24 guests