r/neovim 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.

8 Upvotes

11 comments sorted by

11

u/AlfredKorzybski 1d ago

Probably :help 'autoindent':

If you do not type anything on the new line except <BS> or CTRL-D and then type <Esc>, CTRL-O or <CR>, the indent is deleted again.

1

u/vim-help-bot 1d ago

Help pages for:


`:(h|help) <query>` | about | mistake? | donate | Reply 'rescan' to check the comment again | Reply 'stop' to stop getting replies to your comments

1

u/mdrjevois 22h ago

"We have AI at home"

"The ai"

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 with nvim --clean it depends on the filetype. If I start nvim --clean, and without doing anything else I type in

func 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 by ftplugin.

Also, autoindent only triggers when creating a new line. So, manually adding tab won't remove it.

By default, autoindent is set to true.

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

u/luizmarelo 1d ago

Maybe you have a plugin or custom config to remove trailing whitespace

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.