Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expand tabs to spaces in vim only in python files?

How do I have the tab key insert 4 spaces when I'm editing "*.py" files and not any other files?

Following a recommendation from Vim and PEP 8 -- Style Guide for Python Code, I installed vim-flake8 (and vim-pathogen). This gives warnings when PEP8 style guidelines are violated. This is great, but I would for tabs to be expanded automatically in the first place when editing python files. I would like to have tab key actually insert tabs when editing other types of files.

In other words, I want the following to apply when I'm editing python files and only python files:

set expandtab       " tabs are converted to spaces set tabstop=4       " numbers of spaces of tab character set shiftwidth=4    " numbers of spaces to (auto)indent 
like image 499
Jonathan Avatar asked Apr 03 '12 02:04

Jonathan


People also ask

How do I expand a tab to spaces in vim?

vim Whitespace Convert tabs to spaces and spaces to tabs If you enable expandtab again :set expandtab then and run the :retab! command then all the tabs becomes spaces. If you want to do this for selected text then first select the text in visual mode.

How do you set a tab to 4 spaces in vim?

As for tabs, there are two settings. Within Vim, type a colon and then "set tabstop=4" which will set the tabs to display as four spaces. Hit colon again and type "set expandtab" which will insert spaces for tabs.

How do you change a tab to a space in Python?

In Python, we can use escape sequences to represent some characters like newline, tab, and more. The escape sequence for tabs is \t . We will search a string for tabs and replace them with spaces.


1 Answers

autocmd Filetype python setlocal expandtab tabstop=4 shiftwidth=4 

Or even shorter:

au Filetype python setl et ts=4 sw=4 
like image 64
Vlad the Impala Avatar answered Oct 01 '22 22:10

Vlad the Impala