r/neovim • u/DanielMurphy22 • 1d ago
Need Help Why does whitespace get deleted when going from insert to normal mode?
In the scenario shown on the screenshot, when I'm in insert mode in a line which only contains whitespace, if I press Escape to go back to normal mode, the whitespace is deleted, and the line becomes empty.

So after I press Escape, the two tabs are deleted, the line becomes empty.
Does anyone know what setting or plugin might be doing this?
It only happens if the line _only_ contains whitespace. If there is at least one non-whitespace character on the line, then the whitespace (even if it's trailing) is preserved.
If I start neovim without using any of my config, then it does not happen, so it seems something in my config is causing this, but I don't know how I could pinpoint the cuplrit, any advice is appreciated.
7
u/Exciting_Majesty2005 lua 1d ago
You sure it doesn't happen when running nvim --clean?
If you have something like this,
txt
Some text
Hit <Enter> & Change to normal, it gets rid of the spaces(at least on v0.12.4).
1
u/DanielMurphy22 1d ago
Tested some more.
Even in withnvim --cleanit depends on thefiletype. If I startnvim --clean, and without doing anything else I type infunc main() { }Then in insert mode I go to the empty line, add a tab, then press Escape, the tab whitespace stays there.
But then if I do
:set filetype=go(or:set filetype=javascript), and try the same thing again, then when I press Escape, the whitespace is deleted.2
u/Exciting_Majesty2005 lua 1d ago
See the output of
:set autoindent?, they are most likely set byftplugin.Also,
autoindentonly triggers when creating a new line. So, manually addingtabwon't remove it.By default,
autoindentis set totrue.
5
u/TheLeoP_ 1d ago
If I start neovim without using any of my config, then it does not happen
That's false, this is built-in Neovim behavior. Of you want to get the indentation again, you can enter insert mode with cc.
2
u/DanielMurphy22 1d ago
You're right, I was mistaken, when I tested this the behavior did not depend on whether my config was applied or not, but rather on the filetype.
When I tested it with my config, I happened to be editing a buffer having the filetype go, then the whitespace is deleted, and when the filetype is not set, then it doesn't.
1
1
u/Confused-Armpit 1d ago
If you use something like conform.nvim it has a builtin formatter that deletes trailing whitespace, or it might be some other formatter from your lsp. Bare nvim isn't doing that, it is one of your plugins, or an autocmd.
0
u/dawidd8888 1d ago
Do you have nvim-treesitter plugin? Would be great if you included a link to your config or at least the plugin list. Also, to debug this yourself, try disabling plugins one by one and see.
11
u/AlfredKorzybski 1d ago
Probably
:help 'autoindent':