Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim trailing whitespace only on changed lines #13157

Open
thomthom opened this issue Oct 3, 2016 · 28 comments
Open

Trim trailing whitespace only on changed lines #13157

thomthom opened this issue Oct 3, 2016 · 28 comments
Labels
editor-commands Editor text manipulation commands feature-request Request for new features or functionality
Milestone

Comments

@thomthom
Copy link

thomthom commented Oct 3, 2016

Feature Request:

When working with existing old code base it's not ideal to have the whole file trimmed for trailing whitespace. However, it's still a nice feature to have active for changed lines. That way the codebase will be cleaned up as you work on it.

@Bill-Stewart
Copy link

Why does it matter if trailing whitespace is removed for lines you didn't change?

@Tyriar
Copy link
Member

Tyriar commented Oct 3, 2016

I think https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig does this if you want to try that out.

@Tyriar Tyriar added feature-request Request for new features or functionality editor labels Oct 3, 2016
@thomthom
Copy link
Author

thomthom commented Oct 3, 2016

Why does it matter if trailing whitespace is removed for lines you didn't change?

Because it leads to noisy diffs in code reviews. Also makes the change looks bigger when you inspect history.

I think https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig does this if you want to try that out.

Hmm... I see it mention trim_trailing_whitespace, but nothing about limiting it to changed lines only.

@Tyriar
Copy link
Member

Tyriar commented Oct 3, 2016

Ah ignore me actually, I misread the issue. I set it as a feature request, you should be able to build an extension to do this.

@thomthom
Copy link
Author

thomthom commented Oct 3, 2016

For reference on an editor that already have such feature: RubyMine (And the other JetBrain editors)

2016-10-03_20h18_08

Think trimming changed lines only is the default - which I think is a nice default option. That means new files will get trimmed - and existing files doesn't end up with changes all over.

@Tyriar
Copy link
Member

Tyriar commented Oct 3, 2016

Yeah that would definitely be handy, I normally just disable trimming all together on such code bases and just try to be careful.

@Tyriar
Copy link
Member

Tyriar commented Oct 3, 2016

A set of string settings similar to the editor.renderWhitespace setting would be good:

"files.trimTrailingWhitespace": "all" | "modified" | "off"

@thomthom
Copy link
Author

thomthom commented Oct 3, 2016

Yeah that would definitely be handy, I normally just disable trimming all together on such code bases and just try to be careful.

Yea, that's what I end up doing as well. But then also miss the opportunity to clean up when for instance a piece of code is copy/pasted etc.

The main reason I post this request is that the default in VS Code is now to trim - which means you either need to set it to off on all installations of VSC that you do. Or remember to set it every time you set up VSC for an existing project.

In searching for this I found a number of threads where the debate seemed surprisingly strong on both sides for on/off. Each side has its merits. But I think a default of trim only changed lines is a nice compromise. It answers to both sides of the debate - pragmatic approach.

@TheColorRed
Copy link

Because it leads to noisy diffs in code reviews. Also makes the change looks bigger when you inspect history.

You can ignore all whitespace:

git diff other-branch -w

@thomthom
Copy link
Author

thomthom commented Oct 5, 2016

That doesn't apply to code review tools though. Most probably will have an option to adjust that - but you don't always have control over that. (Organisation policy etc.) Further more, ignoring all white-space isn't ideal either, as you want to catch space/tab changes etc.

@bmaupin
Copy link

bmaupin commented Apr 12, 2018

Based on #17883 I'd be surprised if this gets implemented.

However, the Trailing Spaces extension does the job quite well with this setting:

"trailing-spaces.deleteModifiedLinesOnly": true,

It can also trim on demand or automatically on save.

@pewscorner
Copy link

@bmaupin, the Trailing Spaces extension looks promising, but doesn't seem to work when enabling the trimOnSave and deleteModifiedLinesOnly settings. I've reported this problem, but for now it seems that we still don't have any way to get this functionality in VS Code.

@rjmunro
Copy link

rjmunro commented Feb 1, 2019

I tend to work with "remove trailing space" enabled, then, after git add use this trick to remove whitespace only lines changes.

@kAlvaro
Copy link

kAlvaro commented Jun 5, 2020

@bmaupin, the Trailing Spaces extension looks promising, but doesn't seem to work when enabling the trimOnSave and deleteModifiedLinesOnly settings. I've reported this problem

Here's the ticket.

@dibalavs
Copy link

Hi.
I see VScode 1.49 has option format modified lines on save.
But what about trailing spaces?

