0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【VSCode】で保存時に行末の余分な空白を自動削除する

0
Posted at

VS Codeで保存時に行末の余分な空白を自動削除する

VS Codeには、ファイル保存時に行末の余分なスペースやタブを削除する機能が標準で用意されています。拡張機能のインストールは不要です。

設定画面から有効にする

  1. VS Codeの設定を開きます(Windows/Linux:Ctrl + ,、Mac:Cmd + ,)。
  2. 検索欄に Trim Trailing Whitespace と入力します。
  3. Files: Trim Trailing Whitespace にチェックを入れます。

これで、保存するたびに行末の余分な空白が削除されます。

settings.jsonで設定する

ファイル末尾の空行も整理したい場合は、settings.json に次の設定を追加します。すでに設定がある場合は、既存の {} 内に追記してください。

{
  "files.trimTrailingWhitespace": true,
  "files.trimFinalNewlines": true,
  "files.insertFinalNewline": true
}
設定 動作
files.trimTrailingWhitespace 行末の余分なスペースやタブを削除する
files.trimFinalNewlines ファイル末尾の余分な空行を削除する
files.insertFinalNewline ファイルの最後に改行がなければ追加する

行頭のインデントや、行の途中にある空白を一律に削除する設定ではありません。

Markdownでは注意

Markdownでは、行末の半角スペース2つが改行を表す場合があります。これを残したい場合は、Markdownに限って行末の空白削除を無効にできます。

{
  "files.trimTrailingWhitespace": true,
  "files.trimFinalNewlines": true,
  "files.insertFinalNewline": true,
  "[markdown]": {
    "files.trimTrailingWhitespace": false
  }
}

保存時に自動で整える設定にしておくと、不要な空白による差分を減らせます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?