I am currently coding fortran, and I have huge subprograms (called subroutine in fortran). So I would like the name of the current subroutine (the subprogram my cursor is currently in) to be displayed in my statusline. This is what a subroutine looks like in fortran (this should make my trick clearer):subroutine my_subprogram(args)   stuff going on endI found the following trick, currently in my .vimrc file.function! SubName()   let current_line = line(.)   I store the current line number    ?subroutine                    searches the previous occurence of subroutine   let title_line = line(.)     stores the line where the subroutine title is   execute :.title_line         go to the line where the title is   normal! eeb                    put the cursor on the 2nd word :  my_subprogram   let sub_name = expand(<cword>)  extract the subroutine name in a variable   execute :.current_line       come back to the initial position   return sub_nameendfunctionThen I put the function in my statusline and it works almost fine : the name is displayed but... I cant do / searches anymore ! It highlights found text but I can't scroll to the next occurences of the pattern I'm looking for. I guess this might be because the function is permanently called and thus Vim permanently performs researches because of the ?subroutine line.(This is my first time trying to vimscript, so there are probably some ugly things there that you may want to correct -- please do not hesitate !)
Display current subroutine/subprogram in statusline
vimscript;statusline
You could try the following code:function! SubName() abort    let prev_sub_line_num = search('subroutine ', 'bcnW')    return matchstr(getline(prev_sub_line_num), 'subroutine \zs\w\+')endfunctionset stl+=%{SubName()}    let prev_sub_line_num = search('subroutine ', 'bcnW')Call the search() function to look for the pattern subroutine.In the 2nd argument, 4 flags are passed to search():b look backward instead of forwardc accept a match at current cursor positionn don't move the cursorW don't wrap around the end of the fileSo, search() returns the number of the line where the previous subroutine text is found.This number is stored in the variable prev_sub_line_num.return matchstr(getline(prev_sub_line_num), 'subroutine \zs\w\+')Then, matchstr() is called, which looks for the pattern subroutine \zs\w\+ inside the contents of the line prev_sub_line_num, and returns the matching text.2 arguments are passed to matchstr():getline(prev_sub_line_num) which gets the contents of the previous line where subroutine was foundthe pattern subroutine \zs\w\+ which matches the word (\w\+) just after the text subroutine. In a regex, \zs allows you to set the start of a match. Here, it allows you to get rid of subroutine, even though it's inside the pattern.Note that the contents of the statusline including the expression SubName() will be evaluated on every screen update, which could slow Vim down.