3

Context

I have my .vimrc automatically being sourced with:

autocmd! BufWritePost ~/.vimrc source ~/.vimrc

I also set defaults for spacing:

set tabstop=2 softtabstop=2 shiftwidth=2 expandtab

Later on, I use the FileType event to override the spacing default above with filetype-specific spacing:

" Syntax of these languages is fussy over tabs & spaces¬
autocmd FileType make setlocal ts=8 sts=8 sw=8 noexpandtab¬
autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab¬                                                   

" Customisations based on house-style (arbitrary)
autocmd FileType sh setlocal ts=4 sts=4 sw=4 expandtab
autocmd FileType vim setlocal ts=4 sts=4 sw=4 expandtab

Problem

The problem I am having is that sourcing my .vimrc will set the default values, but will not trigger the FileType event. This means that when I do a :write to my .vimrc while editting a file with filetype-specific spacing, the filetype-specific spacings get overridden by the defaults!

UPDATE: As pointed out by Ingo, the issue only arises in .vimrc and does not occur in any other files.

Example

As an example, when I am working on my .vimrc, initially the file-specific spacing (ts=4 sts=4 sw=4 expandtab) is enabled, but after I make some arbitrary change in my .vimrc, the default spacing (tabstop=2 softtabstop=2 shiftwidth=2 expandtab) is loaded but my file-specific spacing is NOT loaded (since the FileType event was not triggered by a write to my .vimrc) leaving me with the default spacing!

My question(s)

How can I override the default spacing with filetype specific spacings and preserve these even when auto-sourcing .vimrc?

If possible, I would like to accomplish this without getting rid of the autocmd's. Is that possible?

My hunch is that I should somehow manually trigger the FileType event when sourcing .vimrc Can this be done and is this the recommended approach?

Pedro Cattori
  • 2,735
  • 1
  • 25
  • 43

3 Answers3

2

Though you've very clearly described the problem, I doubt that this is happening.

As you're correctly using :setlocal for the filetype-specific settings, and :set in your ~/.vimrc, the sourcing of the latter will only affect the global defaults (and the current buffer, which is your .vimrc during the BufWritePost event). To affect other buffers, there would need to be a :bufdo command in your .vimrc.

You can check this with

:verbose set ts? sts? sw? et?

To avoid that your .vimrc settings are overridden with the global settings, you can append this:

if expand('%:t') ==# '.vimrc'
    setlocal filetype=vim
endif
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • Good point about this only happening in .vimrc! Your suggestion worked, but it needed to be lower in the .vimrc file that the global default declaration to work. – Pedro Cattori Aug 19 '14 at 07:54
  • Yes, that's correct; I thought this was obvious, so I didn't mention it. Appending this to the original autocmd (as per your answer) probably works, too; it may just cause problems with `:wall`. – Ingo Karkat Aug 19 '14 at 08:39
  • I'm pretty new to this, so it wasn't immediately obvious but looking back, it does seem obvious now :) – Pedro Cattori Aug 19 '14 at 08:48
2

I ended up solving this issue by grouping the sourcing autocmd with another autocmd with the same event

" Reload changes to .vimrc automatically  
autocmd BufWritePost ~/.vimrc source ~/.vimrc | setlocal filetype=vim

I had tried this earlier, but I was also using the AirlineRefresh command for vim-airline as such:

autocmd BufWritePost ~/.vimrc source ~/.vimrc | AirlineRefresh | setlocal filetype=vim

which was giving me this error:

E488: Trailing characters:  AirlineRefresh | setlocal filetype=vim

Switching the order of AirlineRefresh and set local filetype=vim fixed the error and results in the desired behavior:

autocmd BufWritePost ~/.vimrc source ~/.vimrc | setlocal filetype=vim | AirlineRefresh
Pedro Cattori
  • 2,735
  • 1
  • 25
  • 43
0

:bufdo e will do it but I couldn't just add it to my .vimrc. It will also remove syntax highlighting which is not so good.

:h bufdo execute the command in each buffer in the buffer list

:h :e re-edit the current file

Brett Y
  • 7,171
  • 1
  • 28
  • 42