neovim icon indicating copy to clipboard operation
neovim copied to clipboard

`editorconfig.trim_trailing_whitespace` breaks repeat last substitute on save

Open mateuszmandera opened this issue 1 year ago • 1 comments

Problem

trim_trailing_whitespace when active, since it works by using :s, changes the last substitute string on every BufWritePre:

https://github.com/neovim/neovim/blob/5aa1a9532cbac835ad027ebdf04311c7e8fb7007/runtime/lua/editorconfig.lua#L147-L170

This is consistent with the original plugin I believe (https://github.com/editorconfig/editorconfig-vim/issues/175), but nonetheless is pretty surprising behavior as it silently breaks things like & (repeat last substitute) or :s/newpattern/~. What adds to the confusion is that since it does preserve the original search history, then if you do:

:s/foo/replacement
:w
&

you're going to get substitution of foo with the empty string.

Steps to reproduce

# in a project with `.editorconfig` with trim_trailing_whitespace = true
nvim --clean .
:s/foo/replacement
:w
&

Expected behavior

In my view this would ideally preserve the original last substitute value.

Neovim version (nvim -v)

NVIM v0.10.0

mateuszmandera avatar Jul 28 '24 18:07 mateuszmandera

If you remove keeppatterns does it fix the issue? https://github.com/neovim/neovim/blob/ab561302a3b4dbb161aa6ef3ea39a6d1410a72fe/runtime/lua/editorconfig.lua#L159

justinmk avatar Aug 15 '24 17:08 justinmk

#30066 preserves the substitute string for &. If you want to preserve the string for ~ as well please open another issue.

zeertzjq avatar Aug 16 '24 23:08 zeertzjq