Sublime Text 和 Python 插件:如何在 sublime 文本编辑器中将 python 代码与 for 循环合并

Sublime Text and Python plugin: How to incorporate a python code with a for loop in sublime text editor

我对使用 python 脚本作为 Sublime Text 中的插件比较陌生(注意:我不是开发人员)。但是,我所处的情况是,我总是以相同的特定事件结束,我需要通过脚本自动执行对字符串的某些操作。

例如,假设我在文本文件中有以下值:

@article{jentsch2010multiple,
      title={The multiple hybrid bootstrap—Resampling multivariate linear processes},
      author={Jentsch, Carsten and Kreiss, Jens-Peter},
      journal={Journal of Multivariate Analysis},
      volume={101},
      number={10},
      pages={2320--2345},
      year={2010},
      publisher={Elsevier}
    }

    @article{buhlmann1997sieve,
      title={Sieve bootstrap for time series},
      author={B{\"u}hlmann, Peter},
      journal={Bernoulli},
      volume={3},
      number={2},
      pages={123--148},
      year={1997},
      publisher={Bernoulli Society for Mathematical Statistics and Probability}
    }


@article{kreiss2011bootstrap,
  title={Bootstrap methods for dependent data: A review},
  author={Kreiss, Jens-Peter and Paparoditis, Efstathios},
  journal={Journal of the Korean Statistical Society},
  volume={40},
  number={4},
  pages={357--378},
  year={2011},
  publisher={Elsevier}
}

我想要的是让每一个都排成一行,即

@article{jentsch2010multiple,   title={The multiple hybrid bootstrap—Resampling multivariate linear processes},   author={Jentsch, Carsten and Kreiss, Jens-Peter},   journal={Journal of Multivariate Analysis},   volume={101},   number={10},   pages={2320--2345},   year={2010},   publisher={Elsevier} }
@article{buhlmann1997sieve,   title={Sieve bootstrap for time series},   author={B{"u}hlmann, Peter},   journal={Bernoulli},   volume={3},   number={2},   pages={123--148},   year={1997},   publisher={Bernoulli Society for Mathematical Statistics and Probability} }
@article{kreiss2011bootstrap,   title={Bootstrap methods for dependent data: A review},   author={Kreiss, Jens-Peter and Paparoditis, Efstathios},   journal={Journal of the Korean Statistical Society},   volume={40},   number={4},   pages={357--378},   year={2011},   publisher={Elsevier} }

目前,我所做的是将所有内容复制到文本编辑器(即 Sublime Text 3),然后将其作为字符串值粘贴到 Python 中以执行代码。

这是我的:

A='''
@article{jentsch2010multiple,
  title={The multiple hybrid bootstrap—Resampling multivariate linear processes},
  author={Jentsch, Carsten and Kreiss, Jens-Peter},
  journal={Journal of Multivariate Analysis},
  volume={101},
  number={10},
  pages={2320--2345},
  year={2010},
  publisher={Elsevier}
}

@article{buhlmann1997sieve,
  title={Sieve bootstrap for time series},
  author={B{\"u}hlmann, Peter},
  journal={Bernoulli},
  volume={3},
  number={2},
  pages={123--148},
  year={1997},
  publisher={Bernoulli Society for Mathematical Statistics and Probability}
}

@article{kreiss2011bootstrap,
  title={Bootstrap methods for dependent data: A review},
  author={Kreiss, Jens-Peter and Paparoditis, Efstathios},
  journal={Journal of the Korean Statistical Society},
  volume={40},
  number={4},
  pages={357--378},
  year={2011},
  publisher={Elsevier}
}
'''

print(
    '\n'.join(
        [' '.join(para.splitlines()) for para in A.split('\n\n')]
    )
)

但是,我注意到可以在 Sublime Text 中完成所有这些操作,因此我不必复制并粘贴整个字符串然后执行脚本。

link 是 here。但是,link 明确表示关于该主题的文档很少。有人可以帮我弄这个吗?

到目前为止,我所知道的关于该主题的全部是以下过程:

1) Select 工具 |开发商 |菜单中的新插件。 这将打开以下内容:

 import sublime
    import sublime_plugin


    class ExampleCommand(sublime_plugin.TextCommand):
        def run(self, edit):
            self.view.insert(edit, 0, "Hello, World!")
  1. 如何调整此代码以添加我的代码?

非常感谢!

创建问题中提到的以下插件,即 Tools ---> Developer ---> New Plugin ... 并在最后用 .py 将其保存为您想要的任何名称。

插件:

import sublime, sublime_plugin, re, string   #import the required modules

class ConvertCommand(sublime_plugin.TextCommand): #create Text Command
    def run(self, edit):   #implement run method
        for region in self.view.sel():  #get user selection
            if not region.empty():  #if selection not empty then
                s = self.view.substr(region)  #assign s variable the selected region
                replace = '\n'.join([' '.join(para.splitlines()) for para in s.split('\n\n')])
                self.view.replace(edit, region, replace) #replace content in view


class UndoCommand(sublime_plugin.TextCommand): #create Text Command
    def run(self, edit):   #implement run method
        for region in self.view.sel():  #get user selection
            if not region.empty():  #if selection not empty then
                s = self.view.substr(region)  #assign s variable the selected region
                replace = '\n'.split([' '.join(para.splitlines()) for para in s.split('\n\n')])
                self.view.replace(edit, region, replace) #replace content in view

这创建了一个名为 ConvertUndo

的新 command

菜单视图

然后创建一个sublime-menu,它将创建一个名为convert的新命令,这被保存为id名称

备注:SUBLIME-MENU 文件用于控制 Sublime Text 中的主菜单和上下文菜单。

菜单:另存为context.sublime-menu

[
{ 
    "caption": "Convert", 
    "id": "convert", 
    "children":
    [
    {
    "caption": "Convert",
    "command": "convert"
    },
    {
    "caption": "Undo Convert",
    "command": "undo"
    }
    ]
}
]

快捷键

Preferences ---> Key Bindings

然后这样写

[
{"keys": ["alt+enter"], "command": "convert"},
]

这将允许您 select 并且只需按下键盘 alt+enter 即可完成任务。