Skip to content

Commit

Permalink
add ultilities to delete word
Browse files Browse the repository at this point in the history
there is no standard key to delete word with ctrl+backspace
a workaround is to repeatedly apply backspace to delete word
  • Loading branch information
randy3k committed Jun 20, 2018
1 parent d015030 commit b628ad3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Default (Windows).sublime-keymap
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[
{"keys": ["ctrl+shift+c"], "command": "copy", "context": [{"key": "setting.console_view"}]},
{"keys": ["ctrl+shift+v"], "command": "console_paste", "context": [{"key": "setting.console_view"}]}
{"keys": ["ctrl+shift+v"], "command": "console_paste", "context": [{"key": "setting.console_view"}]},

{"keys": ["ctrl+backspace"], "command": "console_delete_word", "args": {"forward": false}, "context": [{"key": "setting.console_view"}]},
{"keys": ["ctrl+delete"], "command": "console_delete_word", "args": {"forward": true}, "context": [{"key": "setting.console_view"}]}
]
44 changes: 44 additions & 0 deletions console.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sublime_plugin

import os
import re
import sys
import time
import threading
Expand Down Expand Up @@ -626,6 +627,49 @@ def run(self, edit, bracketed=False):
console.send_key("bracketed_paste_mode_end")


class ConsoleDeleteWord(sublime_plugin.TextCommand):
"""
On Windows, ctrl+backspace and ctrl+delete are used to delete words
However, there is no standard key to delete word with ctrl+backspace
a workaround is to repeatedly apply backspace to delete word
"""

def run(self, edit, forward=False):
view = self.view
console = Console.from_id(view.id())
if not console:
return

if len(view.sel()) != 1 or not view.sel()[0].empty():
return

if forward:
pt = view.sel()[0].end()
line = view.line(pt)
text = view.substr(sublime.Region(pt, line.end()))
match = re.search(r"(?<=\w)\b", text)
if not match:
return
n = match.span()[0]
if n > 0:
delete_code = get_key_code("delete")
console.send_string(delete_code * n)

else:
pt = view.sel()[0].end()
line = view.line(pt)
text = view.substr(sublime.Region(line.begin(), pt))
matches = re.finditer(r"\b(?=\w)", text)
if not matches:
return
for match in matches:
pass
n = view.rowcol(pt)[1] - match.span()[0]
if n > 0:
delete_code = get_key_code("backspace")
console.send_string(delete_code * n)


class ConsoleEventHandler(sublime_plugin.ViewEventListener):

@classmethod
Expand Down

0 comments on commit b628ad3

Please sign in to comment.