每个文件的自定义文件类型 [英] Custom filetype per file

查看:50
本文介绍了每个文件的自定义文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有没有扩展名的文件 /home/foo/myfile.是否可以将语法设置添加到此文件中?在 vim 中,它是 :set syntax=javascript.我知道在 Sublime 中你可以设置默认语法颜色.
有类似的问题 根据文件名更改默认语法,但您可以设置特定的文件名.我需要在文件本身中设置它,因为我有很多没有扩展名的不同文件.

Let's say that i have file /home/foo/myfile without extension. Is there option to add syntax setting into this file? In vim it's :set syntax=javascript. I know that in Sublime you can set default syntax color.
There's similar question Changing default syntax based on filename but there you set specific filename. I need to set it in file itself, since i have a lot of different files without extension.

推荐答案

ApplySyntax 包应该可以做你想做的事.

The package ApplySyntax should be able to do what you want.

ApplySyntax 是 Sublime Text 2 和 3 的插件,它允许你检测和应用文件的语法,否则可能不会正确检测.例如,扩展名为 .rb 的文件是通常是 Ruby 文件,但是当它们出现在 Rails 项目中时,它们可以是 RSpec 规范文件、Cucumber 步骤文件、Ruby on Rails 文件(控制器、模型等),或者只是普通的 Ruby 文件.这实际上是当我开始研究这个问题时我试图解决的问题插件.

ApplySyntax is a plugin for Sublime Text 2 and 3 that allows you to detect and apply the syntax of files that might not otherwise be detected properly. For example, files with the .rb extension are usually Ruby files, but when they are found in a Rails project, they could be RSpec spec files, Cucumber step files, Ruby on Rails files (controllers, models, etc), or just plain Ruby files. This is actually the problem I was trying to solve when I started working on this plugin.

ApplySyntax.sublime-settings 文件中设置您的规则/文件名:

Set your rules/filenames in the ApplySyntax.sublime-settings file:

//"rules" 是您要针对的检查列表(数组)当前视图中的文件.规则是正则表达式或一个函数.如果使用正则表达式,您可以指定您是否希望它针对file_name"或第一个进行测试文件的行(想想 shebangs 和 xml 文件).如果规则是函数,您必须提供包含该文件的路径函数和要调用的函数的名称.当这个函数是调用时,file_name"将作为唯一参数传递给它.你可以自由地在你的函数中做任何你想做的事,只需返回对还是错.

// "rules" is a list (array) of checks that you want to make against the file in the current view. A rule is either a regular expression or a function. If using a regular expression, you can specify whether you want it tested against the "file_name" or the first line of the file (think shebangs and xml files). If the rule is a function, you must provide the path to the file containing the function and the name of the function to call. When this function is called, the "file_name" will be passed to it as the only argument. You are free to do whatever you want in your function, just return True or False.

这篇关于每个文件的自定义文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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