Vim Tips Wiki
(Change to TipImported template + severe manual clean)
No edit summary
(8 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
{{Duplicate|945|1087|1296|1454}}
 
{{Duplicate|945|1087|1296|1454}}
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=1267
 
|id=1267
 
|previous=1266
 
|previous=1266
 
|next=1268
 
|next=1268
|created=June 20, 2006
+
|created=2006
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=do1
 
|author=do1
 
|version=6.0
 
|version=6.0
 
|rating=56/25
 
|rating=56/25
  +
|category1=
  +
|category2=
 
}}
 
}}
 
Add following in your vimrc:
 
Add following in your vimrc:
Line 22: Line 23:
 
call search("\\%" . lnum . "l" . "\\%" . col . "c")
 
call search("\\%" . lnum . "l" . "\\%" . col . "c")
 
endfun
 
endfun
map f :call ShowFuncName() <CR>
+
map f :call ShowFuncName() <CR>
 
</pre>
 
</pre>
   
Line 28: Line 29:
   
 
<pre>
 
<pre>
map ,f :call ShowFuncName() &lt;CR&gt;
+
map ,f :call ShowFuncName() <CR>
 
</pre>
 
</pre>
   
Now if you have jumped into a lengthy function (via tag or search) you can see its name without any further scrolling &ndash; just press <tt>f</tt>.
+
Now if you have jumped into a lengthy function (via tag or search) you can see its name without any further scrolling &ndash; just press <code>f</code>.
   
 
==Comments==
 
==Comments==
Line 43: Line 44:
 
echo getline(search("\\h\\+\\s\\+\\h\\+\\s*(.*)", 'bW'))
 
echo getline(search("\\h\\+\\s\\+\\h\\+\\s*(.*)", 'bW'))
 
</pre>
 
</pre>
  +
: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.
 
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):
  +
<pre>
  +
let sausage = input("Hit enter...")
  +
</pre>
  +
  +
----
  +
Following works for C++ as well as C:
  +
<pre>
  +
echo getline(search("^\\( \\{4}\\|\\t\\)\\?\\a\\S\\{-}\\( \\a\\S\\{-}\\)\\+\\s\\?(.*[^;]\\s\\{-}$", 'bW'))
  +
</pre>
  +
  +
it's not foolproof unfortunately. It will fail with following cases:
  +
<pre>
  +
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
  +
</pre>

Revision as of 10:40, 18 February 2014

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.

Comments

: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