When I edit a python file in Vim (using MacVim), and I press o to insert a new line, Vim throws the following errors:
Error detected while processing function <SNR>20_CheckAlign..GetPythonIndent:
line 30:
E121: Undefined variable: dummy
Press ENTER or type command to continue
Error detected while processing function <SNR>20_CheckAlign..GetPythonIndent:
line 30:
E15: Invalid expression: line('.') < 7 ? dummy : synIDattr(synID(line('.'), col('.')
, 1), 'name') =~ '\(Comment\|String\)$'
How do I fix this?
I figured out the problem. It was throwing an error whenever the file's tab settings were different from the editor's tab settings. For example, my test.py file was set to 2 spaces per tab, with tabs expanded into spaces, whereas my editor was set to 4 spaces per tab, no expand.
So the solution workaround was to set Vim's tab settings to the settings of the python file being edited.
Use the following modeline in your python files, that its tab settings are consistent.
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
Alternately, you have them set in your .vimrc file too.
set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
These are minimal set of things which you ensure consistency when working with python file.
There are some great vimrc examples available which you can use as well.
Changing the indentation settings did not work for me so I worked around this by modifying the python indentation file (/path/to/vim/indent/python.vim).
In the GetPythonIndent function I simply replaced all instances of dummy with 0. This fixed the problem for me.
Alternatively you could just set s:maxoff to something ridiculously high but this is somewhat less elegant.
To resolve the problem it seems one must edit the function GetPythonIndent() in /path/to/vim/indent/python.vim.
To do this, one must read in the help docs, user_30, indent, indentexpr, and indent-expression (also, C-indenting contains general info that will help explain what's going on).
The simplest solution therefore is to switch indenting off when you are editing a python file, until such time as you have time to deal with this irritating bug:
:set indentexpr=""
You will see in /path/to/vim/indent/python.vim that expression is set to run the buggy function GetPythonIndent:
setlocal indentexpr=GetPythonIndent(v:lnum)
Set it to "" there to save the inconvenience of unsetting it every time you open a python file.
Dani's answer above looks like it might be the better solution, since it would preserve some form of auto-indentation. But this will give you instant relief and save having to read and understand the buggy func.
Related
When I type the python code in vs code, indent tab size is 2. But when I save the file, tab size is changed to 4. I think this is because of autopep8.
How can I change the tab size, when I save the python code?
※ I already change editor.tabsize to 2
It's recommended to set '--indent-size' to change the autopep8 behavior. You can refer to this page for details. But I can't make it works in this way, so I directly
make a change in autopep8.py(under site-packages which you installed the autopep8) file, at the line of 112 change "DEFAULT_INDENT_SIZE = 4" to "DEFAULT_INDENT_SIZE = 2". It works and it's the same to set '--indent-size' in logic.
You will need to configure autopep8 to keep indent to two spaces. That way it won't undo how VS Code is inserting spaces for you.
I want to introduce a tab character in the new line for creating a new block in vim for python files.
Example
when I write
if(a < 5):
and press Enter, the cursor should come in next line with a tab after if in vertical alignment from the above line. Something like this
if(a < 5):
a = 5
I have configured my .vimrc file like this
set nu
set autoindent
set tabstop=4
syntax enable
set showmatch
colorscheme gruvbox
set bg=dark
"automatically creates a block after :
autocmd FileType python inoremap :<CR> :<CR><Tab>
Note:
1) Last line, I have written for the intended purpose mentioned in my question.
2) autoindent is set.
3) I dont want to use any plugin.
Problem I am facing:
When I press Enter after : , in addition to tab, one extra space is also created which creates wrong indentation.
Thanks in advance for helping
The way you're trying to do this is the hard way. There's a filetype plugin and a Python syntax type and it already knows what to do.
filetype plugin indent on
And don't use set autoindent. It's not very intelligent, it just copies the indent from the previous line. It's the opposite of what you're trying to do.
Files ending in .py should automatically get the right settings. If you need to force the current syntax type in a buffer, you can do that with
:set filetype=python
If you want to use tabs instead of spaces,
" Globally
set noexpandtab
" Python only
autocmd FileType python set noexpandtab
However, I'll note that using tabs in Python is not recommended and goes against published best practices (see PEP 8).
Lastly I'll mention that "plugin" is just a Vim term. All of this ships with Vim by default, this does not create a third-party dependency.
I am using VIM 7.4 on Ubuntu 16.04, and am coding for Python 3.5.
When writing a Python file (.py), the indentation works perfectly, except for the with statement. Namely, I could remap tab to indent by four spaces, VIM automatically indents by four spaces after a colon (:), and so on.
But when it comes to the with ... as syntax, it fails indenting a newline.
Visual example:
if x == 12:
then tab results on an indented newline, but
with open("test.txt", 'r') as file:
then tab resultes on a not indented newline.
I checked a lot of questions, that I won't link because they are too numerous, but here are some of the things I tried:
Adding set autoindent in my .vimrc;
Adding set smartindent in my .vimrc;
Typing filetype indent on in VIM's interpreter;
Typing :set indentkeys? and checking if the colon was in the list (it was).
It is nearly empty, so I doubt it will be useful, but here is my .vimrc:
set nocompatible
set autoindent
set smartindent
set tabstop=8 softtabstop=0 expandtab shiftwidth=4 smarttab
inoremap <S-Tab> <C-d>
How can I fix VIM so it recognizes with...as and indents after this on a newline?
The solution is indeed in the filetype indent on command.
For some reason, typing it in the VIM interpreter will not do anything. Therefore, one could think that this is not the way to fix this.
However, adding the following line to the .vimrc effectively fixed the problem:
filetype indent on
I'm using the plugin Python-Mode, which has an PymodeLintAuto feature which 'Fix PEP8 errors in current buffer automatically'. I was wondering how I could change the max line length for that was. I couldn't find in the docs
To change max line length in pymode you change
g:pymode_options_max_line_length
with let.
However this does not work for me, so do as #dillbert suggested.
For the colorbar:
autocmd FileType python set colorcolumn=120
in your .vimrc
In recent (as of June 2018) python-mode versions, the required configuration seems to be this:
let g:pymode_options_max_line_length = 88
let g:pymode_lint_options_pep8 = {'max_line_length': g:pymode_options_max_line_length}
let g:pymode_options_colorcolumn = 1
If Python-Mode is using PyLint, you can specify what errors you wish for it to raise. This involves creating a customised pylint.rc and then giving that to pymode using a setting in your .vimrc.
.vimrc:
" Pylint configuration file
let g:pymode_lint_config = '$HOME/pylint.rc'
pylint.rc:
[FORMAT]
# Maximum number of characters on a single line.
max-line-length=120
I tried the above answers, but none works, I have to write hard code the python-mode. Here is my solution:
Find pep8.py in your .vim, my is under /Users/chaopeng/.vim/bundle/Python-mode-klen/pylibs/pylama/pep8.py, then replace 79 with 119 in pep8.py.
And don't miss
let g:pymode_options_max_line_length=120
autocmd FileType python set colorcolumn=120
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
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
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.