Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am having trouble with Python's interaction with comments. These can be divided into two issues. I expect they're related, so I'll post them both here.

Issue 1.

When using gq to automatically wrap Python code gives the wrong result. For example, given this:

# Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod 
# tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim 
# veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
# commodo consequat. Duis aute irure dolor in reprehenderit in voluptate 

Selecting it and running gq gives:

# Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
# tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim #
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea #
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate

In contrast, running the same thing in YAML or Coffee-script gives:

# Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
# tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
# veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
# commodo consequat. Duis aute irure dolor in reprehenderit in voluptate

One would expect Python's comment rules to adhere to the same principles as Coffee-script or YAML or others that have #-based line-comments.

Issue 2.

Starting a comment causes the cursor to jump to the beginning of the line, when it should continue at the current depth.

For example:

class Hello(object):
   """Something"""
# The comment starts here

# or worse
  def hi(self):
# again, here
      pass

This is contrary to what is set out by PEP 8: block comments - adding a comment should indent as follows:

class Hello(object):
   """Something"""
   # The comment starts here

  # or worse
  def hi(self):
      # again, here
      pass

Settings

I am using the awesome Vundle script, and the plugins I am using are:

Bundle 'gmarik/vundle'
" navigation / display
Bundle 'kien/ctrlp.vim'
Bundle "tpope/vim-fugitive"
Bundle "ton/vim-bufsurf"
python import sys; sys.path.append("")
set rtp +=~/.vim/bundle/powerline/powerline/bindings/vim
Bundle 'Lokaltog/powerline'
Bundle 'nathanaelkane/vim-indent-guides'
Bundle 'altercation/vim-colors-solarized'    
" syntax
Bundle 'jQuery'
Bundle 'kchmck/vim-coffee-script'
" Bundle 'scrooloose/syntastic'
Bundle 'Syntastic'
Bundle "groenewege/vim-less"
Bundle "skammer/vim-css-color"
Bundle "hail2u/vim-css3-syntax"    
" Editing
Bundle 'AndrewRadev/splitjoin.vim'
Bundle 'tpope/vim-unimpaired'
Bundle 'rstacruz/sparkup.git'
Bundle 'klen/python-mode'
" Bundle 'sjl/gundo.vim'
Bundle 'mbbill/undotree'
" file-type based configs
filetype plugin indent on

From my .vimrc, here are the Python-related settings:

let g:syntastic_python_checker = 'pyflakes'
let g:syntastic_python_flake8_args = 
      \ '--ignore=W191,E501,E121,E122,E123,E128,E225,W291'
let pymode_lint = 0
au FileType python setlocal expandtab shiftwidth=4 tabstop=8
      \ formatoptions+=croq softtabstop=4 smartindent
      \ cinwords=if,elif,else,for,while,try,except,finally,def,class,with
let python_highlight_all=1
let python_highlight_exceptions=0
let python_highlight_builtins=0

Summary

Since Python is quite a bit older and more mature than YAML and Coffee-script, I am surprised and frustrated that Vim is producing the wrong results. It isn't apparent to me what might be going on - and I would be grateful for any thoughts and direction. Of course if I figure it out I'll post my conclusions.

share|improve this question
    
What is that "nmove" thing? –  romainl Jun 13 '13 at 20:46
    
@romainl When you highlight a block and hit <kbd>gq</kbd> Vim was telling me the command was called Nmove, though the docs say it's formatexpr if defined, otherwise formatprg if defined, or otherwise "done internally". –  Brian M. Hunt Jun 13 '13 at 21:55
    
Try without any plugins and with an empty .vimrc (except for one line: au FileType python setlocal textwidth=79) –  NewWorld Jul 11 at 8:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.