脚本查找&Sublime Text 中的替换操作 [英] Scripting find & replace operations in Sublime Text

查看:39
本文介绍了脚本查找&Sublime Text 中的替换操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Often I find myself doing repetitive file & replace operations in a file. Most often that comes down to fixed find and replace operations; deleting some lines, changing some strings that are always the same and so on.

在 Vim 中,这很简单,

In Vim that is a no-brainer,

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 编辑器中定义这样的函数,如果可以的话,然后调用它来处理当前打开的文件?

How could a function like this be defined in Sublime Text editor, if it can be done at all, and then called to act upon the currently opened file?

推荐答案

以下是编写 Sublime Text 2 插件的资源:

Here are resources to write Sublime Text 2 plugins:

  1. Sublime Text 2 API 参考
  2. Sublime Text 2 插件示例
  3. 如何运行 Sublime Text 2 命令
  4. 设置 Sublime Text 2 自定义键盘快捷键

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

Example: you can write a similar plugin and bind a hot key to it, that is, batch_edit command. Then you can open a file and execute the command via that hot key. By the way, in this script, I didn't consider the file encoding. You can get the file encoding via 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)

这篇关于脚本查找&Sublime Text 中的替换操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