【问题标题】:Scripting find & replace operations in Sublime TextSublime Text 中的脚本查找和替换操作
【发布时间】:2013-11-14 07:27:22
【问题描述】:

我经常发现自己在一个文件中执行重复的文件和替换操作。大多数情况下,这归结为 fixed 查找和替换操作;删除一些行,更改一些始终相同的字符串等等。

在 Vim 中这很简单,

function! Modify_Strength_Files()
    execute':%s/?/-/'
    execute':%s/Ä/-/'
    "--------------------------------------------------------
    execute':%s/Ä/-/'
    execute':%s///g'
    "--------------------------------------------------------
    execute':g/Version\ of\ Light\ Ship/d'
    execute':g/Version\ of\ Data\ for\ Specific\ Regulations/d'
    "--------------------------------------------------------
    " execute':g/LOADING\ CONDITION/d'
    " execute':g/REGULATION:\ A\.562\ IMO\ Resolution/d'

    " This is to reduce multiple blank lines into one.
    execute ':%s/\s\+$//e'
    execute ':%s/\n\{3,}/\r\r/e'
    " ---------------------
endfunction

逐字复制。

如果可以在 Sublime Text 编辑器中定义这样的函数,然后调用以对当前打开的文件执行操作,那么如何在 Sublime Text 编辑器中定义?

【问题讨论】:

  • 你能试着录制一个宏吗?
  • @lhuang - 这有什么帮助?
  • @Idigas 我的错。宏似乎无法正确记录替换命令。也许你必须编写一个插件来做到这一点。
  • @lhuang - 好吧,我正在尝试写(一些东西)来做到这一点。由于 ST 没有与 Vim 类似的脚本功能,我假设我将不得不深入研究 python,在那里编写一个函数并以某种方式将其“连接”到当前打开的文件。关于如何做到这一点的帮助就是我问这个的原因。

标签: sublimetext2


【解决方案1】:

这里是编写 Sublime Text 2 插件的资源:

  1. Sublime Text 2 API Reference
  2. Sublime Text 2 Plugin Examples
  3. How to run Sublime Text 2 commands
  4. Setting up Sublime Text 2 Custom Keyboard Shortcuts

例子:你可以写一个类似的插件,给它绑定一个热键,即batch_edit命令。然后您可以打开一个文件并通过该热键执行命令。顺便说一句,在这个脚本中,我没有考虑文件编码。您可以通过self.view.encoding()获取文件编码。

# -*- coding: utf-8 -*-

import sublime, sublime_plugin

import re

class BatchEditCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self._edit = edit
        self._replace_all(r"\?", "-")
        self._replace_all(u"Ä", "-")
        self._delete_line_with(r"Version of Light Ship")
        self._delete_line_with(r"Version of Data for Specific Regulations")
        self._replace_all(r"(\n\s*\n)+", "\n\n")

    def _get_file_content(self):
        return self.view.substr(sublime.Region(0, self.view.size()))

    def _update_file(self, doc):
        self.view.replace(self._edit, sublime.Region(0, self.view.size()), doc)

    def _replace_all(self, regex, replacement):
        doc = self._get_file_content()
        p = re.compile(regex, re.UNICODE)
        doc = re.sub(p, replacement, doc)
        self._update_file(doc)

    def _delete_line_with(self, regex):
        doc = self._get_file_content()
        lines = doc.splitlines()
        result = []
        for line in lines:
            if re.search(regex, line, re.UNICODE):
                continue
            result.append(line)
        line_ending = {
            "Windows" : "\r\n",
            "Unix"    : "\n",
            "CR"      : "\r"
        }[self.view.line_endings()]
        doc = line_ending.join(result)
        self._update_file(doc)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-08-12
  • 2013-09-10
  • 2017-08-27
  • 2013-07-31
  • 2020-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多