sublimetext3 - 是否可以在不移动光标的情况下复制崇高文本中的一行?

标签 sublimetext3

说我有这个代码

body {
     margin: 0;
     padding: 0;
}

.navbar {
     margin: 0;
     padding: 0;
     background: rgba(0,0,0,0.1);
}

div {

}

在 div 里面我想放一行,'background: rgba(0,0,0,0.1);'所以我可以从上面复制它。我想知道是否有一种方法可以复制上面的行,而不必将光标向上复制并返回。我知道我可以通过 ctrl-c 和 ctrl-v 快速剪切和粘贴,但我认为如果我能告诉 sublime 我想复制哪一行并将其插入当前光标所在的位置,它会快得多。

最佳答案

是的,这是可能的。虽然你必须为此制作一个插件。
我尝试这样做,所以我并不是说这是有史以来最简单的方法,但它确实有效。

这是代码片段:

import sublime_plugin

class PromptCopyLineCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        # prompt fo the line # to copy
        self.view.window().show_input_panel(
            "Enter the line you want to copy: ",
            '',
            self.on_done,  # on_done
            None,          # on_change
            None           # on_cancel
        )

    def on_done(self, numLine):
        if not numLine.isnumeric():
            # if input is not a number, prompt again
            self.view.run_command('prompt_copy_line')
            return
        else:
            numLine = int(numLine)

        # NOL is the number of line in the file
        NOL = self.view.rowcol(self.view.size())[0] + 1
        # if the line # is not valid
        # e.g. 0 or less, or more that the number of line in the file
        if numLine < 1 or numLine > NOL:
            # prompt again
            self.view.run_command('prompt_copy_line')
        else:
            # retrieve the content of numLine
            view = self.view
            point = view.text_point(numLine-1, 0)
            line = view.line(point)
            line = view.substr(line)
            # do the actual copy
            self.view.run_command("copy_line", {"string": line})

class CopyLineCommand(sublime_plugin.TextCommand):
    def run(self, edit, string):
        # retrieve current offset
        current_pos = self.view.sel()[0].begin()
        # retrieve current line number
        CL = self.view.rowcol(current_pos)[0]
        # retrieve offset of the BOL
        offset_BOL = self.view.text_point(CL, 0)
        self.view.insert(edit, offset_BOL, string+'\n')

只需将其保存在 Package/User/ 下的 python 文件中(例如 CopyLine.py )
您还可以为它定义一个快捷方式,如下所示:
{ "keys": ["ctrl+shift+c"], "command": "prompt_copy_line"}

如果您对此有任何疑问,请提问。

PS:演示
enter image description here

关于sublimetext3 - 是否可以在不移动光标的情况下复制崇高文本中的一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25330804/

相关文章:

sublimetext3 - Sublime text 3 如何隐藏鼠标悬停功能的定义?

用于将文件路径转换为包/命名空间的正则表达式

windows - 如何在 windows 的 sublime text 3 中安装 'Emilbus Mono' 字体?

vim - 如何在 VIM 中将文件夹添加到项目(如 Sublime Text)?

python - Sublime Text 3 Python3 构建无法正常工作?

sublimetext3 - Sublime Text 3 中不能插入反斜杠

sublimetext - 在 Sublime Text 3 中搜索项目中的所有文件

sublimetext3 - 如何更改 Sublime Text 3 中的光标样式

sublimetext3 - Sublime Text 3 的 LiveReload - 不工作