Sublime Text - 如何创建键盘快捷方式以将文本从一个打开的文件移动到另一个打开的文件

时间:2016-05-16 07:25:58

标签: sublimetext3

使用Sublime Text,我在左边的列中有文本块,我想选择性地将一些行移到右边的单独文件中。

是否有现成的键盘快捷键可以执行此操作,如果不是,我将如何将其放在一起。步骤将是

1. Cut selection
2. Change to other column
3. Paste selection

1 个答案:

答案 0 :(得分:1)

我写了一个简单的插件,可以满足您的问题的要求,还有一些额外的功能:

  • 双向copy& cut个命令(从活动文档到非活动文档,适用于任一视图
  • 选择&滚动到插入的文本@非活动文档
  • 仅在有2个视图组时才执行

Demo

将以下代码复制到:
Packages/MoveTextToInactiveDocument/MoveTextToInactiveDocument.py

import sublime, sublime_plugin

LINE_BREAK = "\n"

class MoveTextToInactiveDocumentCommand( sublime_plugin.TextCommand ):
    def run( self, edit, MODE = "copy" ):

        view   = self.view
        window = view.window()

        #■■■  Verify Window Groups  ■■■#

        windowGroup_Count = window.num_groups()

        if windowGroup_Count != 2:
            return

        #■■■  Set Active & Inactive Documents  ■■■#

        activeDocument_Group = window.active_group()

        if activeDocument_Group == 0:
            activeDocument   = window.active_view_in_group( 0 )
            inactiveDocument = window.active_view_in_group( 1 )
        elif activeDocument_Group == 1:
            activeDocument   = window.active_view_in_group( 1 )
            inactiveDocument = window.active_view_in_group( 0 )

        #■■■  Verify Selection  ■■■#

        selections = activeDocument.sel()

        if len( selections ) == 0:
            return

        #■■■  Move Text From Active Document To Inactive Document  ■■■#

        first_InsertionPoint = inactiveDocument.size()

        for region in selections:

            regionText     = activeDocument.substr( region ) + LINE_BREAK
            insertionPoint = inactiveDocument.size()

            inactiveDocument.insert( edit, insertionPoint, regionText )

            if MODE.lower() == "cut":
                activeDocument.erase( edit, region )

        #■■■  Select & Scroll To Inserted Text  ■■■#

        inactiveDocument_End = inactiveDocument.size()
        insertedText_Region  = sublime.Region( first_InsertionPoint, inactiveDocument_End )

        inactiveDocument.selection.clear()
        inactiveDocument.selection.add( insertedText_Region )

        inactiveDocument.show( inactiveDocument.size() )
        window.focus_view( activeDocument )

sublime-keymap运行Preferences: Key Bindings - User,打开您的用户command palette文件。

添加以下key-bindings

{
    "keys": [ "ctrl+super+c" ],
    "command": "move_text_to_inactive_document",
    "args": { "MODE": "Copy" }
},

{
    "keys": [ "ctrl+super+x" ],
    "command": "move_text_to_inactive_document",
    "args": { "MODE": "Cut" }
},

您现在可以使用:

  • Ctrl + Super + C 将选择内容复制到非活动文档
  • Ctrl + Super + X 将选择内容切换为非活动文档