Vim Tips Wiki
(Duplicate of 945, 1087, 1296, 1454)
m (Show current function name (for C programmers) moved to Show current function name in C programs: Page moved by JohnBot to improve title)

Revision as of 10:30, 18 October 2007

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Previous TipNext Tip

Tip: #1267 - Show current function name in C programs

Created: June 20, 2006 18:54 Complexity: intermediate Author: do1 --AT-- yandex . ru Version: 6.0 Karma: 56/25 Imported from: Tip#1267

(Explanation below.) Add following in your ~/.vimrc

fun! ShowFuncName() 
  let lnum = line(".")
  let col = col(".") 
  echohl ModeMsg 
  echo getline(search("^[^ \t#/]\\{2}.*[^:]\s*$", 'bW')) 
  echohl None 
  call search("\\%" . lnum . "l" . "\\%" . col . "c") 
endfun 
map f :call ShowFuncName() <CR> 


Or if you need the "f" key just map the function to whatever you like, for example to use ",f" just change the last line above to read:

map ,f :call ShowFuncName() <CR> 

So, now if you have jumped into a lengthy function (via tag or search) you can see its name without any further scrolling, just press f and voila! I find this useful for code digging.

Comments

call cursor(line,col)

vim at bertram dash scharpf dot de , June 21, 2006 3:07


Try this regexp for Java: echo getline(search("\\h\\+\\s\\+\\h\\+\\s*(.*)", 'bW'))

pmorant at mgtplc dot com , June 27, 2006 6:53


This works for Perl too. Won't it work for any language in which each line of a function body begins with whitespace (not just for C programmers)? E.g., PHP, shell scripts, vim scripts, etc.

emallove at yahoo dot com , July 9, 2006 13:09