this post was submitted on 15 Jun 2024
15 points (94.1% liked)

Neovim

2168 readers
3 users here now

founded 1 year ago
MODERATORS
 

I'm normally a straight vim user (just out of habit, no particular preference) and I'm giving neovim a spin. So far I like it but...

For the love of all that's holy, how do I disable automatic indentation?

I have noautoindent set, nosmartindent set, filetype indent off, but neovim keeps inserting indentations. The only thing that works is setting paste on, but that's not the right solution to this problem.

Please help. This is driving me nuts!

all 15 comments
sorted by: hot top controversial new old
[โ€“] [email protected] 6 points 5 months ago (1 children)

Are you using treesitter? I think that has an option to handle indentation, but I'm not sure if it's enabled by default.

[โ€“] [email protected] 3 points 5 months ago* (last edited 5 months ago) (2 children)

I'm not using anything at the moment.

My intention was to give naked Neovim a spin and make sure it performs how I like Vim to perform. Then once it covers what I consider the basics, I was planning on layering kickstart.nvim on top of it and customizing Kickstart to my odd tastes.

The problem being, my odd tastes include:

1/ ABSOLUTELY NO AUTOINDENT. I hate autoindent with a burning passion, in all circumstances

2/ Must work in an 80-column terminal, meaning no line numbers - or at least line numbers that can be disabled. I've survived 40 years without line numbers, I can go on without them a few more ๐Ÿ™‚

Right now I'm stuck at 1/ without even having installed Kickstart. I'm not installing it until I manage to disable autoindent. And I still haven't found out how to do that, so I'm back to vanilla Vim for now because I have work to do.

[โ€“] [email protected] 4 points 5 months ago* (last edited 5 months ago) (2 children)

Using a the ubuntu 24.04 docker image for testing, I was able to disable automatic indentation with this config in ~/.config/nvim/init.lua:

vim.cmd("filetype indent off")

If you prefer using vim syntax it would instead be the following in ~/.config/nvim/init.vim:

filetype indent off

Note: it seems this file is not loaded if a init.lua file is present in that directory

Edit to add: So the reason this is required is, similar to vim (so you may already be familiar with this), there are filetype-specific configurations loaded. These usually reside in /usr/share/nvim/runtime/<plugin/indent/syntax/etc>/<filetype>. You can configure what files to load using the :filetype command.

There's more info here: https://neovim.io/doc/user/filetype.html

Second edit: Also when filetype indent/plugin/syntax is on, it seems to be loaded after your user config, so it overrides it. You can investigate if your actual config was applied or not by running, for example, :set autoindent? or :set cindent?. If the values do not match your configuration, it was likely overridden by :filetype. This was the case for me.

[โ€“] [email protected] 2 points 5 months ago (2 children)

What you describe seems to be what the user manual describes. OP did state "I have noautoindent set, nosmartindent set, filetype indent off" I have a hunch that they, comming from vim, used a .vim config file and didn't realize it wasn't loading because a .lua config file was already present in the folder. But this is just speculation.

[โ€“] [email protected] 2 points 5 months ago (1 children)

The only problem with that theory is that issuing the commands to disable any and all forms of autoindent manually within Neovim itself doesn't do anything either ๐Ÿ™‚

[โ€“] [email protected] 2 points 5 months ago

This sounds like is might be a legitimate bug in neovim.

[โ€“] [email protected] 2 points 5 months ago

Ah dang, you're right, I must have read it too quickly. Yeah then I also think it's something about not loading the config, it can be investigated by checking the runtime values like I described in my second edit.

[โ€“] [email protected] 2 points 5 months ago (1 children)

I tried issuing the commands inside Neovim itself, just in case it was a configuration loading issue. They don't do anything. It's maddening.

If you want to reproduce it, open a Makefile type a target, ENTER, and no matter what any autoindent setting is set to, the next line is indented by one tab, as if writing the recipe for that target. Nothing I do by hand, inside Neovim, after it's loaded and ready to use, will change that.

[โ€“] [email protected] 0 points 5 months ago* (last edited 5 months ago) (2 children)

Try running this: :set indentexpr= and then :set noautoindent. Without any config file, this works for me while in a makefile that looks like this:

foo: foo.c bar.h
        $(CC) $< -o $@

The indentexpr option is set by filetype, but disabling filetype indent after already opening a makefile is too late, it would need to happen before opening it (in either a config file or directly after running nvim without any file specified).

However, indentexpr seems to only control the automatic indentation when hitting enter at the target line, but not within the recipe for it. To fix that I also had to disable autoindent.

[โ€“] [email protected] 2 points 4 months ago* (last edited 4 months ago) (1 children)

Okay so...

I reinstalled Neovim 11 from scratch. ~/.config/nvim/ is empty:

  • I start "nvim Makefile", type "test:<ENTER": Neovim inserts a tab.
  • I start "nvim Makefile", type ":se noautoindent" (or ":se noai") then "test:": Neovim inserts a tab - i.e. Neovim ignores noautoindent
  • I start "nvim Makefile", type ":se indentexpr=" then "test:": Neovim does NOT insert a tab: that works!
  • I put "se indentexpr=" in ~/.config/nvim/init.vim, start "nvim Makefile", type "test:<ENTER": Neovim inserts a tab - i.e. it ignores the statement in init.vim. But it doesn't ignore other statements in init.vim: if I put "se bg=light" in it for instance, the background does indeed show up as light.

So it seems the crux of the issue is that init.vim isn't parsed properly.

EDIT: but putting "filetype indent off" in ~/config/nvim/init.vim seems to do the trick. Thanks for the hint! This is a lot more complicated than it needs to be ๐Ÿ™‚

EDIT #2: ":syntax off" doesn't turn off the syntax either. Well, I've had enough. Back to plain old vim...

[โ€“] [email protected] 1 points 4 months ago (1 children)

Oof, that's annoying.

Weird that :syntax off doesn't work, from a small test it seems to do the trick for me. But I guess as long as vim works there's no need to replace it ๐Ÿ™‚

[โ€“] [email protected] 2 points 4 months ago

Well I've only given Neovim a spin for a few hours, but it's been nothing but an exercise in frustration. Yeah syntax off works in vanilla nvim, but it's replaced by treesitter commands if treesitter is enabled. And treesitter is really, really invasive and aggressive when it comes to highlighting and transparently rewriting what's on the screen.

So basically, without treesitter, it's like vim, only more annoying to configure because init.lua is wildly inconsistent. With treesitter, it breaks my workflow at best (but I suppose I could get used to it) and it silently modifies what I see on the screen vs what I'm actually editing at worst, which is a hard no-no for me.

I think maybe if I configured treesitter from the ground up, I could manage to make it leave my text alone, keep the regex-based syntax highlighting which suits me just fine, and only make treesitter suggest things - which is the only feature I wanted to try Neovim for really. But it's just not worth the incredimazing complication. I've survived just fine without smart hinting from vi for decades, so I can easily do without it.

But hey, thanks man ๐Ÿ™‚

[โ€“] [email protected] 1 points 5 months ago

Gotcha. That's actually good because it will be easier to troubleshoot. I will try to reproduce in a barebones config and see if I can figure something out. What language are you editing, and what version of neovim do you use? Distro may also be relevant in case they package some indent.vim file(s).

[โ€“] [email protected] 0 points 5 months ago

I think you'll still want filetype indent on just as a matter of principle.

Weird suggestion: have you rebooted lately? I find sometimes (rarely) I get a file that will fight me on indent until reboot for some reason, and I think it's related to python updates but not sure. I've otherwise have never had problems.