Wikia

Vim Tips Wiki

Show current function name in C programs

Talk0
1,610pages on
this wiki

Redirected from VimTip1267

Duplicate tip

This tip is very similar to the following:

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

Tip 1267 Printable Monobook Previous Next

created 2006 · complexity intermediate · author do1 · version 6.0


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 to read:

map ,f :call ShowFuncName() <CR>

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.

CommentsEdit

:call cursor(line,col)

Try this regexp for Java:

echo getline(search("\\h\\+\\s\\+\\h\\+\\s*(.*)", 'bW'))
The regexp doesn't seem to work. It just prints the line where the cursor is.
This regexp works fine, at least in Vim 7.0

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.


If you have problems with redraw events after the message is displayed (so the function name can't be read) simply add the following line to the end of the code (before endfun):

let sausage = input("Hit enter...")

Following works for C++ as well as C:

echo getline(search("^\\( \\{4}\\|\\t\\)\\?\\a\\S\\{-}\\( \\a\\S\\{-}\\)\\+\\s\\?(.*[^;]\\s\\{-}$", 'bW'))

it's not foolproof unfortunately. It will fail with following cases:

false positives:
MyClass obj(int a,
            int b);
MyClass obj(int a, int b); // comment

false negatives:
MyFunction func(int a) // comment ending with semicolon;

and maybe more
Advertisement | Your ad here

Around Wikia's network

Random Wiki