There are some issues with the approach you described.
First, you are calling
autocmd! outside of an
augroup. This is really bad, since it's actually erasing all auto-commands!
Second, the logic in
SetFiletype() looks odd, since it's first checking whether the file type is
"vim" and then creating an auto-command to set the highlight group when entering a buffer (any buffer.)
If what you wanted to accomplish is to change the highlight group every time you enter a buffer with a specific filetype, it would make more sense to invert this approach, and have a fixed
autocmd to call a function when you enter a buffer, and then in that function check for the
&filetype and act appropriately. (You might also want to reset back to the original highlight group if you're entering a buffer of a different file type.)
if &filetype ==# 'vim'
hi MyStatusLineDevIcon ctermbg=8 cterm=bold ctermfg=3
hi MyStatusLineDevIcon ctermfg=7 ctermbg=0 cterm=NONE
autocmd BufEnter * call SetMyStatusLineDevIconHighlight()
This will work... But it will probably not do what you want. You see, highlight groups are actually global in Vim and Vim will use the same one for every window you have. So if you have split windows and in one of them you're editing a file of type
"vim", but in the other you're editing a file of a different type, Vim will always switch the status line of all windows to the highlight of the window you're currently in. So when you switch from a
"vim" window to a non-
"vim" window or vice-versa, Vim will switch them all.
There are ways to set the
'statusline' so that it takes the status of the current window (such as the file type of the buffer being edited there) in consideration, such as:
:setlocal to set a local
'statusline' setting for that specific window. You could set that in an
autocmd for the
WinEnter event, or even perhaps on one for a
FileType event, then use a specific highlight for the filetype.
'statusline' call a function to dynamically define what to use in the status line, then have that function look at the
g:statusline_winid variable which is set by Vim during the call to that function and query the
&filetype for the buffer being edited in that window. (That requires quite a few function calls in Vim to do right. Also, the
g:statusline_winid is a fairly addition around the Vim 8.1 timeline, so it might not be available in older versions of Vim, if you care about compatibility.)
There are many more caveats. For instance, the normal setup is for Vim to use different color highlights when a window is in focus (is the one where the cursor is) or not, highlight groups
StatusLineNC are used for this purpose. Then there are 9 additional groups
User9 which can be accessed through
%9* settings in the
'statusline' value. Furthermore, highlight settings for these
UserN groups also apply on top of
StatusLineNC, though only the difference with
StatusLine is what applies there.
'statusline' in Vim is quite complex and it's quite challenging to really get it right. But hopefully this answer will have addressed your question on