本文介绍了如何配置 sublime 以在保存时始终转换为 unix 行结尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我曾经在 Sublime Text 中保存的所有文件都采用 Unix 行尾格式,即使我打开最初以不同格式保存但后来在 Sublime 中编辑的文件?仅仅设置 "default_line_ending": "unix" 是不够的,因为这不会像我提到的那样转换 Windows 文件.我该怎么做?

I want all files that I ever save in Sublime Text to be in Unix line ending format, even when I open files that were originally saved in a different format that I later edited in Sublime? Simply setting "default_line_ending": "unix" is not enough, because that doesn't convert Windows files as I mentioned. How do I do that?

推荐答案

这里有一个快速插件来完成这项工作:

Here's a quick plugin to do the job:

import sublime_plugin

class SetUnixLineEndingsCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.set_line_endings("unix")


class SetLineEndings(sublime_plugin.EventListener):
    def on_pre_save(self, view):
        view.run_command("set_unix_line_endings")

在 Sublime 中,选择 Tools → Developer → New Plugin….在打开的窗口中,删除那里的所有内容并将其替换为上面的程序.点击保存,保存文件对话框应在您的 Packages/User 目录中打开,其位置因操作系统和安装类型而异:

In Sublime, select Tools → Developer → New Plugin…. In the window that opens, delete everything that's there and replace it with the program above. Hit Save, and the save file dialog should open in your Packages/User directory, whose location varies by OS and type of install:

  • Linux:~/.config/sublime-text-3/Packages
  • OS X:~/Library/Application Support/Sublime Text 3/Packages
  • Windows 常规安装:C:\Users\YourUserName\AppData\Roaming\Sublime Text 3\Packages
  • Windows 便携式安装:InstallationFolder\Sublime Text 3\Data\Packages

将文件另存为 set_unix_line_endings.py 它将立即激活.

Save the file as set_unix_line_endings.py and it will activate immediately.

如果您编辑内容然后保存,插件只会更改文件的行尾.只是打开一个文件来查看不会改变任何东西.

The plugin will only change the line endings of a file if you edit the contents and then save it. Just opening a file to view won't change anything.

如果您不再希望插件处于活动状态,只需进入您的 Packages/User 目录并删除该文件或将其后缀更改为 .py 以外的其他内容 - set_unix_line_endings.py.bak 对我来说效果很好.

If you no longer want the plugin active, just enter your Packages/User directory and either delete the file or change its suffix to something other than .py - set_unix_line_endings.py.bak works well for me.

这篇关于如何配置 sublime 以在保存时始终转换为 unix 行结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 10:50