2

So today I'm having some problems with vim. I wanted to use tab when I press the tab key, and a tab is equal to 8 spaces. This is what I added to my /etc/vimrc file:

:set noexpandtab
:set tabstop=8

It worked, for example .php files . But when I edit .py files, it still convert tab to spaces and use 4 spaces instead of 8 spaces. How to force to use the above setting to all file types?

2

1 Answer 1

3

First, it's bad practice to edit /etc/vimrc, which is likely to not be your user vimrc but the system vimrc (and, unless you are maintaining the system and want to set defaults for everyone using vim on the system, it's not the right thing to modify). Use your personal vimrc instead.

Second, if a particular filetype is not behaving correctly, use filetype-plugins. For example, if you still want Tab to be a literal tab character, and type the 4 spaces yourself in python:

" ~/.vim/after/ftplugin/python.vim
setlocal noexpandtab softtabstop=8

" make sure to adjust `b:undo_ftplugin`

Then (I think) your options are

  1. Type the 4 spaces manually;
  2. Use >> and << (and >/</:[range]>/:[range]<, etc.);
  3. Use Ctrl-i and Ctrl-d in insert mode

(This is because Python works best with 4 spaces, though they are not required.)

1
  • This solved my question after some review. Thank you! Commented Apr 4, 2022 at 9:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.