VS File Action
一个用于自定义文件操作的 VS Code 扩展,支持通过右键菜单和状态栏快速执行预定义的操作。
配置文件首次启动时会从插件示例复制到用户家目录下的 .vs-file-action/actions.json,之后插件只读取这份用户配置。
使用方法
点击状态栏配置文件关联(可选)
支持多种操作类型
- VS Code 命令
- Shell 命令
- NPM 脚本
- 可扩展的自定义处理器
右键菜单集成
- 在文件上右键可快速执行预定义操作
- 在目录上右键可执行
dirActions
- 根据文件类型显示不同的操作选项
标题栏按钮
- 点击标题栏按钮,对当前打开的文件,快速执行预定义操作
状态栏
- 可打开用户配置文件
- 可执行
workspaceActions
配置说明
配置文件位于用户家目录下的 .vs-file-action/actions.json。首次启动时会从扩展目录中的示例文件复制;之后修改用户配置即可生效。
配置根对象支持以下三个操作组:
fileActions:按文件扩展名匹配,在文件右键菜单和标题栏按钮中显示。
dirActions:在目录右键菜单中显示。
workspaceActions:从状态栏菜单执行,操作上下文为当前工作区根目录。
每个操作包含 name、type 和 config 字段。type 可使用 vscode-command、shell-command 或 npm-script。
变量支持
${filePath}:当前文件完整路径
${fileDir}:当前文件所在目录
${fileName}:当前文件名
${extensionPath}:插件根目录
${dir}:当前目录上下文;目录操作中为右键目录,文件操作中为文件所在目录
${workspace}:当前工作区根目录
workspaceActions 是目录操作的特殊情况,使用当前工作区根目录作为目录上下文,因此应使用 ${dir} 或 ${workspace} 获取工作区目录。
示例:自定义命令
{
"fileActions": [
{
"fileType": ".csv",
"actions": [
{
"name": "使用Excel打开",
"type": "shell-command",
"config": {
"commands": [
"start excel \"${filePath}\""
]
}
}
]
},
{
"fileType": ".js",
"actions": [
{
"name": "格式化代码",
"type": "vscode-command",
"config": {
"command": "editor.action.formatDocument"
}
},
{
"name": "运行文件",
"type": "shell-command",
"config": {
"commands": [
"node ${filePath}"
]
}
},
{
"name": "使用 Prettier 格式化",
"type": "npm-script",
"config": {
"commands": [
"prettier --write ${filePath}"
]
}
}
]
},
{
"fileType": ".ts",
"actions": [
{
"name": "编译",
"type": "shell-command",
"config": {
"commands": [
"tsc ${filePath}"
]
}
},
{
"name": "运行 ts-node",
"type": "shell-command",
"config": {
"commands": [
"ts-node ${filePath}"
]
}
}
]
},
{
"fileType": ".md",
"actions": [
{
"name": "预览",
"type": "vscode-command",
"config": {
"command": "markdown.showPreview"
}
}
]
},
{
"fileType": ".png",
"actions": [
{
"name": "ocr",
"type": "shell-command",
"config": {
"commands": [
"cd \"${extensionPath}\\bin\"",
".venv\\Scripts\\activate",
"python \"${extensionPath}\\bin\\ocr_cn_cli.py\" -i \"${filePath}\" -o \"${fileDir}\\ocr_output.txt\""
]
}
}
]
}
],
"dirActions": [
{
"name": "打开目录终端",
"type": "shell-command",
"config": { "commands": ["Set-Location -LiteralPath \"${dir}\"", "pwsh"] }
}
],
"workspaceActions": [
{
"name": "打开工作区终端",
"type": "shell-command",
"config": { "commands": ["Set-Location -LiteralPath \"${workspace}\"", "pwsh"] }
}
]
}
系统要求
- Visual Studio Code ^1.98.2
安装
- 通过 VS Code 扩展市场安装
- 重启 VS Code
- 根据需要配置
actions.json
许可证
MIT
开始使用 VS File Action 提升您的开发效率!