How can I prevent global defaults from overriding certain "filetype = vim" options in .vimrc when automatically searching for .vimrc?

Context

I have mine .vimrc

, automatically obtained with:

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

I also set default values ​​for the spacing:

set tabstop = 2 softtabstop = 2 shiftwidth = 2 expandtab

Later I use an event FileType

to override the default spacing given above with the spacing between files:

" 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 facing is that sourcing my will .vimrc

set the defaults but not raise the event FileType

. This means that when I do a :write

for mine .vimrc

while editing a file at a file type specific interval, the file type specific values ​​will be overridden by default!

UPDATE . As pointed out by Ingo, the problem only occurs in .vimrc

and does not occur in any other files.

Example

As an example, when I work on mine , the .vimrc

file size first depends ( ts=4 sts=4 sw=4 expandtab

), but after I made an arbitrary change in mine .vimrc

, the default spacing is ( tabstop=2 softtabstop=2 shiftwidth=2 expandtab

), but my file specific size is NOT loaded (since the event was FileType

not caused by writing to mine .vimrc

), leaving me with the default spacing!

My question (s)

How can I override the default spacing with specific filetype spacing and keep them even with auto search .vimrc

?

If possible, I would like to do this without getting rid of autocmd. Is it possible?

My guess is that I must somehow manually trigger the FileType event when searching. .vimrc

Can this be done and is it recommended?

+3


source to share


3 answers


I solved this problem by grouping the source autocmd

with another one autocmd

with the same event

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

      


I've tried this before, but I also used the command AirlineRefresh

for vim-airline as such:

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


which gave me this error:

E488: Trailing characters: AirlineRefresh | setlocal filetype = vim

Switching the order AirlineRefresh

and set local filetype=vim

fixes the error and results in the desired behavior:

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


source


Although you described the problem very clearly, I doubt this is happening.

Since you are using the :setlocal

file type correctly for certain parameters and :set

in yours ~/.vimrc

, finding the latter only affects the global defaults (and the current buffer, which is yours .vimrc

at the time of the event BufWritePost

). To affect other buffers, you .vimrc

must have a command in yours :bufdo

.

You can check this with



:verbose set ts? sts? sw? et?

      

To have your settings .vimrc

overridden by the global settings, you can add this:

if expand('%:t') ==# '.vimrc'
    setlocal filetype=vim
endif

      

+2


source


:bufdo e

will do it, but I couldn't just add it to mine .vimrc

. It will also remove syntax highlighting which is not that great.

:h bufdo

execute the command in every buffer in the buffer list

:h :e

change the current file

0


source







All Articles