Search code examples
shellvimauto-indent

What setting in vim counteracts smartindent's refusal to indent # comments in shell scripts?


I recently started using vim 7 (previously vim 6) and the smartindent setting. For the most part, it works well, though I'm so used to typing a tab after an open brace that it is almost counter-productive.

However, there is one piece of maniacal behaviour. When editing a shell script, I try to create a comment at the current indent level, but smartindent will have nothing to do with it. It insists that the comment must be at level 0 (no indent). What's worse, it breaks shift-right ('>>' and friends) so that they do not work. This is outright insubordination, and I'd like to know what's the best way to fix it?

(I'm also not keen on smartindent's ideas about indenting then after if.)

Preferred solutions will save me manual bashing - I'm being lazy. One option would be 'turn off smartindent when editing shell scripts (leave it on for the rest)'. Another option would be guidelines on how to find the control script for smartindent and what to edit to change the characteristics I don't like. The final option (which I don't need advice on how to do - just the hint that it is the best, or only, way to restore sanity) is to leave smartindent unset.

I saw the vaguely related question on "(PHP and) annoying vim unindent rules"; it doesn't provide me with the direct answer, though maybe the cindent and related items mentioned in there are in fact part of the answer.


Solution

  • Find the indent file, (e.g. /usr/share/vim/vim71/indent/sh.vim on my system)

    This line looks like the problem:

    setlocal indentkeys-=:,0#
    

    Perhaps you can fix this in your .vimrc or load a custom indent file manually.

    edit: It looks more complicated than I thought, but maybe there is something specifically set in the indenting file that you would need to fix.

    2nd edit: Looks like I was completely wrong, Check out:

    Restoring indent after typing hash

    or

    howto-configure-vim-to-not-put-comments-at-the-beginning-of-lines-while-editing