vscode: Bug (?): auto-trim of trailing whitespace on the cursor line

With the “Trim Trailing Whitespace” (files.trimTrailingWhitespace) option enabled, trailing whitespace is removed even on the cursor line (see below). I consider this to be a bug because in #24601 @alexdima indicated that the intended behavior is for trailing spaces to not be deleted in the cursor line. And also, current behavior is quite annoying and to my knowledge, other editors keep the line under cursor intact in similar situations.

Examples of where this behavior is really annoying:

  • I finished writing a portion of the code, hit enter to get to the next line to continue on to the next portion (at this moment, the cursor is at correct indentation), and decide to save at this point. Cursor is then flung back to the beginning of the line and I have to hit tab several times to get back to my indentation.
  • I start creating a variable and type int , then pause to think of a variable name, instinctively hit cmd+s, my space disappears.

output

Does this issue occur when all extensions are disabled?: Yes

  • VS Code Version: 1.79.1 (4cb974a7aed77a74c7813bdccd99ee0d04901215)
  • OS Version: macOS Ventura 13.4 (22F66)

Steps to Reproduce:

  1. Create/open any file
  2. Enter several trailing spaces in any line and leave the cursor where it is
  3. Hit Save: the trailing spaces will be removed

Expected behavior: the trailing spaces under the cursor remain and the “save” action does not move the cursor.

It would probably be the least invasive if a) this is an option (aka a second checkbox “Leave the cursor line as-is” b) it may be ok to delete trailing whitespace (i.e., keep the current behavior) if the cursor is on the line but not adjacent to the trailing whitespace. I would say that the rule of thumb here should be: save function should not move the cursor.

The main point is that saving should not move the cursor and it should not affect or remove things that were just typed.

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 25
  • Comments: 20 (1 by maintainers)

Most upvoted comments

This issue still occurs in the newer VSCode versions:

Version: 1.81.0
Commit: 6445d93c81ebe42c4cbd7a60712e0b17d9463e97
Date: 2023-08-02T12:38:28.722Z (1 wk ago)
Electron: 22.3.18
ElectronBuildId: 22689846
Chromium: 108.0.5359.215
Node.js: 16.17.1
V8: 10.8.168.25-electron.0
OS: Darwin arm64 22.6.0 (macOS Ventura v13.5 (22G74))