Whenever I want to use line continuation in my python script using macVim, I receive the following format:

a = one,\
        two,\
        three

instead of:

a = one,\
    two,\
    three

I've rifled through help, and was unable to locate what exactly is causing macVim to append an extra tab upon using line continuation.

Note: I currently have the following in my .vimrc file:

set tabstop=4
set shiftwidth=4
set expandtab
set softtabstop=4
有帮助吗?

解决方案 2

Your settings are almost fine. In Python code, the tab settings should always be

:set tabstop=8 shiftwidth=4 softtabstop=4 expandtab

These settings honour the recommendations in the "Style guide for Python code" known as PEP 8 and are universally followed in Python code. Stick to these settings, or even better, set up an autocommand that sets them automatically. In your vimrc:

autocmd FileType python setlocal ts=8 sw=4 sts=4 et

Now on to your problem: This is caused by the default indentation rules in Vim's indent script for Python. Fortunately, these rules can be customised. In your case you just need to put the following line in your vimrc.

let g:pyindent_continue = '&shiftwidth'

(In newer versions of Vim, use 'shiftwidth()' instead of '&shiftwidth'.)

For more information, look up :h ft-python-indent.

其他提示

You have your tabs set to 4 and not 2

Make sure that you have

filetype plugin indent on 

You can also use something as followed in your .vimrc as a oneliner for defaults instead of adding them on separate lines

Use 2 instead of 4

set ts=2 sts=2 sw=2 noexpandtab
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top