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 Complexity: intermediate Author: TonyLiu Version: 6.0 Karma: 14/8 Imported from: Tip#1087

This tip is useful for a programmer who always wants to know what function they are in, and what parameters the current function has.

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 function definitions have a 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 a file, exit to normal mode, and type _F to see which is the current function.

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

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

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 '{{' and '}}' keys.


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.


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

I tried your 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.


Advertisement