The trim_trailing_whitespace option changes the last substitute string at every file save
What I experienced
While editing a file, I started to do some substitutions with :s/o/x. I tested it on one line, saved the file, and tried to apply it to the whole file with g&.
What I expected: all "o" chars changing to "x" chars in the file. What happened: all the "o" chars of the file disapeared.
I had the editorconfig active, with the trim_trailing_whitespace active.
What I suppose
- when I saved, the editorconfig plugin applied the
trim_trailing_whitespace, using a%s/\s\+$//esubstitute - the subsitute string was changed to '' and not restored
- the search pattern was restored
- when I tried to apply
g&, it applieds/o//instead ofs/o/x
To reproduce the bug
In an empty directory, add this simple .editorconfig file:
[*]
trim_trailing_whitespace = true
Then:
- open a new file
- type some text, for example
hello world - make a subsitution, for example
:s/o/x, the text is nowhellx world - save the file with
:w(this also applies thetrim_trailing_whitespacerule) - repeat the last substitution with
&org&or:s - instead of
hellx wxrld, the text ishellx wrld: this is a symptom of the bug: the last substitute used is now '' instead of 'x'.
What I tried & explored
- Using
:@:(execute last command) seems to work as I expect, it replaces "o" chars by "x" chars in my example - Commenting the
trim_trailing_whitespacerule removes the bug - Commenting this line removes the bug: https://github.com/editorconfig/editorconfig-vim/blob/f305bc7/plugin/editorconfig.vim#L510
I saw in the code that the view is saved and restored by s:TrimTrailingWhitespace(), apparently this does not restore the substitute string.
According to this documentation, The last used search pattern and the redo command "." will not be changed by the function
- https://vimhelp.org/eval.txt.html#function%2dsearch%2dundo
So maybe this is a Vim limitation, unlike the search pattern, the subsitute string is changed by the function and not restored?
This is different (search pattern vs substitution is changed), but might be related to: #94
Thanks very much for reporting!
I can reproduce if the "open a new file" step in your original post is vim bar.txt. However, if I just say vim and then :w foo.txt, the issue does not appear:
.
That might be yet another issue :) .
I agree this behaviour should be changed if possible, to avoid surprising the user.