sublime text multiple cursor shortcut

落爺英雄遲暮 提交于 2019-12-23 11:33:13

问题


I'm a huge user of emacs and I absolutly love the fact that you can do EVERYTHING without using the mouse. I thing that feature make emacs really efficient.

I'm also a big fan of Sublime Text on Linux. I like the multiple cursor feature that you enable with Ctrl+left_mouse_clik. I also found that you can click Shift+alt+arrow_up_or_down which create a new cursor on the above or below line. So I was wondering if there was a way in sublime text to create multiple cursor wherever you want without using the mouse.


回答1:


One possible solution is to use bookmarks (if you aren't already). I don't know the Linux key bindings off the top of my head, but you can add bookmarks, then select all. To view the bindings for your platform, go to Goto -> Bookmarks, they should be listed by the command. You can also take a look at the default key binding file.

A second solution is to use a plugin. I wrote the following a while ago. Can't really say if/how well it works, as I can't remember. A quick test with it leads me to believe it works okay.

import sublime
import sublime_plugin


class MultiCursorCommand(sublime_plugin.TextCommand):
    def run(self, edit, action="add"):
        self.key = "multi_cursor"
        cursors = self.view.sel()
        saved_cursors = self.view.get_regions(self.key)
        if action == "add":
            self.view.add_regions(self.key, list(cursors) + saved_cursors, "keyword", "", sublime.DRAW_EMPTY)
        elif action == "show":
            cursors.add_all(saved_cursors)
            self.view.add_regions(self.key, [])
        elif action == "show_begin":
            saved_cursors += list(cursors)
            cursors.clear()
            cursors.add_all([c.begin() for c in saved_cursors])
            self.view.add_regions(self.key, [])
        elif action == "show_end":
            saved_cursors += list(cursors)
            cursors.clear()
            cursors.add_all([c.end() for c in saved_cursors])
            self.view.add_regions(self.key, [])
        elif action == "show_visible":
            pass
        elif action == "clear":
            self.view.add_regions(self.key, [])
        elif action == "remove":
            for cursor in cursors:
                if cursor in saved_cursors:
                    saved_cursors.remove(cursor)
            self.view.add_regions(self.key, saved_cursors, "keyword", "", sublime.DRAW_EMPTY)


class RemoveCursorCommand(sublime_plugin.TextCommand):
    def is_enabled(self):
        return len(self.view.sel()) > 1

    def run(self, edit, forward=True):
        self.view.sel().subtract(self.view.sel()[0 if forward else -1])

Key bindings will look something like

{ "keys": ["alt+a"], "command": "multi_cursor", "args": {"action": "add"} },
{ "keys": ["alt+s"], "command": "multi_cursor", "args": {"action": "show"} }

There are probably plugins that people have written that are on package control that do the same thing, I'm just not aware of them.




回答2:


I think PowerCursors is what you're looking for

https://packagecontrol.io/packages/PowerCursors



来源:https://stackoverflow.com/questions/25827388/sublime-text-multiple-cursor-shortcut

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!