.vimrc configuration for Python - python

My current .vimrc configuration is below:
set nohlsearch
set ai
set bg=dark
set showmatch
highlight SpecialKey ctermfg=DarkGray
set listchars=tab:>-,trail:~
set list
autocmd BufRead *.py set smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class
set tabstop=4
set shiftwidth=4
set expandtab
set autoindent
set smartindent
syntax on
set listchars=tab:>-
set listchars+=trail:.
set ignorecase
set smartcase
map <C-t><up> :tabr<cr>
map <C-t><down> :tabl<cr>
map <C-t><left> :tabp<cr>
map <C-t><right> :tabn<cr>
However, when I write python scripts, when I push "ENTER", it will go to the BEGINNING of the next line. What do I add so that it will auto-tab for me?

Try this:
filetype indent on
filetype on
filetype plugin on
I primarily do Python programming and this is the brunt of my vimrc
set nobackup
set nowritebackup
set noswapfile
set lines=40
set columns=80
set tabstop=4
set shiftwidth=4
set softtabstop=4
set autoindent
set smarttab
filetype indent on
filetype on
filetype plugin on

The short answer is that your autocmd is missing the BufEnter trigger, so it isn't being fired when you create a new file. Try this instead:
au BufEnter,BufRead *.py setlocal smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class
Note that I also changed the set to setlocal. This'll prevent these options from stomping on your other buffers' options.
The "right" way to do what you're trying to do is to add filetype indent on to your .vimrc. This'll turn on the built-in filetype based indentation. Vim comes with Python indentation support. See :help filetype-indent-on for more info.

Consider having a look at the official .vimrc for following PEP 7 & 8 conventions. Present over here
http://svn.python.org/projects/python/trunk/Misc/Vim/vimrc

You shouldn't have to explicitly indent python keywords. The $VIM/indent/python.vim file takes care of that. You just need to turn on filetype indent and autoindent.

I (simply) use this:
set shiftwidth=4
set tabstop=4
set expandtab
filetype plugin on
filetype indent on
syntax on

I'd say that this configuration provides something, without causing conflicts (/etc/vim/vimrc):
" Python Setup
autocmd Filetype python setlocal ts=2 sw=2 expandtab
autocmd Filetype python set number
autocmd Filetype python set autoindent
autocmd Filetype python set expandtab
autocmd Filetype python set shiftwidth=4
autocmd Filetype python set cursorline
autocmd Filetype python set showmatch
autocmd Filetype python let python_highlight_all = 1
autocmd Filetype python set colorcolumn=80

Related

Where is `filetype plugin` defined and how to override it?

I am trying set python indentation to use 2 spaces instead of 4 (or 8) spaces. I put the following in my ~/.vimrc:
set expandtab
set shiftwidth=2
set softtabstop=2
set tabstop=2
This works well, as long as I do not put filetype plugin on in the config as well. When I do have filetype plugin on, when I open a python file, set tabstop? will return 8 and the indentation got messed up.
So my questions are: (1) how does the filetype plugin on work and where is the filetype-specific config files located? (2) how can I override the filetype-specific config in my ~/.vimrc (or somewhere else)?
How does the filetype plugin on work and where is the filetype-specific config files located?
filetype on enables filetype detection, which does exactly what the name implies.
filetype plugin on enables both filetype detection and filetype plugins, which are vimscript files that contain filetype-specific commands for setting various options, defining mappings, etc. Those ftplugins are located under $VIMRUNTIME/ftplugin/ and you can make your own under ~/.vim/ftplugin/ or ~/.vim/after/ftplugin/ as well as various places under &runtimepath and/or &packpath.
Judging by the "python" tag, the ftplugin you are having problems with is $VIMRUNTIME/ftplugin/python.vim, which has this snippet:
if !exists("g:python_recommended_style") || g:python_recommended_style != 0
" As suggested by PEP8.
setlocal expandtab tabstop=4 softtabstop=4 shiftwidth=4
endif
How can I override the filetype-specific config in my ~/.vimrc (or somewhere else)?
In general, you can choose between two methods:
Add FileType autocommands in your .vimrc:
augroup myFiletypes
autocmd!
autocmd FileType foo setlocal this=that
[…]
augroup END
Put your settings in their own ftplugins:
" in after/ftplugin/foo.vim
setlocal this=that
The latter is cleaner.
In this specific case, the feature is behind a global variable so you simply keep your settings in your .vimrc "as-is" and add the following line (which will drive your coworkers mad):
let g:python_recommended_style = 0
as per :help ft-python-plugin.
First enable loading the plugin files for specific file types with
filetype plugin on
This actually loads the file "ftplugin.vim" in 'runtimepath'.
The result is that when a file is edited its plugin file is loaded (if there is one for the detected filetype).
You can enable loading the indent file with:
filetype indent on
You can merge it on :
filetype plugin indent on
Then customize you rule for python:
au FileType python set et sts=2 sw=2 ts=2

introduce <Tab> in new line while creating a block for python files in vim

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.

Auto-indent not working on Python's "with"

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

macVim: line continuation in python creates unnecessary tab?

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.

Auto indent doesn't work when using vim coding python

I want to use vim to write python code but there is a problem on auto indention.
First I downloaded the latest python.vim from http://www.vim.org/scripts/script.php?script_id=790 and putted it in the correct dir.
Then I edited my vimrc.
syntax on
set nu
set tabstop=4
set softtabstop=4
set shiftwidth=4
"set cindent
set autoindent
set smartindent
set expandtab
set filetype=python
au BufNewFile,BufRead *.py,*.pyw setf python
Now I find that keywords like 'for', 'if', 'while' can autoindent perfectly. But it doesn't work on 'def', 'try', 'except'.
What should I do? Thank you very much.
I have this line in my vimrc for long time, don't know if there is better way nowadays. but you could at least give it a try.
set cindent
autocmd FileType python setlocal foldmethod=indent smartindent shiftwidth=4 ts=4 et cinwords=if,elif,else,for,while,try,except,finally,def,class
and I have
filetype plugin indent on
too
That vim script you linked to doesn't do any auto-indentation, only syntax highlighting.
The auto-indentation you are observing is the one that's built into vim, it is designed for coding C, and it only recognizes the keywords described here:
http://vimdoc.sourceforge.net/htmldoc/options.html#%27cinwords%27
That's why it works for if and while but not def (there's no def in C).
You turned it on with set cindent.
You may want to try another script like this one:
http://www.vim.org/scripts/script.php?script_id=974

Categories

Resources