1

As a solution to the problem described in the question Why does vim not obey my expandtab in python files?, I have added this to my vimrc:

let g:use_tabs = 1
let g:indent_width = 4
let g:tab_width = 4

function! SetIndent()
    if g:use_tabs
        set noexpandtab
        set softtabstop=0
        let &shiftwidth = g:indent_width
        let &tabstop = g:tab_width
    else
        set expandtab
        let &softtabstop = g:indent_width
        let &shiftwidth = g:indent_width
        let &tabstop = g:tab_width
    endif
endfunction

autocmd VimEnter * call SetIndent()

This works perfectly when running vim in a terminal, but it seems like MacVim doesn't run the call SetIndent() command at all.

Is this because MacVim ignores VimEnter or runs it at another time than vim? How would I fix my vimrc so it also work in MacVim?


Edit: Link to my full vimrc: https://ghostbin.com/paste/3xnw7

Community
  • 1
  • 1
Tyilo
  • 28,998
  • 40
  • 113
  • 198
  • That function is absolutely pointless. You only need to put your settings in `~/.vim/after/ftplugin/python.vim`. – romainl Dec 30 '14 at 11:22
  • 1
    @romainl I would prefer to not have duplicate code for general and python indentation. – Tyilo Dec 30 '14 at 11:23
  • 1
    1. You already have duplicated code in your function. 2. My proposed solution is the de-facto Vim standard: filetype-specific settings going in proper ftplugins. – romainl Dec 30 '14 at 12:14
  • @romainl I don't know if vim only has default settings for python, but if it has more I would have to create a file for every language containing `call SetIndent()`. I wouldn't need to do that if vimenter worked. – Tyilo Dec 30 '14 at 12:19
  • `VimEnter` works in every Vim. Did you consider the possibility that it's not what you need? Also, Vim has default settings for Python and dozens of languages and yes, overriding them in `~/.vim/after/ftplugin/.vim` is *the* way to go. No need for such a convoluted non-solution to a non-problem. – romainl Dec 30 '14 at 13:55
  • When do you expect `VimEnter` to be triggered? – romainl Dec 30 '14 at 16:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/67932/discussion-between-tyilo-and-romainl). – Tyilo Dec 30 '14 at 16:10

1 Answers1

2

The default settings for Python are:

setlocal tabstop=8
setlocal softtabstop=4
setlocal shiftwidth=4
setlocal expandtab

If you want to change, say… 'expandtab', you only have to put the line below in ~/.vim/after/ftplugin/python.vim:

setlocal noexpandtab

Yes, you are expected to do that for every filetype for which you don't like the default settings. It is a lot simpler, quicker, more dependable and more maintainable than using a function.

KISS.

romainl
  • 186,200
  • 21
  • 280
  • 313