【问题标题】:Deleting until whitespace in Sublime Text在 Sublime Text 中删除直到空白
【发布时间】:2021-03-27 19:05:02
【问题描述】:

有没有办法删除所有字符,直到 Sublime 中的第一个空格。我知道您可以使用ctrl+delete 来执行此操作,但它会在非单词字符处停止(“、:、&、* 等)。当您尝试删除aaa aaa 2+a 时,它会从最后删除@987654323 @ 直到 + 符号,但它会删除 aaa 直到空格。我需要更改它,所以它将删除 2+a 直到第一个空格。解决方案可以是任何东西;更改设置,插件。

【问题讨论】:

  • 你可以写一个正则表达式,搜索它,全选然后删除。
  • 我在打字时这样做,就像你使用 ctrl+backspace 时一样。只需要删除所有非空格字符,直到第一个空格/空格。

标签: sublimetext3 sublimetext sublime-text-plugin


【解决方案1】:

我找到了解决方案。这是通过这个插件:

https://packagecontrol.io/packages/KeyboardNavigation

关键是:

{ "keys": ["ctrl+backspace"], "command": "delete_to_beg_of_contig_boundary", "args": {"forward": false} }

它从右到左删除所有字符,直到第一个空格。

【讨论】:

    【解决方案2】:

    我已经编写了一个 Sublime Text 插件来根据您的需要删除文本。它几乎与 ST 的 delete_word 命令相同,但仅在空白/非空白处中断。

    当被调用时,插件会从光标删除文本到下一组或上一组字符,分组被定义为空白或非空白字符。因此,如果连续运行多次,它将交替删除光标前后的空白和非空白字符组。 run()方法的forwards参数(即命令的arg)控制删除方向。

    将插件保存在配置Packages 文件夹层次结构中的某处。例如

    .../sublime-text-3/Packages/User/DeleteToWhitespace.py
    

    将键绑定添加到您的用户.sublime-keymap 文件。例如

    // 
    // These key bindings override the ST 'delete_word' keys but use whatever keys you want.
    // You could use `super+delete` and `super+backspace` and keep ST's delete keys intact.
    // 
    { "keys": ["ctrl+delete"], "command": "delete_to_whitespace", "args": {"forwards": true} },
    { "keys": ["ctrl+backspace"], "command": "delete_to_whitespace", "args": {"forwards": false} },
    

    下面是DeleteToWhitespace.py 插件。它已上传至this GitHub Gist——此链接直接指向the raw source code

    #
    # Name:        Delete To Whitespace
    # Requires:    Plugin for Sublime Text v3
    # Command:     delete_to_whitespace
    # Args:        forwards: bool (delete backwards if false)
    # License:     MIT License
    #
    
    import sublime, sublime_plugin, re
    
    class DeleteToWhitespaceCommand(sublime_plugin.TextCommand):
        """
        A Sublime Text plugin that deletes text from the cursor to the next or
        previous group of characters, the grouping being defined as either
        whitespace or non-whitespace characters. Thus if run several times in
        succession it will alternate between deleting groups of whitespace and
        non-whitespace ahead or behind the cursor. The forwards parameter of the
        run() method (i.e. the command's arg) controls the deletion direction.
        """
    
        def run(self, edit, forwards=True):
            self.edit = edit
            self.forwards = forwards
    
            if forwards:
                self.delete_forwards()
            else:
                self.delete_backwards()
    
        def delete_forwards(self):
            whitespace_regex = "^\s+"
            non_whitespace_regex = "^\S+"
    
            for sel in self.view.sel():
    
                if sel.size() > 0:
                    self.view.erase(self.edit, sel)
                    continue
    
                # ∴ sel.a == sel.b == sel.begin() == sel.end()
                # view.full_line() includes the trailing newline (if any).
                cursor = sel.a
                line = self.view.full_line(cursor)
                cursor_to_eol = sublime.Region(cursor, line.end())
                cursor_to_eol_str = self.view.substr(cursor_to_eol)
    
                match = re.search(whitespace_regex, cursor_to_eol_str)
                if match:
                    self.erase_matching_characters(cursor, match)
                    continue
    
                match = re.search(non_whitespace_regex, cursor_to_eol_str)
                if match:
                    self.erase_matching_characters(cursor, match)
                    continue
    
        def delete_backwards(self):
            whitespace_regex = "\s+$"
            non_whitespace_regex = "\S+$"
    
            for sel in self.view.sel():
    
                if sel.size() > 0:
                    self.view.erase(self.edit, sel)
                    continue
    
                # ∴ sel.a == sel.b == sel.begin() == sel.end()
                # view.line() excludes the trailing newline (if any).
                cursor = sel.a
                line = self.view.line(cursor)
                cursor_to_bol = sublime.Region(cursor, line.begin())
                cursor_to_bol_str = self.view.substr(cursor_to_bol)
    
                # Delete the newline of the 'previous' line.
                if cursor_to_bol.size() == 0 and cursor > 0:
                    erase_region = sublime.Region(cursor, cursor - 1)
                    self.view.erase(self.edit, erase_region)
                    continue
    
                match = re.search(whitespace_regex, cursor_to_bol_str)
                if match:
                    self.erase_matching_characters(cursor, match)
                    continue
    
                match = re.search(non_whitespace_regex, cursor_to_bol_str)
                if match:
                    self.erase_matching_characters(cursor, match)
                    continue
    
        def erase_matching_characters(self, cursor, match):
            match_len = match.end() - match.start()
            if self.forwards:
                erase_region = sublime.Region(cursor, cursor + match_len)
            else:
                erase_region = sublime.Region(cursor, cursor - match_len)
            self.view.erase(self.edit, erase_region)
    

    【讨论】:

    • 我已经尝试过了,当我按下热键时没有任何反应。
    猜你喜欢
    • 2012-08-14
    • 2011-05-25
    • 2021-12-22
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2015-05-26
    • 2016-09-05
    • 2013-03-04
    相关资源
    最近更新 更多