Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim won't properly indent Python code when using the = command

Tags:

python

vim

When I use the = command to indent an entire Python file or a section it won't properly indent it. Here's my vimrc:

set nocompatible
syntax on
set ruler
set tabstop=2
set softtabstop=2
set shiftwidth=2
set expandtab
set smarttab
set hlsearch
set incsearch
set ignorecase
set autoindent
" turn on line numbers:
set number
" Toggle line numbers and fold column for easy copying:
nnoremap <F2> :set nonumber!<CR>:set foldcolumn=0<CR>
nnoremap <F4> :set nospell!<CR>
nnoremap <F3> :set invpaste paste?<Enter>
imap <F3> <C-O><F3>
set pastetoggle=<F3>

filetype on
filetype plugin indent on
filetype plugin on

" Execute file being edited with <Shift> + e:
map <buffer> <S-e> :w<CR>:!/usr/bin/env python % <CR>
let g:solarized_termcolors=256
set background=dark
colorscheme solarized 
"set spell spelllang=en_us

set backspace=indent,eol,start
autocmd FileType python set complete+=k~/.vim/syntax/python.vim isk+=.,(
autocmd VimEnter * NERDTree

Also when I loop through my python files using w or b for instance, or when deleting it won't delete properly. For instance it will not stop on the . or ( when deleting a work before them and will even delete these.

like image 492
8vius Avatar asked Mar 23 '26 04:03

8vius


1 Answers

You should get rid of filetype on and filetype plugin on: filetype plugin indent on is the only line you need.

edit

The problem with . and ( is almost certainly caused by iskeyword. I vaguely remember someone having the same problem because he/she found out in some misinformed blog post that he/she needed dictionary-based completion. Because the entries in his/her dictionary file where in the form .method(, he/she needed the . to be considered a keyword character.

Try this command when editing a Python file:

:verbose set iskeyword?

It should return a comma separated list of values that includes . and ( and the place where it is set. It's most likely to be a third party python ftplugin because the default ftplugin doesn't touch iskeyword.

This line is what is causing the ./( problem:

autocmd FileType python set complete+=k~/.vim/syntax/python.vim isk+=.,(
  1. You absolutely don't need that completion mechanism because Vim's default omnicompletion is powerful enough.

  2. Because of how that completion mechanism is implemented and how your dictionary file may be written, . and ( must be considered by Vim as keyword characters.

  3. Your custom/ syntax file may not even be formed like that so this setting may not even work.

  4. isk is the short form of iskeyword, the option that defines what is a keyword character.

You seem to have copied settings from someone else without understanding what they did. The simple fact that the answer to your question was in your own ~/.vimrc should suffice to show you how wrong this idea is.

like image 130
romainl Avatar answered Mar 25 '26 17:03

romainl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!