0

The cursor is on line 20. After pressing o the cursor (here depicted with a |) is placed below the e of ext as expected:

18 # set default extension
19 if [ -z "$2" ]; then
20   ext='pdf'
     |
21 else
22   ext="$2"
23 fi

If I now type a #, it is placed at the beginning of the line:

18 # set default extension
19 if [ -z "$2" ]; then
20   ext='pdf'
   #|
21 else
22   ext="$2"
23 fi

but expected is below the e of ext:

18 # set default extension
19 if [ -z "$2" ]; then
20   ext='pdf'
     #|
21 else
22   ext="$2"
23 fi

My comments are

comments=s1:/*,mb:*,ex:*/,://,b:#,:%,:XCOMM,n:>,fb:-

where

b:#

should be responsible for indent-aware comment repetition, but apparently the whitespace indentation (two blanks) is not recognized as such.

The same happens for me in Python files.

These are related settings in .vimrc (A .vimrc containing just these lines should reproduce the problem):

set autoindent
set expandtab
set shiftwidth=2
set smartindent
set smarttab
set softtabstop=2
set tabstop=2

Again, set autoindent should produce the expected behavior, but does not.

Is it a bug or a feature? :-)

1

I suppose your problem comes from setting 'smartindent'. The help has a short explanation on your problem:

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. When using the ">>" command, lines starting with '#' are not shifted right.

I believe this comes from the fact, that the '#' in the C language denotes a pre-processor statement (macro like), however that is only valid if it starts at the beginning of a line.

Since Vim patch 7.4.069, this behaviour has been slightly adjusted with the 'cino-#' option value for C-indenting which allows to shift '#' comment lines.

In general however, I would not recommend going with either 'smartindent' (which is not so smart) or 'autoindent'. Instead, I recommend using filetype specific indenting which is enabled using :filetype plugin indent on (see :help :filetype) in your vimrc and uses indenting specific for the filetype you are editing (which often is a custom 'indentexpr' function, but can also be C like indenting, autoindent or anything else).

| improve this answer | |

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.