A little help in VIM...

I'm working on an old, large codebase. (Who doesn't? ;)

Often, I'm handling modules containing around 8000 lines of code with subs (or JavaScript functions) being several hundred lines long - and I'd like to know how this monster is called I'm in right now.

So, thanks to Damian Conway's handy article series about vim scripts I created a quick and dirty hack to display the name of a sub (or a JavaScript function name) in my vim statusline:

function! FindSub()
  let subpattern = '\(sub\|function\) \w\+'
  let subline = search(subpattern, 'bnW')
  if !subline
    return 'not in sub'
  else
    return matchstr(getline(subline), subpattern)
  endif
endfunction

which gets incorporated (with line number and git branch I'm in) via set statusline:

%-10.(%l\ of\ %L%)\ \ %<%r%{FindSub()}\ \ %=%{GitBranchInfoTokens()[0]}

It's not perfect yet, but you get the idea. I'd like to match it JavaScript object literal stuff like foobar : function () { ... too.

Result:

displaysub.png

displayfunction.png

It automatically updates itself as the cursor moves into a different sub/function.

2 Comments

Thanks for the tip. Which color scheme do you use?

You might also be interested in vim TagList plugin.

Leave a comment

About Su-Shee

user-pic I ♥ Perl.