All answers given here were great, but none quite matched ctrl+delete behavior on Windows.
There the behavior is as follows:
- When you’re before whitespace, it should delete until the next non-whitespace character.
- When you’re in the middle of a word, if deletes the rest of that word.
- When you’re before a word, it deletes the whole word.
- When you’re before a symbol, it deletes just the symbol.
If there is a selection, then the selection is first deleted, and then the above behavior is applied.
The following plugin does this all:
import sublime, sublime_plugin
from sublime import Region
class CtrlDeleteCommand(sublime_plugin.TextCommand):
def run(self, edit):
for caret in self.view.sel():
caretEnd=caret.end()
caretSize=caret.size()
#first, delete selection
if caretSize>0:
self.view.erase(edit, caret)
caretEnd-=caretSize #caret doesn't change when erasing
if caretEnd==self.view.size(): continue
nextChar=self.view.substr(caretEnd)
pos = caretEnd + 1
#if before space, delete upcoming whitespace
if nextChar.isspace():
while self.view.substr(pos).isspace(): pos += 1
self.view.erase(edit, Region(caretEnd, pos))
#else delete next word
else:
#if before symbol, just delete symbol, else delete next word
if nextChar.isdigit() or nextChar.isalpha():
while True:
nextChar = self.view.substr(pos) #this is fine even if at end of file
if not nextChar.isdigit() and not nextChar.isalpha():
break;
pos+=1
self.view.erase(edit, Region(caretEnd, pos))