erikbstack
erikbstack

Reputation: 13254

How not to remove indentation by inserting `#`?

When using the autoindent configuration from VIM, it will automatically indent your cursor to a meaningful position after creating a new line. But when the first character you enter is a hash character (#) then the indentation will be removed and the # will be inserted as the first character of the line.

Why does this happen? How to configure VIM to not do that?

Example (_ as the empty cursor position):

def python_function():
    _

after clicking the# on the keyboard this happens:

def python_function():
#_

but what should have happened is this:

def python_function():
    #_

Upvotes: 3

Views: 463

Answers (3)

avenir
avenir

Reputation: 169

For me it was also the case that the indentation was not properly working when I have forgotten to disable paste mode with :set nopaste, when initially turning it on for copy pasting.

Upvotes: 0

ecatmur
ecatmur

Reputation: 157334

You might have smartindent or cindent instead of (or as well as) autoindent; these indent styles are designed for C-syntax languages. It's a good idea when editing Python to use :filetype plugin indent on as this will load appropriate indent settings for Python.

Upvotes: 4

William Pursell
William Pursell

Reputation: 212228

:help smartindent

Use the mapping :inoremap # X^H# (^h is entered via CTRL-V CTRL-H)

Upvotes: 4

Related Questions