I have a function that detects the name of the subroutine* my cursor is in, thanks to this answer. function! SubName() abort   if &syntax !='fortran'      return    endif   let prev_sub_line_num = search('subroutine ', 'bcnW')   return matchstr(getline(prev_sub_line_num), 'subroutine \zs\w\+')endfunctionIt is used in my statusline as follows :if &syntax== 'fortran'   set statusline+=%8*\ %{SubName()}\ endifIt works well, excepted that I have to source my .vimrc file upon opening VIM every time I want it to work. I would like some sort of 'autocmd' that would save me this sourcing at each session I'm starting -- and also understanding what's happening !(I naively tried autocmd BufWritePre,BufRead *.f* call SubName() but it does not seem to change anything).Edituser9433424 gave me an explanation as well as a solution through taking away the test around the set statusline command. It works, but now when opening a non-fortran file I get a small empty zone in my statusline with color highlight %8... And it's a little ugly. I understand why but I'd love to get rid of it !(Maybe I could just add a else case in my SubName function like :else   return You're awesome !endifbut it was not really my initial plan !)Edit IIAs asked by user9433424, here is the part of my .vimrc that concerns my statusline.*subroutine is the name for Fortran's subprograms.
Statusline function only displayed when sourcing .vimrc
vimscript;vimrc;statusline
'syntax' is an option which is local to a buffer, I don't think you should test it inside your vimrc but inside your function SubName(), which you already did. So, you probably don't need the test around set statusline+=%8*\ %{SubName()}\.Inside your vimrc, you could simply write this:function! SubName()   return &syntax !=# 'fortran' ? '' : ' ' . matchstr(getline(search('subroutine ', 'bcnW')), 'subroutine \zs\w\+') . ' 'endfunctionset statusline+=%8*%{SubName()}The reason why it didn't work is because during the startup, when Vim sources your vimrc, it's not in the context of a fortran buffer. The test fails and no %{SubName()} item is added to the statusline.The test has to be performed in the context of the current buffer, inside the function.The reason why, when opening a non-fortran file, you get a small empty zone in your statusline with color highlight %8 is because you didn't move the spaces around the item from your 'statusline' option to your function.In your previous code, you had this line:set statusline+=%8*\ %{SubName()}\ The backslash and space before and after the %{SubName()} item shouldn't be there, they should be inside your function where the syntax test is realised.In the code I wrote earlier, there's this line:   return &syntax !=# 'fortran' ? '' : ' ' . matchstr(getline(search('subroutine ', 'bcnW')), 'subroutine \zs\w\+') . ' 'Before and after the matchstr() function, there's ' ' . and . ' '. These are the original spaces which will be added around your subroutine name, but only if you're inside a fortran file.