When will we see option to remove trailing spaces on save but only for modified lines?

@Nowaker
Copy link

Nowaker commented Nov 5, 2020

While we're at it, please upvote this as well: editorconfig/editorconfig#366

There's so many editors and tools that totally ignore this feature, and think it's okay to modify thousands of lines of existing code, just because a single line of code is changed.

@jameswilson
Copy link

I'm new to VS Code, and when I go to the Trailing Spaces extension settings page in the UI, it was empty... the extension page talks about configuration via JSON which I don't know where or how to do.

So ultimately, I got what I needed (highlighting of trailing whitespaces + auto-trim trailing white space of modified lines only upon save) with Trailing Whitespace extension, which does have easy-to-use and understand GUI Settings:

Screen Shot 2020-12-17 at 3 31 22 PM

@leonid-s-usov
Copy link

I'm new to VS Code, and when I go to the Trailing Spaces extension settings page in the UI, it was empty... the extension page talks about configuration via JSON which I don't know where or how to do.

So ultimately, I got what I needed (highlighting of trailing whitespaces + auto-trim trailing white space of modified lines only upon save) with Trailing Whitespace extension, which does have easy-to-use and understand GUI Settings:

Screen Shot 2020-12-17 at 3 31 22 PM

I had the exact opposite issue. Having read your post I immediately tried installing the TWS extension and couldn't find its settings. Removed it, installed Trailing Spaces and it showed up in the settings right after installation.

strange.

@kAlvaro
Copy link

kAlvaro commented Mar 6, 2021

You may also consider (both for an implementation idea or for your own use) the AutoTrim by chrisant996 extension. It works in a different and simpler way than similar addons: it tracks the line where cursor is, it detects if you've modified it and it removes trailing whitespace when you move away from it.

@shanebishop
Copy link

Another point that I believe hasn't been mentioned yet - automatically trimming trailing whitespace on all lines can make git-blame less useful, because then git-blame can report the last person to touch a line of code when in fact it was only that person's editor that touched the line to trim the whitespace.

@dibalavs
Copy link

dibalavs commented Oct 8, 2021

5 days ago this ticket celebrated 5 years anniversary.
Many people vote for this ticket. And still no any response from VSCode team.

@alexdima alexdima added editor-commands Editor text manipulation commands and removed editor labels Oct 15, 2021
@Nowaker
Copy link

Nowaker commented Oct 23, 2021

@Tom-Bonnike How does it have anything to do with this feature request?

@johnhubbard
Copy link

Hi there, I just got here after about 20 years in $OTHER_EDITOR, and want to mention that this issue is one of the very few things that make me want to open up the old editor. There are a number of projects that have trailing whitespace that I may not always fix up (at least, not in the same patch/commit), when fixing a bug or adding a tiny feature.

Running with "trim whitespace only on changed lines" has made my life very comfortable for a long time, but right now, it's gone. Please please add this feature!

@108anup
Copy link

108anup commented Jul 13, 2022

https://marketplace.visualstudio.com/items?itemName=shardulm94.trailing-spaces

Use:

{
    "trailing-spaces.deleteModifiedLinesOnly": true,
    "trailing-spaces.trimOnSave": true
}

From docs, "[Trim on save] abides by the other settings." With above config, I believe only modified lines (since last save) are trimmed on every save.

@johnhubbard
Copy link

Yes, thank you. That works (in fact, I eventually found that extension and have those same settings right now, so I feel validated, haha). I think I'm just slow to catch on to the idea of "basic VS Code core behavior, and anything you want can be changed via extensions". That works.

@PhilippeFerreiraDeSousa

The Trailing spaces extension is a live-saver! I would highly recommend Microsoft to add this feature in built-in settings.
This is a hard requirement to use any IDE along with git and GitHub.

@budimanfajarf
Copy link

budimanfajarf commented Apr 17, 2023

https://marketplace.visualstudio.com/items?itemName=shardulm94.trailing-spaces

Use:

{
    "trailing-spaces.deleteModifiedLinesOnly": true,
    "trailing-spaces.trimOnSave": true
}

From docs, "[Trim on save] abides by the other settings." With above config, I believe only modified lines (since last save) are trimmed on every save.

FYI for you guys who haven't got the expected result, before using the extension config, try to disable config Files: Trim Trailing Whitespace (built-in vscode config)

@lombervid
Copy link

Any update on this as a built-in setting?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
editor-commands Editor text manipulation commands feature-request Request for new features or functionality
Projects
None yet
Development

No branches or pull requests