内置变量
内置变量
仅支持在 cmd
、 working_dir和
shell_cmd
字段内使用
{
"cmd": ["echo", "$file"],
"shell_cmd": "tsc -p $project"
}
Variable | Description |
---|---|
$packages | Packages 目录 |
$platform | 返回系统类别: "windows" , "osx" or "linux" . |
$file | 当前激活视图的文件,绝对路径 |
$file_path | 当前激活视图的文件目录,绝对路径 |
$file_name | 当前激活文件,带后缀的名称 xxxx.txt => xxxx.txt |
$file_base_name | 当前激活文件,不后缀的名称 xxxx.txt => xxxx |
$file_extension | 当前激活文件,后缀名 xxxx.txt => txt |
$folder | The full path to the first folder open in the side bar. |
$project | The full path to the current project file. 实测无效 |
$project_path | The path to the folder containing the current project file. 实测无效 |
$project_name | The file name (sans folder path) of the current project file. 实测无效 |
$project_base_name | The file name, excluding the extension, of the current project file. 实测无效 |
$project_extension | The extension of the current project file. |
使用方式
${file_path} # 当前打开的文件目录
属性解释
属性 | 说明 | 示例 |
---|---|---|
cmd | ||
shell_cmd | ||
shell | ||
env | 注入环境变量,shell_cmd 可以通过 %var_name% 来使用 | |
file_regex | ||
encoding | ||
file_patterns | ||
selector | ||
variants | ||
target | 调用一个 sublime.WindowCommand 实例执行负责的操作 |
常用案例
- 基础案例
{
"cmd": ["python", "$file"],
"selector": "source.python",
"file_regex": "^\\s*File \"(...*?)\", line ([0-9]*)"
}
- target 的使用
# Package/User/xxxx_build.py
class XxxxxBuildCommand(sublime_plugin.WindowCommand):
def run(self, param1=False, param2=False, .../):
pass
{
"target": "xxxx_build",
"selector": "source.mylang",
"cancel": { "kill": true },
"variants": [
{ "name": "param1", "param1": true },
{ "name": "param2", "param2": true }
]
}
node
{
"cmd": ["node", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"encoding": "UTF8",
"file_patterns": ["*.js", "*.cjs", "*.mjs"],
"selector": "source.js",
"variants": [
{
"name": "node(外部调用)",
"encoding": "UTF8",
"shell_cmd": "start cmd /c \"node ${file} & pause \""
},
{
"name": "node --experimental-specifier-resolution=node",
"encoding": "UTF8",
"cmd": ["node", "--experimental-specifier-resolution=node", "$file"]
},
{
"name": "v16.11.1(内部调用)",
"cmd": ["D:\\CPS\\nodejs\\v16.11.1\\node.exe", "${file}"],
"encoding": "UTF8"
},
{
"name": "v16.11.1(外部调用)",
"shell_cmd": "start cmd /c \"D:\\CPS\\nodejs\\v16.11.1\\node.exe ${file} & pause \"",
"encoding": "UTF8"
},
{
"name": "v16.11.1(内部调用 ES6)",
"cmd": ["D:\\CPS\\nodejs\\v16.11.1\\node.exe", "--experimental-modules", "${file}"],
"encoding": "UTF8"
}
]
}
ts-node
{
"cmd": ["ts-node", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"encoding": "UTF8",
"selector": "source.ts"
}
{
"cmd":["ts-node", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"encoding": "UTF8",
"file_patterns": ["*.ts","*.tsx"],
"selector": "source.ts",
"variants": [
{
"name":"ts-node/ems with file",
"encoding": "UTF8",
"shell_cmd": "node --loader ts-node/esm $file",
},
{
"name":"ts-node/ems with file on cmd window",
"encoding": "UTF8",
"shell_cmd": "start cmd /c \"ts-node $file & pause \"",
},
{
"name":"build currt file",
"encoding": "UTF8",
"shell_cmd": "tsc $file",
},
{
"name":"watch currt file",
"encoding": "UTF8",
"shell_cmd": "start cmd /c \"echo watching file:$file & tsc $file -w\"",
},
{
"name":"watch currt project",
"encoding": "UTF8",
"shell_cmd": "start cmd /c \"echo watching file:$file & tsc $file -w\"",
},
{
"name":"run & build",
"encoding": "UTF8",
"shell_cmd": "ts-node $file & tsc $file",
}]
}