• 0 Posts
  • 11 Comments
Joined 1 year ago
cake
Cake day: June 28th, 2023

help-circle




  • Oscar@programming.devtoProgrammer Humor@programming.devBrace Style
    link
    fedilink
    English
    arrow-up
    8
    ·
    edit-2
    3 months ago

    Linux uses 8 spaces. Excerpt from the official style guide:

    Tabs are 8 characters, and thus indentations are also 8 characters. There are heretic movements that try to make indentations 4 (or even 2!) characters deep, and that is akin to trying to define the value of PI to be 3.

    Rationale: The whole idea behind indentation is to clearly define where a block of control starts and ends. Especially when you’ve been looking at your screen for 20 straight hours, you’ll find it a lot easier to see how the indentation works if you have large indentations.

    Now, some people will claim that having 8-character indentations makes the code move too far to the right, and makes it hard to read on a 80-character terminal screen. The answer to that is that if you need more than 3 levels of indentation, you’re screwed anyway, and should fix your program.

    In short, 8-char indents make things easier to read, and have the added benefit of warning you when you’re nesting your functions too deep. Heed that warning.

    The reasoning seems sound, but I still prefer 4 personally.



  • 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.



  • 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.