Vim Tips Wiki
Advertisement

Duplicate tip

This tip is very similar to the following:

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

Previous TipNext Tip

Tip: #1087 - Quick reference of current function

Created: December 27, 2005 19:01 Complexity: intermediate Author: TonyLiu Version: 6.0 Karma: 14/8 Imported from: Tip#1087

It is obliged for me to share this trick with you guys -- my friend.

It is useful for programmer who always want to know what function they are in and what parameters the current function have.

With marks, it is still boring with mark and jump,mark and jump every time you went into a function.

Here is the solution I use:

1.(to avoid the "Hit ENTER or type command to continue" message. )I suggest that you'd better set your command line height to 2 or more,because many functions definition have long width.

2.add this into your _vimrc

:set cmdheight=2 
nmap _F :call CurrentFunc()<CR> 
" side effect: REGISTER k and MARK k will be changed 

func! CurrentFunc() 
  exec "normal mk" 
  " c-type code have remarkable definitions from other OO code. 
  let l:extension = expand("%:e") 
  if l:extension == "c" 
    exec "normal ][%b%b" 
  else 
    exec "?private\\|public\\|protected\\|procedure\\|function\\s\\+\.*(" 
  endif 
  "TODO: maybe you need to open your closed fold at first 
  exec "normal v$\"ky`k" 
  exec "echo @k" 
endfunc " CurrentFunc 

Now, when you are editing or viewing a file ,exit to command mod, and type _F to see which is the current function.


If you wanna know or add some parameters followed by the definition, just type CTRL and O (<C-O>) to jump to the definition, and type `k can jump back.

.cs, .pas,.c,.sql filetype is supported with this trick.

thanks for readding.

Happy vimming.


email:IknowUknow2@hotmail.com


Comments

There is alternative method to jump to function definition, see if you like this:

If your C/C++ code is coded in Kernighan Richie style of indenting, that is if the open brace '{' of the function is starting at the first column of the C file, you can traverse to all functions of that file by pressing 'Template:' and '' keys.


For example the function definition should look like this:

column 0 
| 
| 
V 
void somefunc(void) 
{ 
}

msrinirao--AT--gmail.com , December 27, 2005 23:23


I suggest you add

if l:extension == "c" || l:extension == "cpp" 

because in cpp code public/private is not used in function definition so standard c method of obtaining header should be used.

tree--AT--apcoh.org , December 28, 2005 1:09


thanks for tree's note


Anonymous , December 28, 2005 4:36


Good idea, TonyLiu. There are a couple of things to be pointed out though:

  1. The tip above does not work well with non-Kernighan Richie indenting
  2. The tip will always display the function declaration even if the cursor is outside of the function

The function below remedies both 1 & 2.

"function declaration preview (double-backslash with default <Leader>) 
nmap <Leader><Leader> :cal FuncPreview()<CR> 

function FuncPreview() 
  let opening = search("^\\S.*)\\s*\\\(\\n\\\)\\={","bn") 
  let closing = search("^}","bn") 
  if opening > closing 
    echo getline(opening) 
  else 
    echo "" 
  endif 
endfunction 

Gerald Lai , December 28, 2005 8:07


Hello,

i tried your(s) ideas. Thank you for them.
If you have time, you could try also my script#1429, which makes the same thing. Basically I tested it with C.

It seems that (like mine) also your tips have problems with unmatched {} in comments.


fabioviso--AT--hotmail.com , December 29, 2005 13:31


Advertisement