Vim automatically removes indentation on Python comments

This question already has an answer here:

  • How to configure vim to not put comments at the beginning of lines while editing python files 8 answers

  • Setting smartindent on makes Vim behave like you describe for me, whereas with nosmartindent (which is what I tend to use) it behaves like you'd prefer it to.

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

    That seems to be it.


    Update: Probably no need to bother with the following... I'll leave it here for the added informational value. ;-)

    If setting nosmartindent doesn't help, perhaps you could use the :set command -- with no parameters -- to obtain the list of all settings in effect in your Vim session, then paste it somewhere (on Pastie perhaps). There's a few other options which affect automatic indentation, as far as I remember.


    While Michał's post explains what smartindent does, you can do a lot better than just turning it off. You could configure it more to your liking, or better yet, let Vim pick better indentation for you. With the following in your vimrc instead of other indent settings:

    filetype indent on
    

    Vim will automatically use the proper indent plugin for python. This is way better than just not de-indenting a # line - pretty much everything should be properly indented.


    你只能为python文件尝试一个选项:

    autocmd BufRead *.py inoremap # X<c-h>#<space>
    
    链接地址: http://www.djcxy.com/p/49528.html

    上一篇: gnome(通过SSH运行到Linux服务器)由KEYBOARD ONLY

    下一篇: Vim自动删除Python注释中的缩进