6

When writing C/C++ code, vim defaults to forcing any pound sign ("#") to the zero position column. This is fine for for C/C++, but it is very annoying for shell scripting and perl scripting, since the pound sign is used for comments. For example, if I try to type this:

if [ -e foo.txt ]
then
    # This is a comment
    # which I want to stay aligned here
    do_the_stuff
fi

What I get instead is:

if [ -e foo.txt ]
then
# This is a comment
# which is no longer where I want it to be aligned!
    do_the_stuff
fi

...and I have to go and correct this by hand (really slows me down!). Now if I make sure cindent and smartindent are turned off, and turn on autoindent, the annoying "comment-grab" doesn't happen. However, the indentation is not very "smart" and does not respect the syntax as well as when smartindent is turned on. However, smartindent is better, but the auto-grabbing of '#' keeps happening. How can I turn off just the "comment-grab" without messing up the language-dependent indentation smarts? Is there a nifty switch I can just turn on in my .vimrc?

EDIT: I'm running Ubuntu Linux 16.04 x64 vim 7.4.1689

  • 4
    This shouldn't happen, and I can't reproduce it. Are you sure the filetype is set to sh? Also make sure there isn't some strange plugin or setting by starting Vim with minimal settings (see: How do I debug my vimrc file?). – Martin Tournoij Sep 15 '16 at 16:17
  • Not sure if this messing with your formatting or not, but I would give :h cino-# a look. It seems relevant. – Tumbler41 Sep 15 '16 at 16:29
  • If I run vim in default settings, with --noplugins, as you suggested, I get the exact same results. There is something with smartindent which is causing this problem. I'm running Ubuntu 16.04 x64 vim 7.4.1689, if that helps. – Doug Barbieri Sep 15 '16 at 16:31
7

As documented in :help 'smartindent'

When typing '#' as the first character in a new line, the indent for that line is removed, the '#' is put in the first column. The indent is restored for the next line. If you don't want this, use this mapping: ":inoremap # X^H#", where ^H is entered with CTRL-V CTRL-H.

That is, 'smartindent' causes this behavior and there's no way to disable it.

'smartindent' itself should pretty much be considered deprecated in favor of 'cindent' or the filetype specific indent scripts.

With 'cindent', this behavior can be controlled by 'cinkeys'. With filetype indent scripts, which you can enable by adding filetype indent on to your vimrc, there are scripts (located in the indent/ directory in the runtime paths) that help determine how to perform indentation based on the buffer's contents.

As implied by Carpetsmoker, an indent script for shell scripts comes with Vim already. Enabling the use of that will likely resolve the issue you're seeing and probably give better overall indent results.

  • Perfect! Thank you so much! That was exactly what I was looking for. :-) – Doug Barbieri Sep 15 '16 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.