You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS Code保存文件时如何移除末尾全部空白行?

解决VS Code保存时完全移除所有末尾空白行的问题

方案一:使用扩展实现(推荐)

  1. 安装Trim Trailing Spaces扩展
  2. 在用户设置(settings.json)中添加以下配置,覆盖内置的换行修剪逻辑:
    "trimTrailingSpaces.onSave": true,
    "trimTrailingSpaces.includeEmptyLines": true,
    "files.trimFinalNewlines": false
    
    此配置会让扩展在保存时移除所有末尾空白行,包括最后一个换行符,完全匹配你的需求。

方案二:无扩展自定义任务实现

如果不想安装扩展,可以通过自定义任务实现:

  1. 打开命令面板(Ctrl+Shift+P),执行Tasks: Open User Tasks
  2. 粘贴以下任务配置(根据系统调整命令):
    {
      "version": "2.0.0",
      "tasks": [
        {
          "label": "Trim All Final Blank Lines",
          "type": "shell",
          "problemMatcher": [],
          "runOptions": {
            "runOn": "folderOpen"
          },
          "dependsOn": ["Trim Final Empty Lines"]
        },
        {
          "label": "Trim Final Empty Lines",
          "type": "shell",
          // Linux/macOS 使用此命令
          "command": "sed -i '$d' '${file}'",
          // Windows 替换为以下PowerShell命令
          // "command": "powershell -Command \"(Get-Content '${file}') | Where-Object { $_ -ne '' } | Set-Content '${file}'\"",
          "problemMatcher": []
        }
      ]
    }
    
  3. 保存设置后,可通过命令面板执行Tasks: Run Task选择Trim All Final Blank Lines,或绑定该任务到保存快捷键实现自动触发。

内容的提问来源于stack exchange,提问作者Nozomi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 21:20:08